Skip to content

Commit a8d442d

Browse files
authored
Merge pull request #4 from Invocoder/main
backmerging
2 parents 05ce5b2 + b69a8b4 commit a8d442d

3 files changed

Lines changed: 41 additions & 46 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<artifactId>spring-boot-starter-test</artifactId>
3131
<scope>test</scope>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
<version>1.18.30</version>
37+
<scope>provided</scope>
38+
</dependency>
3339
</dependencies>
3440

3541
<build>

src/main/java/com/journal/journal/Controller/JournalEntryControllerV2.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.journal.journal.services.JournalEntryService;
77
import org.bson.types.ObjectId;
88
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
911
import org.springframework.web.bind.annotation.*;
1012

1113
import 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
}
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.journal.journal.entities;
22

3+
import lombok.Data;
4+
import lombok.Getter;
5+
import lombok.Setter;
36
import org.bson.types.ObjectId;
47
import org.springframework.data.annotation.Id;
58
import org.springframework.data.mongodb.core.mapping.Document;
@@ -8,43 +11,11 @@
811
import java.util.Date;
912

1013
@Document(collection = "journal_entries")
14+
@Data
1115
public class JournalEntry {
1216
@Id
1317
private ObjectId id;
1418
private String title;
1519
private String content;
1620
private LocalDateTime date;
17-
public LocalDateTime getDate() {
18-
return date;
19-
}
20-
21-
public void setDate(LocalDateTime date) {
22-
this.date = date;
23-
}
24-
25-
26-
27-
public ObjectId getId() {
28-
return id;
29-
}
30-
31-
public void setId(ObjectId id) {
32-
this.id = id;
33-
}
34-
35-
public String getTitle() {
36-
return title;
37-
}
38-
39-
public void setTitle(String title) {
40-
this.title = title;
41-
}
42-
43-
public String getContent() {
44-
return content;
45-
}
46-
47-
public void setContent(String content) {
48-
this.content = content;
49-
}
5021
}

0 commit comments

Comments
 (0)