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;
import java.security.Principal;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.Instant;
import java.util.Date;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ -53,24 +50,9 @@ public class AdminRESTController {
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName,
@PathVariable String lastMessageTime, Principal principal) {
System.out.println("Last message time = " + lastMessageTime);
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
// 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);
@PathVariable Instant lastMessageTime, Principal principal) {
List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(
principal.getName(), userName, Date.from(lastMessageTime));
return chatMessageDTOs;
}
@ -79,14 +61,3 @@ public class AdminRESTController {
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;
import java.security.Principal;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
@ -34,64 +35,67 @@ import lombok.RequiredArgsConstructor;
@RequestMapping("/api/chat")
@RequiredArgsConstructor
public class ChatMessageController {
private final ChatService chatService;
private final UserService userService;
@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) {
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 toUser = chatMessageDTO.getToUser();
System.out.println("Message cipher = " + messageCipher);
final ChatMessageDTO chatMessageDTOSaved = chatService.createMessage(fromUser, toUser, messageCipher);
return new ResponseEntity<ChatMessageDTO>(chatMessageDTOSaved, HttpStatus.CREATED);
final ChatMessageDTO chatMessageDTOSaved = chatService
.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()
.map(err -> new ErrorModel(err.getField(), err.getRejectedValue(), err.getDefaultMessage())).distinct()
.collect(Collectors.toList());
final List<ErrorModel> errorMessages = bindingResult.getFieldErrors()
.stream()
.map(err -> new ErrorModel(err.getField(),
err.getRejectedValue(), err.getDefaultMessage()))
.distinct().collect(Collectors.toList());
return ErrorResponse.builder().errorMessage(errorMessages).build();
}
@GetMapping(value = "/get/messages/{userName}")
public List<ChatMessageDTO> getAllMessages(@PathVariable final String userName, final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService.getAllMessages(principal.getName(), userName);
public List<ChatMessageDTO> getAllMessages(
@PathVariable final String userName, final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService
.getAllMessages(principal.getName(), userName);
return chatMessageDTOs;
}
@GetMapping(value = "/get/messages/{userName}", params = { "page", "size" })
public List<ChatMessageDTO> findPaginated(@RequestParam("page") final int page,
@RequestParam("size") final int size, @PathVariable final String userName, final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService.getMessagePage(principal.getName(), userName, page,
size);
public List<ChatMessageDTO> findPaginated(
@RequestParam("page") final int page,
@RequestParam("size") final int size,
@PathVariable final String userName, final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService
.getMessagePage(principal.getName(), userName, page, size);
return chatMessageDTOs;
}
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
public List<ChatMessageDTO> sendNewMessages(@PathVariable final String userName,
@PathVariable final String lastMessageTime, final Principal principal) {
System.out.println("Last message time = " + lastMessageTime);
final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
// 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();
final Date date = Date.from(OffsetDateTime.parse(lastMessageTime, formatter).toInstant());
final List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(principal.getName(), userName, date);
public List<ChatMessageDTO> sendNewMessages(
@PathVariable final String userName,
@PathVariable final Instant lastMessageTime,
final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService
.getNewMessages(principal.getName(), userName,
Date.from(lastMessageTime));
return chatMessageDTOs;
}
@ -101,7 +105,8 @@ public class ChatMessageController {
}
@GetMapping("/get/active-users")
public List<ActiveUserDTO> getAllOtherActiveUsers(final Principal principal) {
public List<ActiveUserDTO> getAllOtherActiveUsers(
final Principal principal) {
return userService.getOtherActiveUsers(principal.getName());
}
@ -110,14 +115,3 @@ public class ChatMessageController {
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);
// }