Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.groupfour.chatapp.chatapp.message;

import com.groupfour.chatapp.chatapp.exceptions.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,18 +20,40 @@ public MessageController(MessageService messageService) {
public ResponseEntity<Message> createMessage(@RequestBody Message message) {
return new ResponseEntity<>(messageService.create(message), HttpStatus.CREATED);
}

@GetMapping("/messages/{messageId}")
public ResponseEntity<Message> getMessage(@PathVariable Long messageId) {
return new ResponseEntity<>(messageService.getMessageById(messageId), HttpStatus.OK);
try {
verifyMessage(messageId);
return new ResponseEntity<>(messageService.getMessageById(messageId), HttpStatus.OK);
} catch (ResourceNotFoundException ex) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@PutMapping("/messages/{messageId}")
public ResponseEntity<Message> editMessage(@PathVariable Long messageId, @RequestBody Message message) {
return new ResponseEntity<>(messageService.updateMessageBody(messageId, message), HttpStatus.OK);
try {
verifyMessage(messageId);
return new ResponseEntity<>(messageService.updateMessageBody(messageId, message), HttpStatus.OK);
} catch (ResourceNotFoundException ex) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@DeleteMapping("messages/{messageId")
public ResponseEntity<Boolean> deleteMessage(@PathVariable Long messageId) {
return new ResponseEntity<>(messageService.deleteMessageById(messageId), HttpStatus.OK);
try {
verifyMessage(messageId);
return new ResponseEntity<>(messageService.deleteMessageById(messageId), HttpStatus.OK);
} catch (ResourceNotFoundException ex) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

private void verifyMessage(Long messageId) {
if (!messageService.messageExists(messageId)) {
throw new ResourceNotFoundException("Message " + messageId + " not found.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Repository
public interface MessageRepository extends CrudRepository<Message, Long> {

Message findByUser();
//Message findByUser();


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.groupfour.chatapp.chatapp.message;

import com.groupfour.chatapp.chatapp.message.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
Expand Down Expand Up @@ -36,4 +33,12 @@ public Boolean deleteMessageById(Long messageId) {
messageRepository.deleteById(messageId);
return true;
}

public Boolean messageExists(Long messageId) {
if(messageRepository.existsById(messageId))
return true;
else
return false;
}

}