Added find endpoint

This commit is contained in:
Tokishu
2025-05-15 01:33:24 +02:00
parent 18f5330e7b
commit 16ba0ef41c
3 changed files with 35 additions and 3 deletions
@@ -18,6 +18,11 @@ public class NotesController {
return noteService.getAll();
}
@GetMapping("/note/{id}")
public Note find(@PathVariable Long id){
return noteService.find(id);
}
@PostMapping("/note")
public Note add(@RequestBody Note note){
return noteService.add(note);
@@ -1,7 +1,9 @@
package net.tokishu.note.service;
import net.tokishu.note.model.Note;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;
@@ -17,6 +19,16 @@ public class NoteService {
return notes;
}
public Note find(Long id) {
for (Note note : notes) {
if (note.getId().equals(id)) {
return note;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Note not found");
}
public Note add(Note note){
note.setId(idGenerator.getAndIncrement());
notes.add(note);
@@ -27,7 +27,7 @@ public class NoteServiceTest {
}
@Test
void updateNote() {
void updateNote(){
Note note = new Note(1L, "Old QQ", "Old test content");
noteService.add(note);
@@ -39,10 +39,25 @@ public class NoteServiceTest {
}
@Test
void deleteNote() {
noteService.add(new Note(1L, "Bye bye :(", "content"));
void deleteNote(){
Note noteToDelete = new Note(1L, "Bye bye :(", "content");
noteService.add(noteToDelete);
noteService.delete(1L);
assertTrue(noteService.getAll().isEmpty());
}
@Test
void findNote(){
for (long i = 1; i <= 10; i++) {
Note note = new Note(i, "Note " + i, "Content for note №" + i);
noteService.add(note);
}
Note result = noteService.find(5L);
assertNotNull(result, "Note should be found");
assertEquals("Note 5", result.getName());
}
}