소스 검색

add delete note endpoint

Daniel Bohry 1 주 전
부모
커밋
d905d3cc57
2개의 변경된 파일21개의 추가작업 그리고 2개의 파일을 삭제
  1. 9 0
      src/main/java/com/lhamacorp/knotes/api/NoteController.java
  2. 12 2
      src/main/java/com/lhamacorp/knotes/service/NoteService.java

+ 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());
+        }
+
     }
 
 }