DomainTemplateCreateController.java

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

import java.net.URI;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
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.CreateDomainTemplateRequest;
import fr.tiogars.domaintemplate.domains.domaintemplate.services.DomainTemplateService;
import io.swagger.v3.oas.annotations.Operation;
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 creates domain records.
 *
 * @since 0.1.0
 */
@Tag(name = "DomainTemplates", description = "CRUD operations for the DomainTemplate business object.")
@RestController
public class DomainTemplateCreateController {

    private final DomainTemplateService service;

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

    /**
     * Creates a new version of a domain record.
     *
     * @param request validated version and payload to create
     * @return a created response containing the record, or a conflict response when
     *         the version exists
     */
    @Operation(
            summary = "Create a record version",
            description = "Stores a new version for the record identified by the domain and key.")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "The record version was created."),
            @ApiResponse(responseCode = "400", description = "The record version or payload is invalid."),
            @ApiResponse(
                    responseCode = "409",
                    description = "A record with the same domain, key, and version already exists.")
    })
    @PostMapping("/api/v1/domaintemplate")
    public ResponseEntity<DomainTemplate> createDomainTemplate(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "Business data of the template to create.",
                    required = true)
            @Valid
            @RequestBody
            CreateDomainTemplateRequest request) {
        return service.create(request)
                .map(created -> ResponseEntity
                        .created(URI.create("/api/v1/domaintemplate/" + created.id()))
                        .body(created))
                .orElseGet(() -> ResponseEntity.status(409).build());
    }
}