Added find endpoint
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -40,9 +40,24 @@ public class NoteServiceTest {
|
||||
|
||||
@Test
|
||||
void deleteNote(){
|
||||
noteService.add(new Note(1L, "Bye bye :(", "content"));
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user