116 lines
4.5 KiB
Java
116 lines
4.5 KiB
Java
package ru.soune.no_copy.controller;
|
|||
|
|
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import jakarta.validation.Valid;
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.validation.BeanPropertyBindingResult;
|
||
|
|
import org.springframework.validation.BindingResult;
|
||
|
|
import org.springframework.validation.FieldError;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
import ru.soune.no_copy.dto.BaseRequest;
|
||
|
|
import ru.soune.no_copy.dto.MessageCode;
|
||
|
|
import ru.soune.no_copy.dto.RegRequest;
|
||
|
|
import ru.soune.no_copy.entity.AuthToken;
|
||
|
|
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
||
|
|
import ru.soune.no_copy.handler.RegRequestValidator;
|
||
|
|
import ru.soune.no_copy.repository.UserRepository;
|
||
|
|
import ru.soune.no_copy.service.AuthService;
|
||
|
|
|
||
|
|
import java.io.Serializable;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Objects;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api")
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class ApiController {
|
||
|
|
|
||
|
|
private final ObjectMapper objectMapper;
|
||
|
|
|
||
|
|
private final UserRepository userRepository;
|
||
|
|
|
||
|
|
private final RegRequestValidator regRequestValidator;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private AuthService authService;
|
||
|
|
|
||
|
|
@PostMapping("/v{version}/data")
|
||
|
|
public ResponseEntity<?> handlePostRequest(
|
||
|
|
@RequestBody BaseRequest request,
|
||
|
|
@PathVariable("version") int version) {
|
||
|
|
Integer msgId = request.getMsgId();
|
||
|
|
Map<String, ? extends Serializable> response = new HashMap<>();
|
||
|
|
|
||
|
|
switch (msgId) {
|
||
|
|
case 20002:
|
||
|
|
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||
|
|
|
||
|
|
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
||
|
|
throw new UserAlreadyExistsException("User already exists with email: " + regRequest.getEmail(),
|
||
|
|
msgId, MessageCode.REG_EMAIL_EXISTS.getCode(), MessageCode.REG_EMAIL_EXISTS.getDescription());
|
||
|
|
}
|
||
|
|
|
||
|
|
BindingResult bindingResult = new BeanPropertyBindingResult(regRequest, "regRequest");
|
||
|
|
|
||
|
|
regRequestValidator.validate(regRequest, bindingResult);
|
||
|
|
|
||
|
|
if (bindingResult.hasErrors()) {
|
||
|
|
return createValidationErrorResponse(bindingResult, msgId);
|
||
|
|
} else {
|
||
|
|
authService.register(regRequest);
|
||
|
|
|
||
|
|
response = Map.of(
|
||
|
|
"msg_id", msgId,
|
||
|
|
"message_code", MessageCode.SUCCESS.getCode(),
|
||
|
|
"message_desc", MessageCode.SUCCESS.getDescription());
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
return ResponseEntity.ok().body(Map.of(
|
||
|
|
"msg_id", msgId,
|
||
|
|
"message_code", MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
||
|
|
"message_desc", MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
||
|
|
}
|
||
|
|
|
||
|
|
return ResponseEntity.status(MessageCode.SUCCESS.getCode()).body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
private ResponseEntity<Map<String, Object>> createValidationErrorResponse(
|
||
|
|
BindingResult bindingResult, Integer msgId) {
|
||
|
|
|
||
|
|
Map<String, Object> response = new HashMap<>();
|
||
|
|
response.put("msg_id", msgId);
|
||
|
|
response.put("message_code", MessageCode.INVALID_FIELD.getCode());
|
||
|
|
response.put("message_desc", MessageCode.INVALID_FIELD.getDescription());
|
||
|
|
|
||
|
|
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
||
|
|
.stream()
|
||
|
|
.map(this::createErrorDetail)
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
response.put("field_errors", fieldErrors);
|
||
|
|
|
||
|
|
return ResponseEntity.badRequest().body(response);
|
||
|
|
}
|
||
|
|
|
||
|
|
private Map<String, String> createErrorDetail(FieldError fieldError) {
|
||
|
|
Map<String, String> errorDetail = new HashMap<>();
|
||
|
|
errorDetail.put("field", fieldError.getField());
|
||
|
|
errorDetail.put("code", fieldError.getCode() != null ? fieldError.getCode() : "VALIDATION_ERROR");
|
||
|
|
errorDetail.put("message", fieldError.getDefaultMessage());
|
||
|
|
|
||
|
|
if (fieldError.getRejectedValue() != null &&
|
||
|
|
!fieldError.getField().toLowerCase().contains("password")) {
|
||
|
|
errorDetail.put("rejected_value", fieldError.getRejectedValue().toString());
|
||
|
|
}
|
||
|
|
|
||
|
|
return errorDetail;
|
||
|
|
}
|
||
|
|
}
|