• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torresj / email-register-api / 29

25 Jul 2025 09:48AM UTC coverage: 53.704% (-4.3%) from 58.0%
29

push

circleci

Jaime Torres Benavente
New endpoint to download emails in text added

0 of 8 new or added lines in 1 file covered. (0.0%)

58 of 108 relevant lines covered (53.7%)

0.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.67
/src/main/java/com/torresj/email_register_api/controllers/EmailController.java
1
package com.torresj.email_register_api.controllers;
2

3
import com.torresj.email_register_api.dtos.DeleteRequestDto;
4
import com.torresj.email_register_api.dtos.RegisterRequestDto;
5
import com.torresj.email_register_api.exceptions.InvalidEmailException;
6
import com.torresj.email_register_api.servicies.EmailService;
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.media.ArraySchema;
9
import io.swagger.v3.oas.annotations.media.Content;
10
import io.swagger.v3.oas.annotations.media.Schema;
11
import io.swagger.v3.oas.annotations.responses.ApiResponse;
12
import io.swagger.v3.oas.annotations.responses.ApiResponses;
13
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
14
import lombok.RequiredArgsConstructor;
15
import lombok.extern.slf4j.Slf4j;
16
import org.springframework.core.io.ByteArrayResource;
17
import org.springframework.core.io.Resource;
18
import org.springframework.http.HttpHeaders;
19
import org.springframework.http.MediaType;
20
import org.springframework.http.ResponseEntity;
21
import org.springframework.security.access.prepost.PreAuthorize;
22
import org.springframework.web.bind.annotation.DeleteMapping;
23
import org.springframework.web.bind.annotation.GetMapping;
24
import org.springframework.web.bind.annotation.PostMapping;
25
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RestController;
28

29
import java.nio.charset.StandardCharsets;
30
import java.util.List;
31

32
@RestController
33
@RequestMapping("/v1/emails")
34
@Slf4j
1✔
35
@RequiredArgsConstructor
36
public class EmailController {
37

38
    private final EmailService emailService;
39

40
    @Operation(summary = "Register email", security = @SecurityRequirement(name = "basicScheme"))
41
    @ApiResponses(
42
            value = {
43
                    @ApiResponse(
44
                            responseCode = "200",
45
                            description = "Registered"
46
                    ),
47
            })
48
    @PostMapping()
49
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
50
    public ResponseEntity<String> register(
51
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
52
                    description = "Email to be registered",
53
                    required = true,
54
                    content = @Content(schema = @Schema(implementation = RegisterRequestDto.class)))
55
            @RequestBody RegisterRequestDto request
56
    ) throws InvalidEmailException {
57
        emailService.register(request.getEmail());
×
58
        return ResponseEntity.ok("{\"status\":\"Registered\"}");
×
59
    }
60

61
    @Operation(summary = "Get registered emails", security = @SecurityRequirement(name = "basicScheme"))
62
    @ApiResponses(
63
            value = {
64
                    @ApiResponse(
65
                            responseCode = "200",
66
                            description = "ok",
67
                            content = {
68
                                    @Content(
69
                                            mediaType = "application/json",
70
                                            array = @ArraySchema(schema = @Schema(implementation = String.class)))
71
                            }
72
                    ),
73
            })
74
    @GetMapping()
75
    @PreAuthorize("hasRole('ADMIN')")
76
    public ResponseEntity<List<String>> getEmails() {
77
        var emails = emailService.getEmails();
×
78
        return ResponseEntity.ok(emails);
×
79
    }
80

81
    @Operation(summary = "Get registered emails in a file", security = @SecurityRequirement(name = "basicScheme"))
82
    @ApiResponses(
83
            value = {
84
                    @ApiResponse(
85
                            responseCode = "200",
86
                            description = "ok",
87
                            content = {
88
                                @Content(
89
                                        mediaType = "text/plain",
90
                                        schema = @Schema(implementation = Resource.class)
91
                                )
92
                            }
93
                    ),
94
            })
95
    @GetMapping("/export")
96
    @PreAuthorize("hasRole('ADMIN')")
97
    public ResponseEntity<Resource> getEmailsInFile() {
NEW
98
        var emails = emailService.getEmails();
×
NEW
99
        String content = String.join(", ", emails);
×
100

NEW
101
        ByteArrayResource resource = new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
×
102

NEW
103
        return ResponseEntity.ok()
×
NEW
104
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"emails.txt\"")
×
NEW
105
                .contentType(MediaType.TEXT_PLAIN)
×
NEW
106
                .contentLength(resource.contentLength())
×
NEW
107
                .body(resource);
×
108
    }
109

110
    @Operation(summary = "Delete email", security = @SecurityRequirement(name = "basicScheme"))
111
    @ApiResponses(
112
            value = {
113
                    @ApiResponse(
114
                            responseCode = "200",
115
                            description = "Deleted"
116
                    ),
117
            })
118
    @DeleteMapping()
119
    @PreAuthorize("hasRole('ADMIN')")
120
    public ResponseEntity<String> remove(
121
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
122
                    description = "Email to be deleted",
123
                    required = true,
124
                    content = @Content(schema = @Schema(implementation = DeleteRequestDto.class)))
125
            @RequestBody DeleteRequestDto request
126
    ) throws InvalidEmailException {
127
        emailService.remove(request.getEmail());
×
128
        return ResponseEntity.ok("{\"status\":\"Deleted\"}");
×
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc