This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package ru.soune.no_copy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindingResult;
|
||||
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.exception.NotValidFieldException;
|
||||
import ru.soune.no_copy.exception.ValidationException;
|
||||
import ru.soune.no_copy.repository.UserRepository;
|
||||
import ru.soune.no_copy.service.AuthService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class RegRequestHandler implements RequestHandler {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final RegRequestValidator regRequestValidator;
|
||||
private final AuthService authService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> handle(BaseRequest request) throws ValidationException {
|
||||
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||||
|
||||
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
||||
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
|
||||
request.getMsgId(), MessageCode.REG_EMAIL_EXISTS.getCode(),
|
||||
MessageCode.REG_EMAIL_EXISTS.getDescription());
|
||||
}
|
||||
|
||||
BindingResult bindingResult = new BeanPropertyBindingResult(regRequest, "regRequest");
|
||||
regRequestValidator.validate(regRequest, bindingResult);
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
throw new ValidationException(bindingResult, request.getMsgId());
|
||||
}
|
||||
|
||||
authService.register(regRequest);
|
||||
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