Added 'getPostByTitle' method and improved query design
This commit is contained in:
parent
df471937a3
commit
10e595d346
@ -63,11 +63,11 @@ public class PostController {
|
||||
* @return the content of the post
|
||||
*/
|
||||
@GetMapping("/posts/{postId}")
|
||||
public ResponseEntity<String> getPost(@PathVariable("postId") String postId) {
|
||||
public ResponseEntity<String> getPostById(@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 -> {
|
||||
var res = postService.getPostById(postId).map(post -> {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(post);
|
||||
} catch(JsonProcessingException e) {
|
||||
@ -87,13 +87,25 @@ public class PostController {
|
||||
: new ResponseEntity<>(res.getLeft(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get posts by title
|
||||
*
|
||||
* @param req the body contains the title.
|
||||
* Without the title, it acts the same as 'GET /posts'
|
||||
* @return the list of posts
|
||||
*/
|
||||
@PostMapping("/posts")
|
||||
public ResponseEntity<List<Post>> getPostByTitle(@RequestBody Post req) {
|
||||
return new ResponseEntity<>(postService.getPostByTitle(req.getTitle()), HttpStatus.OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new post
|
||||
*
|
||||
* @param post the new post to be submitted
|
||||
* @return on success the new postId, on failure the error message
|
||||
*/
|
||||
@PostMapping("/posts")
|
||||
@PostMapping("/posts/new")
|
||||
public ResponseEntity<String> submitPost(@Valid @RequestBody Post post) {
|
||||
var objectMapper = new ObjectMapper();
|
||||
var res = postService.addNewPost(post).map(postId -> {
|
||||
|
@ -1,6 +1,12 @@
|
||||
package com.ceticamarco.bits.post;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, String> {
|
||||
@Query("SELECT p FROM Post p WHERE p.title LIKE %:title%")
|
||||
List<Post> findPostByTitle(@Param("title") String title);
|
||||
}
|
||||
|
@ -43,8 +43,8 @@ public class PostService {
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Either<Error, Post> getPost(String postId) {
|
||||
Optional<Post> post = postRepository.findById(postId);
|
||||
Either<Error, Post> getPostById(String postId) {
|
||||
var post = postRepository.findById(postId);
|
||||
|
||||
// Check whether the post exists or not
|
||||
if(post.isEmpty()) {
|
||||
@ -58,7 +58,20 @@ public class PostService {
|
||||
}
|
||||
|
||||
return Either.right(post.get());
|
||||
}
|
||||
|
||||
List<Post> getPostByTitle(String title) {
|
||||
var postList = postRepository.findPostByTitle(title);
|
||||
|
||||
// Conceal user information
|
||||
postList.forEach(post -> {
|
||||
if(post.getUser() != null) {
|
||||
post.getUser().setId(null);
|
||||
post.getUser().setPassword(null);
|
||||
}
|
||||
});
|
||||
|
||||
return postList;
|
||||
}
|
||||
|
||||
Either<Error, String> addNewPost(Post post) {
|
||||
|
@ -2,18 +2,19 @@ package com.ceticamarco.bits.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, String> {
|
||||
@Query("SELECT u FROM User u WHERE u.email = ?1")
|
||||
Optional<User> findUserByEmail(String email);
|
||||
@Query("SELECT u FROM User u WHERE u.email = :email")
|
||||
Optional<User> findUserByEmail(@Param("email") String email);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.username = ?1")
|
||||
Optional<User> findUserByUsername(String username);
|
||||
@Query("SELECT u FROM User u WHERE u.username = :username")
|
||||
Optional<User> findUserByUsername(@Param("username") String username);
|
||||
|
||||
@Query("SELECT u.password FROM User u WHERE u.email = ?1")
|
||||
Optional<String> findPasswordByEmail(String email);
|
||||
@Query("SELECT u.password FROM User u WHERE u.email = :email")
|
||||
Optional<String> findPasswordByEmail(@Param("email") String email);
|
||||
|
||||
void deleteUserByEmail(String email);
|
||||
}
|
Loading…
Reference in New Issue
Block a user