85 lines
3.6 KiB
Java
85 lines
3.6 KiB
Java
package org.lanit.controllers;
|
|
import org.lanit.models.*;
|
|
import org.lanit.service.JsonProcessingService;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import java.io.IOException;
|
|
import java.time.Instant;
|
|
import org.springframework.http.MediaType;
|
|
@Controller
|
|
public class JSONController {
|
|
|
|
private final JsonProcessingService processingService;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
public JSONController(JsonProcessingService processingService, ObjectMapper objectMapper) {
|
|
this.processingService = processingService;
|
|
this.objectMapper = objectMapper;
|
|
}
|
|
|
|
|
|
private ResponseEntity<Object> createJsonResponse(Object body, HttpStatus status) {
|
|
return ResponseEntity.status(status)
|
|
.header("Content-Type", "application/json")
|
|
.body(body);
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/json", produces = "application/json", consumes = MediaType.APPLICATION_JSON_VALUE)
|
|
@ResponseBody
|
|
public ResponseEntity<?> handleJsonRequest(
|
|
@RequestParam("action") String action,
|
|
@RequestBody String jsonBody) throws IOException {
|
|
|
|
System.out.println("Received JSON: " + jsonBody);
|
|
System.out.println("Action: " + action);
|
|
|
|
try {
|
|
if ("add".equals(action)) {
|
|
AddRequest addRequest = objectMapper.readValue(jsonBody, AddRequest.class);
|
|
Info modifiedInfo = processingService.processAdd(addRequest);
|
|
|
|
Response response = new Response();
|
|
response.setInfo(modifiedInfo);
|
|
response.setUuid(addRequest.getUuid());
|
|
response.setLastUpdate(Instant.now().toString());
|
|
return createJsonResponse(response, HttpStatus.OK);
|
|
|
|
} else if ("delete".equals(action)) {
|
|
DeleteRequest deleteRequest = objectMapper.readValue(jsonBody, DeleteRequest.class);
|
|
Info modifiedInfo = processingService.processDelete(deleteRequest);
|
|
|
|
Response response = new Response();
|
|
response.setInfo(modifiedInfo);
|
|
response.setUuid(deleteRequest.getUuid());
|
|
response.setLastUpdate(Instant.now().toString());
|
|
return createJsonResponse(response, HttpStatus.OK);
|
|
|
|
} else {
|
|
ErrorResponse error = new ErrorResponse();
|
|
error.setStatus("error");
|
|
error.setMessage("Передан некорректный action - " + action);
|
|
return createJsonResponse(error, HttpStatus.OK);
|
|
}
|
|
|
|
} catch (RuntimeException e) {
|
|
ErrorResponse error = new ErrorResponse();
|
|
error.setStatus("error");
|
|
error.setMessage(e.getMessage());
|
|
HttpStatus status = e.getMessage().contains("тикер") || e.getMessage().contains("индекс")
|
|
? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST;
|
|
return ResponseEntity.status(status).body(error);
|
|
} catch (IOException e) {
|
|
ErrorResponse error = new ErrorResponse();
|
|
error.setStatus("error");
|
|
error.setMessage("Ошибка разбора JSON");
|
|
return createJsonResponse(error, HttpStatus.OK);
|
|
}
|
|
}
|
|
} |