Fixed a bug
This commit is contained in:
parent
007a80b1bc
commit
c33c7bdfa6
@ -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; }
|
||||
|
@ -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<String> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<Error, Post> getPost(String postId) {
|
||||
Optional<Post> 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<Error, String> 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) {
|
||||
|
@ -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; }
|
||||
}
|
||||
|
@ -52,9 +52,9 @@ public class UserController {
|
||||
@PostMapping("/users")
|
||||
public ResponseEntity<String> 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());
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user