Skip to content

Commit

Permalink
add map/exists Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Oct 28, 2023
1 parent c493ed9 commit fd7845c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/main/java/com/recom/api/map/MapExistsController.java
Original file line number Diff line number Diff line change
@@ -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<MapExistsResponseDto> mapExistsForm(
@AuthenticationPrincipal final RECOMAccount account,
@RequestParam(required = true)
@NonNull final Map<String, String> 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<MapExistsResponseDto> 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());
}
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/recom/dto/map/exists/MapExistsRequestDto.java
Original file line number Diff line number Diff line change
@@ -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;

}

33 changes: 33 additions & 0 deletions src/main/java/com/recom/dto/map/exists/MapExistsResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;

}

0 comments on commit fd7845c

Please sign in to comment.