Explorar el Código

code cleanups

Daniel Bohry hace 1 semana
padre
commit
4f3dceeec9
Se han modificado 1 ficheros con 10 adiciones y 7 borrados
  1. 10 7
      src/main/java/com/lhamacorp/knotes/api/NoteController.java

+ 10 - 7
src/main/java/com/lhamacorp/knotes/api/NoteController.java

@@ -9,7 +9,6 @@ import com.lhamacorp.knotes.context.UserContextHolder;
 import com.lhamacorp.knotes.domain.EncryptionMode;
 import com.lhamacorp.knotes.domain.EncryptionMode;
 import com.lhamacorp.knotes.domain.Note;
 import com.lhamacorp.knotes.domain.Note;
 import com.lhamacorp.knotes.service.NoteService;
 import com.lhamacorp.knotes.service.NoteService;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
@@ -18,7 +17,9 @@ import java.util.List;
 import static com.lhamacorp.knotes.context.UserContextHolder.isAuthenticated;
 import static com.lhamacorp.knotes.context.UserContextHolder.isAuthenticated;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PRIVATE;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PRIVATE;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PUBLIC;
 import static com.lhamacorp.knotes.domain.EncryptionMode.PUBLIC;
-import static org.springframework.http.ResponseEntity.*;
+import static org.springframework.http.HttpStatus.UNAUTHORIZED;
+import static org.springframework.http.ResponseEntity.badRequest;
+import static org.springframework.http.ResponseEntity.ok;
 
 
 @RestController
 @RestController
 @RequestMapping("api/notes")
 @RequestMapping("api/notes")
@@ -27,6 +28,8 @@ public class NoteController {
 
 
     private final NoteService service;
     private final NoteService service;
 
 
+    private static final String ANONYMOUS = "1";
+
     public NoteController(NoteService service) {
     public NoteController(NoteService service) {
         this.service = service;
         this.service = service;
     }
     }
@@ -42,8 +45,8 @@ public class NoteController {
         UserContext user = UserContextHolder.get();
         UserContext user = UserContextHolder.get();
         Note note = service.findById(id);
         Note note = service.findById(id);
 
 
-        if (!note.createdBy().equals("1") && !note.createdBy().equals(user.id())) {
-            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+        if (!note.createdBy().equals(ANONYMOUS) && !note.createdBy().equals(user.id())) {
+            return ResponseEntity.status(UNAUTHORIZED).build();
         }
         }
 
 
         EncryptionMode mode = note.encryptionMode() != null ? note.encryptionMode() : PUBLIC;
         EncryptionMode mode = note.encryptionMode() != null ? note.encryptionMode() : PUBLIC;
@@ -68,7 +71,7 @@ public class NoteController {
         UserContext user = UserContextHolder.get();
         UserContext user = UserContextHolder.get();
         String userId = user.id();
         String userId = user.id();
 
 
-        if ("1".equals(userId) && request.encryptionMode() != null
+        if (ANONYMOUS.equals(userId) && request.encryptionMode() != null
                 && !request.encryptionMode().equals("PUBLIC")) {
                 && !request.encryptionMode().equals("PUBLIC")) {
             return badRequest().build();
             return badRequest().build();
         }
         }
@@ -94,9 +97,9 @@ public class NoteController {
 
 
     @PostMapping
     @PostMapping
     public ResponseEntity<NoteResponse> save(@RequestBody NoteRequest request) {
     public ResponseEntity<NoteResponse> save(@RequestBody NoteRequest request) {
-        String userId = isAuthenticated() ? UserContextHolder.get().id() : "1";
+        String userId = isAuthenticated() ? UserContextHolder.get().id() : ANONYMOUS;
 
 
-        EncryptionMode mode = userId.equals("1") ? PUBLIC : PRIVATE;
+        EncryptionMode mode = userId.equals(ANONYMOUS) ? PUBLIC : PRIVATE;
         Note savedNote = service.save(request.note(), mode);
         Note savedNote = service.save(request.note(), mode);
 
 
         return switch (mode) {
         return switch (mode) {