switched to constructor injection
This commit is contained in:
parent
53d89dc36f
commit
1a15743dcd
@ -17,7 +17,6 @@ import org.ros.chatto.error.ErrorModel;
|
|||||||
import org.ros.chatto.error.ErrorResponse;
|
import org.ros.chatto.error.ErrorResponse;
|
||||||
import org.ros.chatto.service.ChatService;
|
import org.ros.chatto.service.ChatService;
|
||||||
import org.ros.chatto.service.UserService;
|
import org.ros.chatto.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
@ -27,84 +26,59 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/chat")
|
@RequestMapping("/api/chat")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ChatMessageController {
|
public class ChatMessageController {
|
||||||
@Autowired
|
|
||||||
private ChatService chatService;
|
|
||||||
|
|
||||||
@Autowired
|
private final ChatService chatService;
|
||||||
private UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@PostMapping(value = "/post/message", consumes = { "application/json" })
|
@PostMapping(value = "/post/message", consumes = { "application/json" })
|
||||||
@ResponseBody
|
public ResponseEntity<?> newMessage(@RequestBody @Valid final ChatMessageDTO chatMessageDTO,
|
||||||
public ResponseEntity<?> newMessage(@RequestBody @Valid ChatMessageDTO chatMessageDTO, BindingResult bindingResult,
|
final BindingResult bindingResult, final Principal principal) {
|
||||||
Principal principal) {
|
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
|
return new ResponseEntity<ErrorResponse>(handleValidationErrors(bindingResult), HttpStatus.BAD_REQUEST);
|
||||||
// return new ResponseEntity<List<FieldError>>(bindingResult.getFieldErrors(),HttpStatus.BAD_REQUEST);
|
|
||||||
return new ResponseEntity<ErrorResponse>(handleException(bindingResult), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
}
|
||||||
MessageCipherDTO messageCipher = chatMessageDTO.getMessageCipher();
|
final MessageCipherDTO messageCipher = chatMessageDTO.getMessageCipher();
|
||||||
String fromUser = principal.getName();
|
final String fromUser = principal.getName();
|
||||||
String toUser = chatMessageDTO.getToUser();
|
final String toUser = chatMessageDTO.getToUser();
|
||||||
System.out.println("Message cipher = " + messageCipher);
|
System.out.println("Message cipher = " + messageCipher);
|
||||||
chatMessageDTO = chatService.saveNewMessage(fromUser, toUser, messageCipher);
|
final ChatMessageDTO chatMessageDTOSaved = chatService.createMessage(fromUser, toUser, messageCipher);
|
||||||
return new ResponseEntity<ChatMessageDTO>(chatMessageDTO, HttpStatus.CREATED);
|
return new ResponseEntity<ChatMessageDTO>(chatMessageDTOSaved, HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private ErrorResponse handleValidationErrors(final BindingResult bindingResult) {
|
||||||
* Method that check against {@code @Valid} Objects passed to controller
|
|
||||||
* endpoints
|
|
||||||
*
|
|
||||||
* @param exception
|
|
||||||
* @return a {@code ErrorResponse}
|
|
||||||
* @see com.aroussi.util.validation.ErrorResponse
|
|
||||||
*/
|
|
||||||
// @ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
||||||
// @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
// public ErrorResponse handleException(MethodArgumentNotValidException exception) {
|
|
||||||
//
|
|
||||||
// List<ErrorModel> errorMessages = exception.getBindingResult().getFieldErrors().stream()
|
|
||||||
// .map(err -> new ErrorModel(err.getField(), err.getRejectedValue(), err.getDefaultMessage())).distinct()
|
|
||||||
// .collect(Collectors.toList());
|
|
||||||
// return ErrorResponse.builder().errorMessage(errorMessages).build();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
||||||
// @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public ErrorResponse handleException(BindingResult bindingResult) {
|
|
||||||
|
|
||||||
List<ErrorModel> errorMessages = bindingResult.getFieldErrors().stream()
|
final List<ErrorModel> errorMessages = bindingResult.getFieldErrors().stream()
|
||||||
.map(err -> new ErrorModel(err.getField(), err.getRejectedValue(), err.getDefaultMessage())).distinct()
|
.map(err -> new ErrorModel(err.getField(), err.getRejectedValue(), err.getDefaultMessage())).distinct()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return ErrorResponse.builder().errorMessage(errorMessages).build();
|
return ErrorResponse.builder().errorMessage(errorMessages).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/get/messages/{userName}")
|
@GetMapping(value = "/get/messages/{userName}")
|
||||||
@ResponseBody
|
public List<ChatMessageDTO> getAllMessages(@PathVariable final String userName, final Principal principal) {
|
||||||
public List<ChatMessageDTO> sendAllMessages(@PathVariable String userName, Principal principal) {
|
final List<ChatMessageDTO> chatMessageDTOs = chatService.getAllMessages(principal.getName(), userName);
|
||||||
List<ChatMessageDTO> chatMessageDTOs = chatService.getAllMessages(principal.getName(), userName);
|
|
||||||
return chatMessageDTOs;
|
return chatMessageDTOs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/get/messages/{userName}", params = { "page", "size" })
|
@GetMapping(value = "/get/messages/{userName}", params = { "page", "size" })
|
||||||
@ResponseBody
|
public List<ChatMessageDTO> findPaginated(@RequestParam("page") final int page,
|
||||||
public List<ChatMessageDTO> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size,
|
@RequestParam("size") final int size, @PathVariable final String userName, final Principal principal) {
|
||||||
@PathVariable String userName, Principal principal) {
|
final List<ChatMessageDTO> chatMessageDTOs = chatService.getMessagePage(principal.getName(), userName, page,
|
||||||
List<ChatMessageDTO> chatMessageDTOs = chatService.getMessagePage(principal.getName(), userName, page, size);
|
size);
|
||||||
return chatMessageDTOs;
|
return chatMessageDTOs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
|
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
|
||||||
@ResponseBody
|
public List<ChatMessageDTO> sendNewMessages(@PathVariable final String userName,
|
||||||
public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName, @PathVariable String lastMessageTime,
|
@PathVariable final String lastMessageTime, final Principal principal) {
|
||||||
Principal principal) {
|
|
||||||
System.out.println("Last message time = " + lastMessageTime);
|
System.out.println("Last message time = " + lastMessageTime);
|
||||||
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
|
final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
|
||||||
// date/time
|
// date/time
|
||||||
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||||
// offset (hh:mm - "+00:00" when it's zero)
|
// offset (hh:mm - "+00:00" when it's zero)
|
||||||
@ -115,19 +89,19 @@ public class ChatMessageController {
|
|||||||
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
|
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
|
||||||
// create formatter
|
// create formatter
|
||||||
.toFormatter();
|
.toFormatter();
|
||||||
Date date = Date.from(OffsetDateTime.parse(lastMessageTime, formatter).toInstant());
|
final Date date = Date.from(OffsetDateTime.parse(lastMessageTime, formatter).toInstant());
|
||||||
|
|
||||||
List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(principal.getName(), userName, date);
|
final List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(principal.getName(), userName, date);
|
||||||
return chatMessageDTOs;
|
return chatMessageDTOs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/get/users")
|
@GetMapping("/get/users")
|
||||||
public List<String> getAllOtherUsers(Principal principal) {
|
public List<String> getAllOtherUsers(final Principal principal) {
|
||||||
return userService.findAllOtherUsers(principal.getName());
|
return userService.findAllOtherUsers(principal.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/get/active-users")
|
@GetMapping("/get/active-users")
|
||||||
public List<ActiveUserDTO> getAllOtherActiveUsers(Principal principal) {
|
public List<ActiveUserDTO> getAllOtherActiveUsers(final Principal principal) {
|
||||||
return userService.getOtherActiveUsers(principal.getName());
|
return userService.getOtherActiveUsers(principal.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,11 +111,13 @@ public class ChatMessageController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//public ResponseEntity<List<ChatMessage>> getMessages(@PathVariable String userName, Principal principal) {
|
// public ResponseEntity<List<ChatMessage>> getMessages(@PathVariable String
|
||||||
////List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(principal.getName(), userName);
|
// userName, Principal principal) {
|
||||||
|
//// List<ChatMessage> chatMessages =
|
||||||
|
// chatMessageRepository.getAllMessages(principal.getName(), userName);
|
||||||
//
|
//
|
||||||
//// return posts.stream()
|
//// return posts.stream()
|
||||||
//// .map(post -> convertToDto(post))
|
//// .map(post -> convertToDto(post))
|
||||||
//// .collect(Collectors.toList());
|
//// .collect(Collectors.toList());
|
||||||
//return new ResponseEntity<List<ChatMessage>>(chatMessages, HttpStatus.OK);
|
// return new ResponseEntity<List<ChatMessage>>(chatMessages, HttpStatus.OK);
|
||||||
//}
|
// }
|
||||||
|
@ -3,25 +3,17 @@ package org.ros.chatto.service;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
|
||||||
|
|
||||||
import org.ros.chatto.dto.ChatMessageDTO;
|
import org.ros.chatto.dto.ChatMessageDTO;
|
||||||
import org.ros.chatto.dto.MessageCipherDTO;
|
import org.ros.chatto.dto.MessageCipherDTO;
|
||||||
import org.ros.chatto.dto.ReencryptionDTO;
|
import org.ros.chatto.dto.ReencryptionDTO;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
|
||||||
public interface ChatService {
|
public interface ChatService {
|
||||||
public @Valid ChatMessageDTO saveNewMessage(String fromUser, String toUser, MessageCipherDTO messageCipherDTO);
|
public ChatMessageDTO createMessage(String fromUser, String toUser, MessageCipherDTO messageCipherDTO);
|
||||||
|
|
||||||
public List<ChatMessageDTO> getAllMessages(String fromUser, String toUser);
|
public List<ChatMessageDTO> getAllMessages(String fromUser, String toUser);
|
||||||
|
|
||||||
public List<ChatMessageDTO> getMessagePage(String fromUser, String toUser, int page, int size);
|
public List<ChatMessageDTO> getMessagePage(String fromUser, String toUser, int page, int size);
|
||||||
|
|
||||||
public List<ChatMessageDTO> getNewMessages(String fromUser, String toUser, Date lastMessageTime);
|
public List<ChatMessageDTO> getNewMessages(String fromUser, String toUser, Date lastMessageTime);
|
||||||
|
|
||||||
public void reencryptMessages(List<ReencryptionDTO> reencryptionDTOs);
|
public void reencryptMessages(List<ReencryptionDTO> reencryptionDTOs);
|
||||||
|
|
||||||
public List<ReencryptionDTO> getAllMessagesForReencryption(String fromUser, String toUser);
|
public List<ReencryptionDTO> getAllMessagesForReencryption(String fromUser, String toUser);
|
||||||
|
|
||||||
public List<ChatMessageDTO> getAllMessages(String name, String userName, PageRequest pageRequest);
|
public List<ChatMessageDTO> getAllMessages(String name, String userName, PageRequest pageRequest);
|
||||||
}
|
}
|
||||||
|
@ -13,25 +13,23 @@ import org.ros.chatto.model.MessageCipher;
|
|||||||
import org.ros.chatto.repository.ChatMessageRepository;
|
import org.ros.chatto.repository.ChatMessageRepository;
|
||||||
import org.ros.chatto.repository.MessageCipherRepository;
|
import org.ros.chatto.repository.MessageCipherRepository;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ChatServiceImpl implements ChatService {
|
public class ChatServiceImpl implements ChatService {
|
||||||
|
|
||||||
@Autowired
|
private final UserRepository userRepository;
|
||||||
UserRepository userRepository;
|
private final MessageCipherRepository messageCipherRepository;
|
||||||
@Autowired
|
private final ChatMessageRepository chatMessageRepository;
|
||||||
MessageCipherRepository messageCipherRepository;
|
private final MyConversionService myConversionService;
|
||||||
@Autowired
|
|
||||||
ChatMessageRepository chatMessageRepository;
|
|
||||||
@Autowired
|
|
||||||
MyConversionService myConversionService;
|
|
||||||
|
|
||||||
public ChatMessageDTO saveNewMessage(final String fromUserName, final String toUserName,
|
public ChatMessageDTO createMessage(final String fromUserName, final String toUserName,
|
||||||
final MessageCipherDTO messageCipherDTO) {
|
final MessageCipherDTO messageCipherDTO) {
|
||||||
|
|
||||||
MessageCipher messageCipher = myConversionService.convertToMessageCipher(messageCipherDTO);
|
MessageCipher messageCipher = myConversionService.convertToMessageCipher(messageCipherDTO);
|
||||||
|
@ -17,13 +17,15 @@ import org.ros.chatto.repository.RoleRepository;
|
|||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.ros.chatto.repository.UserRoleRepository;
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
import org.ros.chatto.repository.UserSessionRepository;
|
import org.ros.chatto.repository.UserSessionRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
@ -35,17 +37,6 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
private final UserSessionRepository userSessionRepository;
|
private final UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
public UserServiceImpl(@Autowired final UserRepository userRepository,
|
|
||||||
@Autowired final UserRoleRepository userRoleRepository, @Autowired final PasswordEncoder passwordEncoder,
|
|
||||||
@Autowired final RoleRepository roleRepository,
|
|
||||||
@Autowired final UserSessionRepository userSessionRepository) {
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
this.userRoleRepository = userRoleRepository;
|
|
||||||
this.passwordEncoder = passwordEncoder;
|
|
||||||
this.roleRepository = roleRepository;
|
|
||||||
this.userSessionRepository = userSessionRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) {
|
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) {
|
||||||
final ChatUser user = new ChatUser();
|
final ChatUser user = new ChatUser();
|
||||||
|
Loading…
Reference in New Issue
Block a user