View Javadoc
1   package fr.tiogars.domaintemplate.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.domaintemplate.domains.domaintemplate.entities.DomainTemplate;
11  import fr.tiogars.domaintemplate.domains.domaintemplate.models.CreateDomainTemplateRequest;
12  import fr.tiogars.domaintemplate.domains.domaintemplate.services.DomainTemplateService;
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 DomainTemplateService service;
29  
30      /**
31       * Creates the record creation endpoint.
32       *
33       * @param service domain record service
34       */
35      public DomainTemplateCreateController(DomainTemplateService 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(
47              summary = "Create a record version",
48              description = "Stores a new version for the record identified by the domain and key.")
49      @ApiResponses(value = {
50              @ApiResponse(responseCode = "201", description = "The record version was created."),
51              @ApiResponse(responseCode = "400", description = "The record version or payload is invalid."),
52              @ApiResponse(
53                      responseCode = "409",
54                      description = "A record with the same domain, key, and version already exists.")
55      })
56      @PostMapping("/api/v1/domaintemplate")
57      public ResponseEntity<DomainTemplate> createDomainTemplate(
58              @io.swagger.v3.oas.annotations.parameters.RequestBody(
59                      description = "Business data of the template to create.",
60                      required = true)
61              @Valid
62              @RequestBody
63              CreateDomainTemplateRequest request) {
64          return service.create(request)
65                  .map(created -> ResponseEntity
66                          .created(URI.create("/api/v1/domaintemplate/" + created.id()))
67                          .body(created))
68                  .orElseGet(() -> ResponseEntity.status(409).build());
69      }
70  }