diff --git a/src/main/java/com/recom/api/map/MapExistsController.java b/src/main/java/com/recom/api/map/MapExistsController.java new file mode 100644 index 00000000..cbb3c2c1 --- /dev/null +++ b/src/main/java/com/recom/api/map/MapExistsController.java @@ -0,0 +1,97 @@ +package com.recom.api.map; + +import com.recom.api.commons.HttpCommons; +import com.recom.dto.map.exists.MapExistsRequestDto; +import com.recom.dto.map.exists.MapExistsResponseDto; +import com.recom.exception.HttpNotFoundException; +import com.recom.security.account.RECOMAccount; +import com.recom.security.account.RECOMAuthorities; +import com.recom.service.AssertionService; +import com.recom.service.ReforgerPayloadParserService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.CacheControl; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@Slf4j +@RestController +@Tag(name = "Maps") +@RequiredArgsConstructor +@RequestMapping("/api/v1/map/exists") +public class MapExistsController { + + @NonNull + private final AssertionService assertionService; + @NonNull + private final ReforgerPayloadParserService payloadParser; + + @Operation( + summary = "Determines existence of map", + description = "Checks if map is already scanned and known in the system.", + security = @SecurityRequirement(name = HttpCommons.BEARER_AUTHENTICATION_REQUIREMENT) + ) + @ApiResponses(value = { + @ApiResponse(responseCode = HttpCommons.OK_CODE, description = HttpCommons.OK), + @ApiResponse(responseCode = HttpCommons.UNAUTHORIZED_CODE, description = HttpCommons.UNAUTHORIZED, content = @Content()) + }) + @PostMapping(path = "/form", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + public ResponseEntity mapExistsForm( + @AuthenticationPrincipal final RECOMAccount account, + @RequestParam(required = true) + @NonNull final Map payload + ) { + log.debug("Requested POST /api/v1/map/exists/form (FORM)"); + + return mapExists(account, payloadParser.parseValidated(payload, MapExistsRequestDto.class)); + } + + @Operation( + summary = "Determines existence of map", + description = "Checks if map is already scanned and known in the system.", + security = @SecurityRequirement(name = HttpCommons.BEARER_AUTHENTICATION_REQUIREMENT) + ) + @ApiResponses(value = { + @ApiResponse(responseCode = HttpCommons.OK_CODE, description = HttpCommons.OK), + @ApiResponse(responseCode = HttpCommons.UNAUTHORIZED_CODE, description = HttpCommons.UNAUTHORIZED, content = @Content()) + }) + @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE) + @Secured({RECOMAuthorities.EVERYBODY}) + public ResponseEntity mapExists( + @AuthenticationPrincipal final RECOMAccount account, + @RequestBody final MapExistsRequestDto mapExistsRequestDto + ) { + log.debug("Requested GET /api/v1/map/exists (JSON)"); + + try { + assertionService.assertMapExists(mapExistsRequestDto.getMapName()); + return ResponseEntity.status(HttpStatus.OK) + .cacheControl(CacheControl.noCache()) + .body(MapExistsResponseDto.builder() + .mapName(mapExistsRequestDto.getMapName()) + .mapExists(true) + .build()); + } catch (final HttpNotFoundException e) { + return ResponseEntity.status(HttpStatus.OK) + .cacheControl(CacheControl.noCache()) + .body(MapExistsResponseDto.builder() + .mapName(mapExistsRequestDto.getMapName()) + .mapExists(false) + .build()); + } + } + +} diff --git a/src/main/java/com/recom/dto/map/exists/MapExistsRequestDto.java b/src/main/java/com/recom/dto/map/exists/MapExistsRequestDto.java new file mode 100644 index 00000000..5f3576ab --- /dev/null +++ b/src/main/java/com/recom/dto/map/exists/MapExistsRequestDto.java @@ -0,0 +1,28 @@ +package com.recom.dto.map.exists; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@Schema +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class MapExistsRequestDto implements Serializable { + + @NotBlank + @Schema + @JsonProperty() + private String mapName; + +} + diff --git a/src/main/java/com/recom/dto/map/exists/MapExistsResponseDto.java b/src/main/java/com/recom/dto/map/exists/MapExistsResponseDto.java new file mode 100644 index 00000000..6963ddec --- /dev/null +++ b/src/main/java/com/recom/dto/map/exists/MapExistsResponseDto.java @@ -0,0 +1,33 @@ +package com.recom.dto.map.exists; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@Schema +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class MapExistsResponseDto implements Serializable { + + @NotBlank + @Schema + @JsonProperty() + private String mapName; + + @NotBlank + @Schema + @JsonProperty() + private boolean mapExists; + +} +