|
| 1 | +package com.groupfour.chatapp.chatapp.controllers; |
| 2 | + |
| 3 | +import com.groupfour.chatapp.chatapp.dataprojections.ChatDTO; |
| 4 | +import com.groupfour.chatapp.chatapp.repositories.ChatRepository; |
| 5 | +import com.groupfour.chatapp.chatapp.services.ChatService; |
| 6 | +import com.groupfour.chatapp.chatapp.exceptions.ResourceNotFoundException; |
| 7 | +import com.groupfour.chatapp.chatapp.models.Chat; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.*; |
| 12 | + |
| 13 | +@CrossOrigin |
| 14 | +@RestController |
| 15 | +public class ChatController { |
| 16 | + |
| 17 | + private ChatService chatService; |
| 18 | + private ChatRepository chatRepository; |
| 19 | + |
| 20 | + @Autowired |
| 21 | + public ChatController(ChatService chatService, ChatRepository chatRepository) { |
| 22 | + this.chatService = chatService; |
| 23 | + this.chatRepository = chatRepository; |
| 24 | + } |
| 25 | + |
| 26 | + @GetMapping("/chat/user/{userId}") |
| 27 | + public ResponseEntity<Iterable<ChatDTO>> getChatsByUser(@PathVariable Long userId) { |
| 28 | + try { |
| 29 | + return new ResponseEntity<>(chatService.getUserChats(userId), HttpStatus.OK); |
| 30 | + } catch (ResourceNotFoundException e) { |
| 31 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @GetMapping("/chat/{chatId}") |
| 36 | + public ResponseEntity<Chat> findChatBy(@PathVariable Long chatId) { |
| 37 | + try { |
| 38 | + verifyChatById(chatId); |
| 39 | + return new ResponseEntity<>(chatService.getChatById(chatId), HttpStatus.OK); |
| 40 | + } catch (ResourceNotFoundException ex) { |
| 41 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @PostMapping("/chat/{adminId}") |
| 46 | + public ResponseEntity<ChatDTO> createNewChat(@RequestBody Chat chatName, @PathVariable Long adminId) { |
| 47 | + Chat chat = chatService.createNewChat(chatName, adminId); |
| 48 | + return new ResponseEntity<>(chatRepository.findByChatId(chat.getChatId()), HttpStatus.CREATED); |
| 49 | + } |
| 50 | + |
| 51 | + @PutMapping("/chat/{chatId}/user/{username}") |
| 52 | + public ResponseEntity<ChatDTO> addUserToChat(@PathVariable Long chatId, @PathVariable String username) { |
| 53 | + return new ResponseEntity<>(chatService.addUserToChat(chatId, username), HttpStatus.OK); |
| 54 | + } |
| 55 | + |
| 56 | + @PatchMapping("/chat/{chatId}") |
| 57 | + public ResponseEntity<Chat> updateChatName(@PathVariable Long chatId, @RequestBody String newChatName) { |
| 58 | + try { |
| 59 | + verifyChatById(chatId); |
| 60 | + chatService.updateChatName(chatId, newChatName); |
| 61 | + return new ResponseEntity<>(HttpStatus.OK); |
| 62 | + } catch (ResourceNotFoundException ex) { |
| 63 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @PatchMapping("/chat/{chatId}/1") |
| 68 | + public ResponseEntity<Chat> updateChatAdmin(@PathVariable Long chatId, @RequestBody Long newAdminId) { |
| 69 | + try { |
| 70 | + verifyChatById(chatId); |
| 71 | + chatService.updateChatAdmin(chatId, newAdminId); |
| 72 | + return new ResponseEntity<>(HttpStatus.OK); |
| 73 | + } catch (ResourceNotFoundException ex) { |
| 74 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @DeleteMapping("/chat/{chatId}") |
| 79 | + public ResponseEntity<Boolean> deleteChat(@PathVariable Long chatId) { |
| 80 | + try { |
| 81 | + verifyChatById(chatId); |
| 82 | + return new ResponseEntity<>(chatService.deleteChatByChatId(chatId), HttpStatus.OK); |
| 83 | + } catch (ResourceNotFoundException ex) { |
| 84 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + public void verifyChatById(Long chatId) { |
| 90 | + if (chatRepository.existsById(chatId)) { |
| 91 | + throw new ResourceNotFoundException("Department " + chatId + " not found."); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments