Kaynağa Gözat

add delete note endpoint

Daniel Bohry 1 hafta önce
ebeveyn
işleme
d905d3cc57

+ 9 - 0
src/main/java/com/lhamacorp/knotes/api/NoteController.java

@@ -102,4 +102,13 @@ public class NoteController {
         };
     }
 
+    @DeleteMapping("{id}")
+    public ResponseEntity<Void> delete(@PathVariable String id) {
+        if (isAuthenticated()) {
+            service.delete(id);
+        }
+
+        return ok().build();
+    }
+
 }

+ 12 - 2
src/main/java/com/lhamacorp/knotes/service/NoteService.java

@@ -89,8 +89,18 @@ public class NoteService {
 
     @CacheEvict(value = {"content", "metadata"}, key = "#id")
     public Note update(String id, String content) {
-        Note existingNote = repository.findById(id).orElseThrow(() -> new NotFoundException(NOT_FOUND));
-        return update(id, content, existingNote.encryptionMode(), null);
+        Note note = repository.findById(id).orElseThrow(() -> new NotFoundException(NOT_FOUND));
+        return update(id, content, note.encryptionMode(), null);
+    }
+
+    public void delete(String id) {
+        Note note = repository.findById(id).orElseThrow(() -> new NotFoundException("Note not found"));
+        String userId = UserContextHolder.get().id();
+
+        if (note.createdBy().equals(userId)) {
+            repository.deleteById(note.id());
+        }
+
     }
 
 }