From 64b4b7e40473b40db9847c78a255e32fd7cdc6e5 Mon Sep 17 00:00:00 2001 From: Zhongheng Liu Date: Wed, 17 Jan 2024 23:45:58 +0200 Subject: Added UserRepository if exists by UserName handler added corresponding typedefs to User entity class according to commit 5ce5f9 on epq-web added combined user getMapping for id and name-identified User getters --- .../imsonmia/epqapi/controller/UserController.java | 68 ++++++++++++++++++---- 1 file changed, 56 insertions(+), 12 deletions(-) (limited to 'src/main/java/me/imsonmia/epqapi/controller/UserController.java') diff --git a/src/main/java/me/imsonmia/epqapi/controller/UserController.java b/src/main/java/me/imsonmia/epqapi/controller/UserController.java index cd35735..ffdfdf8 100644 --- a/src/main/java/me/imsonmia/epqapi/controller/UserController.java +++ b/src/main/java/me/imsonmia/epqapi/controller/UserController.java @@ -2,14 +2,20 @@ package me.imsonmia.epqapi.controller; import java.time.Instant; import java.util.ArrayList; +import java.util.Optional; + +import javax.swing.text.html.Option; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; import me.imsonmia.epqapi.model.Message; @@ -24,32 +30,70 @@ public class UserController { private UserRepository userRepository; @Autowired private MessageRepository messageRepository; - @GetMapping("/user/{id}") - public User getUserById(@PathVariable(value = "id") Long id) { - return userRepository.findById(id).get(); + + // request URL like .../user?id={number} or .../user?name={string} + @GetMapping("/user") + public ResponseEntity getUserByParam(@RequestParam(value = "id") Optional id, + @RequestParam(value = "name") Optional name) { + if (!id.isPresent()) { + if (!name.isPresent()) { + // malformed request + return ResponseEntity.badRequest().build(); + } else { + // Filter by name branch + return ResponseEntity.ok().body(userRepository.findByUserName(name.get())); + } + } else { + // get by id branch + return ResponseEntity.ok().body(userRepository.findById(id.get()).get()); + } } + @PostMapping("/user") - public User addUser( - @RequestBody - User newUser - ) { - return userRepository.save(newUser); + public ResponseEntity> addUser( + @RequestBody User newUser) { + if (userRepository.existsByUserName(newUser.getUserName())) { + return ResponseEntity.badRequest().build(); + } + userRepository.save(newUser); + return ResponseEntity.ok().build(); } + @DeleteMapping("/user/{id}") - public void deleteUser( - @PathVariable(value = "id") Long id - ) { + public boolean deleteUser( + @PathVariable(value = "id") Long id) { + // user doesn't exist + if (!userRepository.existsById(id)) { + return false; + } userRepository.deleteById(id); + return true; } + @GetMapping("/msg/{from}") public ArrayList getMessagesFromTimestamp(@PathVariable(value = "from") Long fromTimestamp) { + if (fromTimestamp < 0) { + return new ArrayList<>(); + } + ; ArrayList messages = new ArrayList<>(); Instant targetInstant = Instant.ofEpochMilli(fromTimestamp); for (Message msg : messageRepository.findAll()) { Instant t = Instant.ofEpochMilli(msg.getTimeMillis()); - if (t.isBefore(targetInstant)) {continue;} + if (t.isBefore(targetInstant)) { + continue; + } messages.add(msg); } return messages; } + + @PatchMapping("/user/{id}") + boolean changeUserProperties(@PathVariable(value = "id") Long userId, @RequestBody User newUser) { + if (!userRepository.existsById(userId)) { + return false; + } + userRepository.save(newUser); + return true; + } } -- cgit