Changed time formatter to Instant in Controllers

This commit is contained in:
Rohan Sircar 2020-05-27 16:24:40 +05:30
parent 5978eaebaa
commit a79d85f67f
2 changed files with 43 additions and 78 deletions

View File

@ -1,9 +1,7 @@
package org.ros.chatto.controller; package org.ros.chatto.controller;
import java.security.Principal; import java.security.Principal;
import java.time.OffsetDateTime; import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -21,7 +19,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@ -53,24 +50,9 @@ public class AdminRESTController {
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}") @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName, public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName,
@PathVariable String lastMessageTime, Principal principal) { @PathVariable Instant lastMessageTime, Principal principal) {
System.out.println("Last message time = " + lastMessageTime); List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(
DateTimeFormatter formatter = new DateTimeFormatterBuilder() principal.getName(), userName, Date.from(lastMessageTime));
// date/time
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// offset (hh:mm - "+00:00" when it's zero)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
// offset (hhmm - "+0000" when it's zero)
.optionalStart().appendOffset("+HHMM", "+0000").optionalEnd()
// offset (hh - "Z" when it's zero)
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
// create formatter
.toFormatter();
Date date = Date.from(
OffsetDateTime.parse(lastMessageTime, formatter).toInstant());
List<ChatMessageDTO> chatMessageDTOs = chatService
.getNewMessages(principal.getName(), userName, date);
return chatMessageDTOs; return chatMessageDTOs;
} }
@ -79,14 +61,3 @@ public class AdminRESTController {
return userService.findAllOtherUsers(principal.getName()); return userService.findAllOtherUsers(principal.getName());
} }
} }
// public ResponseEntity<List<ChatMessage>> getMessages(@PathVariable String
// userName, Principal principal) {
//// List<ChatMessage> chatMessages =
// chatMessageRepository.getAllMessages(principal.getName(), userName);
//
//// return posts.stream()
//// .map(post -> convertToDto(post))
//// .collect(Collectors.toList());
// return new ResponseEntity<List<ChatMessage>>(chatMessages, HttpStatus.OK);
// }

View File

@ -1,6 +1,7 @@
package org.ros.chatto.controller; package org.ros.chatto.controller;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
@ -39,59 +40,62 @@ public class ChatMessageController {
private final UserService userService; private final UserService userService;
@PostMapping(value = "/post/message", consumes = { "application/json" }) @PostMapping(value = "/post/message", consumes = { "application/json" })
public ResponseEntity<?> newMessage(@RequestBody @Valid final ChatMessageDTO chatMessageDTO, public ResponseEntity<?> newMessage(
@RequestBody @Valid final ChatMessageDTO chatMessageDTO,
final BindingResult bindingResult, final Principal principal) { final BindingResult bindingResult, final Principal principal) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
return new ResponseEntity<ErrorResponse>(handleValidationErrors(bindingResult), HttpStatus.BAD_REQUEST); return new ResponseEntity<ErrorResponse>(
handleValidationErrors(bindingResult),
HttpStatus.BAD_REQUEST);
} }
final MessageCipherDTO messageCipher = chatMessageDTO.getMessageCipher(); final MessageCipherDTO messageCipher = chatMessageDTO
.getMessageCipher();
final String fromUser = principal.getName(); final String fromUser = principal.getName();
final String toUser = chatMessageDTO.getToUser(); final String toUser = chatMessageDTO.getToUser();
System.out.println("Message cipher = " + messageCipher); System.out.println("Message cipher = " + messageCipher);
final ChatMessageDTO chatMessageDTOSaved = chatService.createMessage(fromUser, toUser, messageCipher); final ChatMessageDTO chatMessageDTOSaved = chatService
return new ResponseEntity<ChatMessageDTO>(chatMessageDTOSaved, HttpStatus.CREATED); .createMessage(fromUser, toUser, messageCipher);
return new ResponseEntity<ChatMessageDTO>(chatMessageDTOSaved,
HttpStatus.CREATED);
} }
private ErrorResponse handleValidationErrors(final BindingResult bindingResult) { private ErrorResponse handleValidationErrors(
final BindingResult bindingResult) {
final List<ErrorModel> errorMessages = bindingResult.getFieldErrors().stream() final List<ErrorModel> errorMessages = bindingResult.getFieldErrors()
.map(err -> new ErrorModel(err.getField(), err.getRejectedValue(), err.getDefaultMessage())).distinct() .stream()
.collect(Collectors.toList()); .map(err -> new ErrorModel(err.getField(),
err.getRejectedValue(), err.getDefaultMessage()))
.distinct().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}")
public List<ChatMessageDTO> getAllMessages(@PathVariable final String userName, final Principal principal) { public List<ChatMessageDTO> getAllMessages(
final List<ChatMessageDTO> chatMessageDTOs = chatService.getAllMessages(principal.getName(), userName); @PathVariable final String userName, final Principal principal) {
final 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" })
public List<ChatMessageDTO> findPaginated(@RequestParam("page") final int page, public List<ChatMessageDTO> findPaginated(
@RequestParam("size") final int size, @PathVariable final String userName, final Principal principal) { @RequestParam("page") final int page,
final List<ChatMessageDTO> chatMessageDTOs = chatService.getMessagePage(principal.getName(), userName, page, @RequestParam("size") final int size,
size); @PathVariable final String userName, final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService
.getMessagePage(principal.getName(), userName, page, size);
return chatMessageDTOs; return chatMessageDTOs;
} }
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}") @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
public List<ChatMessageDTO> sendNewMessages(@PathVariable final String userName, public List<ChatMessageDTO> sendNewMessages(
@PathVariable final String lastMessageTime, final Principal principal) { @PathVariable final String userName,
System.out.println("Last message time = " + lastMessageTime); @PathVariable final Instant lastMessageTime,
final DateTimeFormatter formatter = new DateTimeFormatterBuilder() final Principal principal) {
// date/time final List<ChatMessageDTO> chatMessageDTOs = chatService
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .getNewMessages(principal.getName(), userName,
// offset (hh:mm - "+00:00" when it's zero) Date.from(lastMessageTime));
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
// offset (hhmm - "+0000" when it's zero)
.optionalStart().appendOffset("+HHMM", "+0000").optionalEnd()
// offset (hh - "Z" when it's zero)
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
// create formatter
.toFormatter();
final Date date = Date.from(OffsetDateTime.parse(lastMessageTime, formatter).toInstant());
final List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(principal.getName(), userName, date);
return chatMessageDTOs; return chatMessageDTOs;
} }
@ -101,7 +105,8 @@ public class ChatMessageController {
} }
@GetMapping("/get/active-users") @GetMapping("/get/active-users")
public List<ActiveUserDTO> getAllOtherActiveUsers(final Principal principal) { public List<ActiveUserDTO> getAllOtherActiveUsers(
final Principal principal) {
return userService.getOtherActiveUsers(principal.getName()); return userService.getOtherActiveUsers(principal.getName());
} }
@ -110,14 +115,3 @@ public class ChatMessageController {
return new ResponseEntity<String>(HttpStatus.OK); return new ResponseEntity<String>(HttpStatus.OK);
} }
} }
// public ResponseEntity<List<ChatMessage>> getMessages(@PathVariable String
// userName, Principal principal) {
//// List<ChatMessage> chatMessages =
// chatMessageRepository.getAllMessages(principal.getName(), userName);
//
//// return posts.stream()
//// .map(post -> convertToDto(post))
//// .collect(Collectors.toList());
// return new ResponseEntity<List<ChatMessage>>(chatMessages, HttpStatus.OK);
// }