2025-12-10 12:17:08 +07:00
|
|
|
package ru.soune.no_copy.handler;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2025-12-10 15:27:14 +07:00
|
|
|
import ru.soune.no_copy.dto.*;
|
|
|
|
|
import ru.soune.no_copy.entity.AuthToken;
|
2025-12-10 12:17:08 +07:00
|
|
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
|
|
|
|
import ru.soune.no_copy.repository.UserRepository;
|
|
|
|
|
import ru.soune.no_copy.service.AuthService;
|
|
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
import java.util.Arrays;
|
2025-12-10 12:17:08 +07:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class LoginRequestHandler implements RequestHandler {
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
private final AuthService authService;
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-12-10 15:27:14 +07:00
|
|
|
public BaseResponse handle(BaseRequest request) {
|
2025-12-10 12:17:08 +07:00
|
|
|
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
|
|
|
|
|
|
|
|
|
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
2025-12-10 15:27:14 +07:00
|
|
|
LoginAnswer loginAnswer = new LoginAnswer();
|
|
|
|
|
loginAnswer.setFieldErrors(Arrays.asList(Map.of("email", loginRequest.getEmail())));
|
|
|
|
|
|
2025-12-10 12:17:08 +07:00
|
|
|
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
2025-12-10 15:27:14 +07:00
|
|
|
new BaseResponse(request.getMsgId(), MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription(), loginAnswer));
|
2025-12-10 12:17:08 +07:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
AuthToken authToken = authService.login(loginRequest);
|
2025-12-10 12:17:08 +07:00
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
LoginAnswer loginAnswer = new LoginAnswer();
|
|
|
|
|
loginAnswer.setToken(authToken.getToken());
|
2025-12-10 12:17:08 +07:00
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
|
|
|
|
MessageCode.SUCCESS.getDescription(), loginAnswer);
|
2025-12-10 12:17:08 +07:00
|
|
|
}
|
|
|
|
|
}
|