View Javadoc
1   package fr.tiogars.inception.domains.domaintemplate.controllers;
2   
3   import org.springframework.http.ResponseEntity;
4   import org.springframework.web.bind.annotation.PathVariable;
5   import org.springframework.web.bind.annotation.PutMapping;
6   import org.springframework.web.bind.annotation.RequestBody;
7   import org.springframework.web.bind.annotation.RestController;
8   
9   import fr.tiogars.inception.domains.domaintemplate.models.DomainTemplate;
10  import fr.tiogars.inception.domains.domaintemplate.models.UpdateDomainTemplateRequest;
11  import fr.tiogars.inception.domains.domaintemplate.services.DomainTemplateUpdateService;
12  import io.swagger.v3.oas.annotations.Operation;
13  import io.swagger.v3.oas.annotations.Parameter;
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 updates 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 DomainTemplateUpdateController {
27  
28          private final DomainTemplateUpdateService service;
29  
30          /**
31           * Creates the record update endpoint.
32           *
33           * @param service domain record update service
34           */
35          public DomainTemplateUpdateController(DomainTemplateUpdateService service) {
36                  this.service = service;
37          }
38  
39          /**
40           * Replaces the payload of an existing record version.
41           *
42           * @param id      persistent template identifier
43           * @param request validated version and replacement payload
44           * @return an OK response containing the record, or a not-found response when
45           *         the version is absent
46           */
47          @Operation(summary = "Replace a record version", description = "Replaces the payload of an existing version identified by the domain and record key.")
48          @ApiResponses(value = {
49                          @ApiResponse(responseCode = "200", description = "The record version was updated."),
50                          @ApiResponse(responseCode = "400", description = "The record version or payload is invalid."),
51                          @ApiResponse(responseCode = "404", description = "No record exists for the supplied domain, key, and version.")
52          })
53          @PutMapping("/api/v1/domaintemplate/{id}")
54          public ResponseEntity<DomainTemplate> updateDomainTemplate(
55                          @Parameter(description = "Persistent template identifier.", required = true, example = "42") @PathVariable Long id,
56                          @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Replacement label and description.", required = true) @Valid @RequestBody UpdateDomainTemplateRequest request) {
57                  return service.update(id, request)
58                                  .map(ResponseEntity::ok)
59                                  .orElseGet(() -> ResponseEntity.notFound().build());
60          }
61  }