DomainTemplateUpdateController.java

package fr.tiogars.domaintemplate.domains.domaintemplate.controllers;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import fr.tiogars.domaintemplate.domains.domaintemplate.entities.DomainTemplate;
import fr.tiogars.domaintemplate.domains.domaintemplate.models.UpdateDomainTemplateRequest;
import fr.tiogars.domaintemplate.domains.domaintemplate.services.DomainTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

/**
 * Exposes the endpoint that updates domain records.
 *
 * @since 0.1.0
 */
@Tag(name = "DomainTemplates", description = "CRUD operations for the DomainTemplate business object.")
@RestController
public class DomainTemplateUpdateController {

    private final DomainTemplateService service;

    /**
     * Creates the record update endpoint.
     *
     * @param service domain record service
     */
    public DomainTemplateUpdateController(DomainTemplateService service) {
        this.service = service;
    }

    /**
     * Replaces the payload of an existing record version.
     *
     * @param id      persistent template identifier
     * @param request validated version and replacement payload
     * @return an OK response containing the record, or a not-found response when
     *         the version is absent
     */
    @Operation(
            summary = "Replace a record version",
            description = "Replaces the payload of an existing version identified by the domain and record key.")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "The record version was updated."),
            @ApiResponse(responseCode = "400", description = "The record version or payload is invalid."),
            @ApiResponse(
                    responseCode = "404",
                    description = "No record exists for the supplied domain, key, and version.")
    })
    @PutMapping("/api/v1/domaintemplate/{id}")
    public ResponseEntity<DomainTemplate> updateDomainTemplate(
            @Parameter(
                    description = "Persistent template identifier.",
                    required = true,
                    example = "42")
            @PathVariable
            Long id,
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "Replacement label and description.",
                    required = true)
            @Valid
            @RequestBody
            UpdateDomainTemplateRequest request) {
        return service.update(id, request)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }
}