Explorar el Código

code cleanups

Daniel Bohry hace 1 semana
padre
commit
309c7d9661

+ 6 - 11
src/main/java/com/lhamacorp/knotes/service/NoteService.java

@@ -17,8 +17,10 @@ import java.time.Instant;
 import java.util.List;
 
 import static com.github.f4b6a3.ulid.UlidCreator.getUlid;
+import static com.lhamacorp.knotes.api.dto.NoteMetadata.from;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PRIVATE;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PUBLIC;
+import static com.lhamacorp.knotes.domain.Note.ANONYMOUS;
 import static java.time.Instant.now;
 import static java.util.Collections.emptyList;
 
@@ -39,8 +41,7 @@ public class NoteService {
 
     public List<String> findAll() {
         UserContext user = UserContextHolder.get();
-
-        return "1".equals(user.id())
+        return ANONYMOUS.equals(user.id())
                 ? emptyList()
                 : repository.findAllByCreatedBy(user.id()).stream().map(Note::id).toList();
     }
@@ -55,14 +56,14 @@ public class NoteService {
     public NoteMetadata findMetadataById(String id) {
         Note noteProjection = repository.findMetadataById(id)
                 .orElseThrow(() -> new NotFoundException(NOT_FOUND));
-        return NoteMetadata.from(noteProjection);
+        return from(noteProjection);
     }
 
     public Note save(String content, EncryptionMode encryptionMode) {
         Ulid id = getUlid();
         UserContext user = UserContextHolder.get();
-
         Instant now = now();
+
         return repository.save(new Note(id.toString(), content, user.id(), now, now, encryptionMode, null));
     }
 
@@ -75,7 +76,7 @@ public class NoteService {
             throw new UnauthorizedException("Not authorized to update this content");
         }
 
-        if ("1".equals(user.id())) {
+        if (ANONYMOUS.equals(user.id())) {
             encryptionMode = PUBLIC;
             password = null;
         }
@@ -87,12 +88,6 @@ public class NoteService {
         return repository.save(new Note(id, content, existingNote.createdBy(), existingNote.createdAt(), now(), encryptionMode, password));
     }
 
-    @CacheEvict(value = {"content", "metadata"}, key = "#id")
-    public Note update(String id, String content) {
-        Note note = repository.findById(id).orElseThrow(() -> new NotFoundException(NOT_FOUND));
-        return update(id, content, note.encryptionMode(), null);
-    }
-
     @CacheEvict(value = {"content", "metadata"}, key = "#id")
     public void delete(String id) {
         Note note = repository.findById(id).orElseThrow(() -> new NotFoundException("Note not found"));

+ 3 - 3
src/test/java/com/lhamacorp/knotes/service/NoteServiceTest.java

@@ -226,7 +226,7 @@ class NoteServiceTest {
         when(repository.save(any(Note.class))).thenReturn(updatedNote);
 
         // When
-        Note result = noteService.update(testId, updatedContent);
+        Note result = noteService.update(testId, updatedContent, null, null);
 
         // Then
         assertEquals(updatedNote, result);
@@ -248,7 +248,7 @@ class NoteServiceTest {
 
         // When & Then
         NotFoundException exception = assertThrows(NotFoundException.class,
-            () -> noteService.update(testId, updatedContent));
+            () -> noteService.update(testId, updatedContent, null, null));
 
         assertEquals("Note not found!", exception.getMessage());
         verify(repository).findById(testId);
@@ -265,7 +265,7 @@ class NoteServiceTest {
         when(repository.save(any(Note.class))).thenReturn(updatedNote);
 
         // When
-        Note result = noteService.update(testId, null);
+        Note result = noteService.update(testId, null, null, null);
 
         // Then
         assertEquals(updatedNote, result);