From c33c7bdfa6cecb476498f2225aed99a04cdce705 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Thu, 11 Jan 2024 11:45:42 +0100 Subject: [PATCH] Fixed a bug --- .../java/com/ceticamarco/bits/post/Post.java | 2 ++ .../ceticamarco/bits/post/PostController.java | 26 +++++++++++++++++-- .../ceticamarco/bits/post/PostService.java | 19 ++++++++++++++ .../java/com/ceticamarco/bits/user/User.java | 4 ++- .../ceticamarco/bits/user/UserController.java | 4 +-- .../ceticamarco/bits/user/UserService.java | 6 ++--- 6 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ceticamarco/bits/post/Post.java b/src/main/java/com/ceticamarco/bits/post/Post.java index 64fec7a..c661dd0 100644 --- a/src/main/java/com/ceticamarco/bits/post/Post.java +++ b/src/main/java/com/ceticamarco/bits/post/Post.java @@ -45,6 +45,8 @@ public class Post { public String getContent() { return this.content; } + public LocalDate getExpirationDate() { return this.expirationDate; } + public User getUser() { return this.user; } public void setUser(User user) { this.user = user; } diff --git a/src/main/java/com/ceticamarco/bits/post/PostController.java b/src/main/java/com/ceticamarco/bits/post/PostController.java index b5c16fb..430ce7a 100644 --- a/src/main/java/com/ceticamarco/bits/post/PostController.java +++ b/src/main/java/com/ceticamarco/bits/post/PostController.java @@ -2,6 +2,8 @@ package com.ceticamarco.bits.post; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -61,8 +63,28 @@ public class PostController { * @return the content of the post */ @GetMapping("/posts/{postId}") - public String getPost(@PathVariable("postId") Integer postId) { - return ""; + public ResponseEntity getPost(@PathVariable("postId") String postId) { + var objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + var res = postService.getPost(postId).map(post -> { + try { + return objectMapper.writeValueAsString(post); + } catch(JsonProcessingException e) { + throw new RuntimeException(e.getMessage()); + } + }).swap().map(error -> { + try { + var jsonNode = objectMapper.createObjectNode().put("error", error.getMessage()); + return objectMapper.writeValueAsString(jsonNode); + } catch(JsonProcessingException e) { + throw new RuntimeException(e.getMessage()); + } + }).swap(); + + return res.isRight() + ? new ResponseEntity<>(res.get(), HttpStatus.OK) + : new ResponseEntity<>(res.getLeft(), HttpStatus.BAD_REQUEST); } /** diff --git a/src/main/java/com/ceticamarco/bits/post/PostService.java b/src/main/java/com/ceticamarco/bits/post/PostService.java index bdc69d7..94370c1 100644 --- a/src/main/java/com/ceticamarco/bits/post/PostService.java +++ b/src/main/java/com/ceticamarco/bits/post/PostService.java @@ -8,6 +8,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import java.time.LocalDate; +import java.util.Optional; @Service public class PostService { @@ -30,6 +31,24 @@ public class PostService { return encodedPassword.filter(s -> passwordEncoder.matches(rawPassword, s)).isPresent(); } + Either getPost(String postId) { + Optional post = postRepository.findById(postId); + + // Check whether the post exists or not + if(post.isEmpty()) { + return Either.left(new Error("Cannot find post")); + } + + // Conceal personal user information if available + if(post.get().getUser() != null) { + post.get().getUser().setId(null); + post.get().getUser().setPassword(null); + } + + return Either.right(post.get()); + + } + Either addNewPost(Post post) { // Check whether the user email and user password are specified if(post.getUser() != null && post.getUser().getEmail() != null && post.getUser().getPassword() != null) { diff --git a/src/main/java/com/ceticamarco/bits/user/User.java b/src/main/java/com/ceticamarco/bits/user/User.java index 14d7957..b3502b5 100644 --- a/src/main/java/com/ceticamarco/bits/user/User.java +++ b/src/main/java/com/ceticamarco/bits/user/User.java @@ -37,6 +37,8 @@ public class User { public User() {} + public String getId() { return this.id; } + public String getEmail() { return this.email; } public String getUsername() { return this.username; } @@ -45,5 +47,5 @@ public class User { public void setPassword(String password) { this.password = password; } - public String getId() { return this.id; } + public void setId(String id) { this.id = id; } } diff --git a/src/main/java/com/ceticamarco/bits/user/UserController.java b/src/main/java/com/ceticamarco/bits/user/UserController.java index 253a259..655ffd5 100644 --- a/src/main/java/com/ceticamarco/bits/user/UserController.java +++ b/src/main/java/com/ceticamarco/bits/user/UserController.java @@ -52,9 +52,9 @@ public class UserController { @PostMapping("/users") public ResponseEntity submitUser(@Valid @RequestBody User user) { var objectMapper = new ObjectMapper(); - var res = userService.addNewUser(user).map(user_id -> { + var res = userService.addNewUser(user).map(userId -> { try { - var jsonNode = objectMapper.createObjectNode().put("user_id", user_id); + var jsonNode = objectMapper.createObjectNode().put("user_id", userId); return objectMapper.writeValueAsString(jsonNode); } catch(JsonProcessingException e) { throw new RuntimeException(e.getMessage()); diff --git a/src/main/java/com/ceticamarco/bits/user/UserService.java b/src/main/java/com/ceticamarco/bits/user/UserService.java index 7939e72..79aa3c6 100644 --- a/src/main/java/com/ceticamarco/bits/user/UserService.java +++ b/src/main/java/com/ceticamarco/bits/user/UserService.java @@ -26,7 +26,7 @@ public class UserService { // If they are found, return an error if(userEmail.isPresent() || userName.isPresent()) { - return Either.left(new Error("Email or username already taken.")); + return Either.left(new Error("Email or username already taken")); } // Hash the password @@ -47,13 +47,13 @@ public class UserService { // Check whether user exists if(encodedPassword.isEmpty()) { - return Optional.of(new Error("Cannot find user.")); + return Optional.of(new Error("Cannot find user")); } // Otherwise compare the hash var isHashEqual = passwordEncoder.matches(rawPassword, encodedPassword.get()); if(!isHashEqual) { - return Optional.of(new Error("Wrong password.")); + return Optional.of(new Error("Wrong password")); } userRepository.deleteUserByEmail(user.getEmail());