2025-12-10 02:37:31 +07:00
|
|
|
package ru.soune.no_copy.controller;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2025-12-10 04:51:07 +07:00
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
2025-12-10 02:37:31 +07:00
|
|
|
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;
|
2025-12-10 04:51:07 +07:00
|
|
|
import ru.soune.no_copy.dto.LoginRequest;
|
2025-12-10 02:37:31 +07:00
|
|
|
import ru.soune.no_copy.dto.MessageCode;
|
|
|
|
|
import ru.soune.no_copy.dto.RegRequest;
|
2025-12-10 04:51:07 +07:00
|
|
|
import ru.soune.no_copy.entity.AuthToken;
|
|
|
|
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
2025-12-10 02:37:31 +07:00
|
|
|
import ru.soune.no_copy.handler.RegRequestValidator;
|
|
|
|
|
import ru.soune.no_copy.repository.UserRepository;
|
|
|
|
|
import ru.soune.no_copy.service.AuthService;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
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;
|
|
|
|
|
|
2025-12-10 04:51:07 +07:00
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
2025-12-10 02:37:31 +07:00
|
|
|
@Autowired
|
|
|
|
|
private AuthService authService;
|
|
|
|
|
|
|
|
|
|
@PostMapping("/v{version}/data")
|
2025-12-10 04:22:09 +07:00
|
|
|
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request, @PathVariable("version") int version) {
|
2025-12-10 02:37:31 +07:00
|
|
|
Integer msgId = request.getMsgId();
|
2025-12-10 04:22:09 +07:00
|
|
|
Map<String, Object> response;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
|
|
|
|
switch (msgId) {
|
|
|
|
|
case 20002:
|
|
|
|
|
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
|
|
|
|
|
|
|
|
|
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
2025-12-10 04:51:07 +07:00
|
|
|
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
|
2025-12-10 02:37:31 +07:00
|
|
|
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);
|
2025-12-10 04:22:09 +07:00
|
|
|
response = fillResponseInfo(msgId, MessageCode.SUCCESS.getCode(),
|
|
|
|
|
MessageCode.SUCCESS.getDescription());
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|
|
|
|
|
break;
|
2025-12-10 04:22:09 +07:00
|
|
|
case 20001:
|
2025-12-10 04:51:07 +07:00
|
|
|
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
|
|
|
|
|
|
|
|
|
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
|
|
|
|
|
|
|
|
|
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
|
|
|
|
msgId, MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authService.login(loginRequest);
|
|
|
|
|
|
|
|
|
|
response = fillResponseInfo(msgId, MessageCode.SUCCESS.getCode(),
|
|
|
|
|
MessageCode.SUCCESS.getDescription());
|
|
|
|
|
break;
|
2025-12-10 02:37:31 +07:00
|
|
|
default:
|
2025-12-10 04:22:09 +07:00
|
|
|
return ResponseEntity.ok().body(fillResponseInfo(msgId, MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 04:22:09 +07:00
|
|
|
return ResponseEntity.ok().body(response);
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResponseEntity<Map<String, Object>> createValidationErrorResponse(
|
|
|
|
|
BindingResult bindingResult, Integer msgId) {
|
|
|
|
|
|
2025-12-10 04:22:09 +07:00
|
|
|
Map<String, Object> response = fillResponseInfo(msgId, MessageCode.INVALID_FIELD.getCode(),
|
|
|
|
|
MessageCode.INVALID_FIELD.getDescription());
|
2025-12-10 02:37:31 +07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2025-12-10 04:22:09 +07:00
|
|
|
|
|
|
|
|
private Map<String, Object> fillResponseInfo(Integer msgId, Integer messageCode, String messageDesc) {
|
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
|
|
|
response.put("msg_id", msgId);
|
|
|
|
|
response.put("message_code", messageCode);
|
|
|
|
|
response.put("message_desc", messageDesc);
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|