package org.ros.chatto.controller; import java.security.Principal; import java.time.Instant; import java.util.List; import java.util.Optional; import javax.validation.Valid; import com.spencerwi.either.Result; import org.ros.chatto.dto.AdminUserDTO; import org.ros.chatto.dto.ChatMessageDTO; import org.ros.chatto.dto.ReencryptionDTO; import org.ros.chatto.service.AdminService; import org.ros.chatto.service.ChatService; import org.ros.chatto.service.UserDTOSpec; import org.ros.chatto.service.UserService; import org.springframework.http.HttpStatus; 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.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 lombok.RequiredArgsConstructor; @RestController @RequestMapping("/api/admin") @RequiredArgsConstructor public class AdminRESTController { private final ChatService chatService; private final UserService userService; private final AdminService adminService; @PostMapping(value = "/post/re-encrypt", consumes = { "application/json" }) public ResponseEntity reencryptMessages( @RequestBody @Valid List reencryptionDTOs, Principal principal) { if (reencryptionDTOs.size() > 0) { chatService.reencryptMessages(reencryptionDTOs); } return new ResponseEntity(HttpStatus.OK); } @GetMapping(value = "/get/messages/{userName}") public List sendAllMessages(@PathVariable String userName, Principal principal) { List reencryptionDTOs = chatService .getAllMessagesForReencryption(principal.getName(), userName); return reencryptionDTOs; } @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}") public List sendNewMessages(@PathVariable String userName, @PathVariable Instant lastMessageTime, Principal principal) { List chatMessageDTOs = chatService .getNewMessages(principal.getName(), userName, lastMessageTime); return chatMessageDTOs; } @GetMapping("/get/users") public List getUsers(Principal principal, @RequestParam Optional select) { if (select.isPresent()) { switch (select.get()) { case "all": return adminService.getUsers("", UserDTOSpec.ALL_USERS); case "regular": return adminService.getUsers("", UserDTOSpec.REGULAR_USERS); } } return adminService.getUsers(principal.getName(), UserDTOSpec.OTHER_USERS); } @DeleteMapping("/delete/users/{userName}") public ResponseEntity deleteUser(@PathVariable String userName) { Result res = adminService.deleteUser(userName); if (res.isOk()) { return ResponseEntity.ok() .body("Deleted User with name - " + userName); } else { return ResponseEntity.badRequest().body( "An error occured while trying to delete user with name - " + userName); } } }