This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package ru.soune.no_copy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.no_copy.dto.BaseRequest;
|
||||
import ru.soune.no_copy.dto.LoginRequest;
|
||||
import ru.soune.no_copy.dto.MessageCode;
|
||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||
import ru.soune.no_copy.repository.UserRepository;
|
||||
import ru.soune.no_copy.service.AuthService;
|
||||
|
||||
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
|
||||
public Map<String, Object> handle(BaseRequest request) {
|
||||
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
||||
|
||||
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
||||
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
||||
request.getMsgId(), MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription());
|
||||
}
|
||||
|
||||
authService.login(loginRequest);
|
||||
|
||||
return fillResponseInfo(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user