View Javadoc
1   package fr.tiogars.inception.domains.domaintemplate.controllers;
2   
3   import java.net.URI;
4   
5   import org.springframework.http.ResponseEntity;
6   import org.springframework.web.bind.annotation.PostMapping;
7   import org.springframework.web.bind.annotation.RequestBody;
8   import org.springframework.web.bind.annotation.RestController;
9   
10  import fr.tiogars.inception.domains.domaintemplate.models.CreateDomainTemplateRequest;
11  import fr.tiogars.inception.domains.domaintemplate.models.DomainTemplate;
12  import fr.tiogars.inception.domains.domaintemplate.services.DomainTemplateCreateService;
13  import io.swagger.v3.oas.annotations.Operation;
14  import io.swagger.v3.oas.annotations.responses.ApiResponse;
15  import io.swagger.v3.oas.annotations.responses.ApiResponses;
16  import io.swagger.v3.oas.annotations.tags.Tag;
17  import jakarta.validation.Valid;
18  
19  /**
20   * Exposes the endpoint that creates domain records.
21   *
22   * @since 0.1.0
23   */
24  @Tag(name = "DomainTemplates", description = "CRUD operations for the DomainTemplate business object.")
25  @RestController
26  public class DomainTemplateCreateController {
27  
28          private final DomainTemplateCreateService service;
29  
30          /**
31           * Creates the record creation endpoint.
32           *
33           * @param service domain record creation service
34           */
35          public DomainTemplateCreateController(DomainTemplateCreateService service) {
36                  this.service = service;
37          }
38  
39          /**
40           * Creates a new version of a domain record.
41           *
42           * @param request validated version and payload to create
43           * @return a created response containing the record, or a conflict response when
44           *         the version exists
45           */
46          @Operation(summary = "Create a record version", description = "Stores a new version for the record identified by the domain and key.")
47          @ApiResponses(value = {
48                          @ApiResponse(responseCode = "201", description = "The record version was created."),
49                          @ApiResponse(responseCode = "400", description = "The record version or payload is invalid."),
50                          @ApiResponse(responseCode = "409", description = "A record with the same domain, key, and version already exists.")
51          })
52          @PostMapping("/api/v1/domaintemplate")
53          public ResponseEntity<DomainTemplate> createDomainTemplate(
54                          @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Business data of the template to create.", required = true) @Valid @RequestBody CreateDomainTemplateRequest request) {
55                  return service.create(request)
56                                  .map(created -> ResponseEntity
57                                                  .created(URI.create("/api/v1/domaintemplate/" + created.id()))
58                                                  .body(created))
59                                  .orElseGet(() -> ResponseEntity.status(409).build());
60          }
61  }