66import com .journal .journal .services .JournalEntryService ;
77import org .bson .types .ObjectId ;
88import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .http .HttpStatus ;
10+ import org .springframework .http .ResponseEntity ;
911import org .springframework .web .bind .annotation .*;
1012
1113import java .time .LocalDateTime ;
@@ -23,38 +25,54 @@ public class JournalEntryControllerV2 {
2325
2426 //@GetMapping("/feed")
2527 @ GetMapping
26- public List <JournalEntry > getAll () {
27- return journalEntryService .getAll ();
28+ public ResponseEntity <?> getAll () {
29+ List <JournalEntry > all = journalEntryService .getAll ();
30+ if (all !=null && !all .isEmpty ())
31+ {
32+ return new ResponseEntity <>(all , HttpStatus .OK );
33+ }
34+ return new ResponseEntity <>(HttpStatus .NOT_FOUND );
2835 }
2936
3037 @ PostMapping
31- public JournalEntry createEntry (@ RequestBody JournalEntry userEntry ) {
32- userEntry .setDate (LocalDateTime .now ());
33- journalEntryService .saveEntry (userEntry );
34- return userEntry ;
38+ public ResponseEntity <JournalEntry > createEntry (@ RequestBody JournalEntry userEntry ) {
39+ try {
40+ userEntry .setDate (LocalDateTime .now ());
41+ journalEntryService .saveEntry (userEntry );
42+ return new ResponseEntity <>(userEntry , HttpStatus .CREATED );
43+ } catch (Exception e )
44+ {
45+ return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
46+ }
3547 }
3648
3749 @ GetMapping ("id/{myId}" )
38- public JournalEntry getEntryById (@ PathVariable ObjectId myId ) {
39- return journalEntryService .findById (myId ).orElse (null );
50+ public ResponseEntity <JournalEntry > getEntryById (@ PathVariable ObjectId myId ) {
51+ Optional <JournalEntry > journalEntry = journalEntryService .findById (myId );
52+ if (journalEntry .isPresent ())
53+ {
54+ return new ResponseEntity <>(journalEntry .get (), HttpStatus .OK );
55+ }
56+ return new ResponseEntity <>(HttpStatus .NOT_FOUND );
4057
4158 }
4259
4360 @ DeleteMapping ("id/{myId}" )
44- public boolean deleteEntryById (@ PathVariable ObjectId myId ) {
61+ public ResponseEntity <?> deleteEntryById (@ PathVariable ObjectId myId ) {
4562 journalEntryService .deleteById (myId );
46- return true ;
63+ return new ResponseEntity <>( HttpStatus . NO_CONTENT ) ;
4764 }
4865
4966 @ PutMapping ("id/{id}" )
50- private JournalEntry updateEntryById (@ PathVariable ObjectId id , @ RequestBody JournalEntry newEntry ) {
67+ private ResponseEntity < JournalEntry > updateEntryById (@ PathVariable ObjectId id , @ RequestBody JournalEntry newEntry ) {
5168 JournalEntry oldEntry = journalEntryService .findById (id ).orElse (null );
5269 if (oldEntry !=null )
5370 {
5471 oldEntry .setTitle (newEntry .getTitle ()!=null && !newEntry .getTitle ().isEmpty () ? newEntry .getTitle () : oldEntry .getTitle ());
5572 oldEntry .setContent (newEntry .getContent ()!=null && !newEntry .getTitle ().isEmpty () ? newEntry .getContent () : oldEntry .getContent ());
73+ journalEntryService .saveEntry (oldEntry );
74+ return new ResponseEntity <>(oldEntry , HttpStatus .OK );
5675 }
57- journalEntryService .saveEntry (oldEntry );
58- return oldEntry ;
76+ return new ResponseEntity <>(HttpStatus .NOT_FOUND );
5977 }
6078}
0 commit comments