Retrieve posts according to their expiration date
This commit is contained in:
parent
8607f4dab6
commit
4cd54d81c1
@ -27,14 +27,6 @@ public class PostService {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
private boolean isUserNotRegistered(User user) {
|
||||
var encodedPassword = userRepository.findUserByEmail(user.getEmail());
|
||||
var rawPassword = user.getPassword();
|
||||
|
||||
// Return true if user email exists and the password matches
|
||||
return encodedPassword.filter(s -> passwordEncoder.matches(rawPassword, s.getPassword())).isEmpty();
|
||||
}
|
||||
|
||||
List<Post> getPosts() {
|
||||
return postRepository.findAll().stream().map(post -> {
|
||||
if(post.getUser() != null) {
|
||||
@ -42,7 +34,9 @@ public class PostService {
|
||||
post.getUser().setPassword(null);
|
||||
}
|
||||
return post;
|
||||
}).collect(Collectors.toList());
|
||||
})
|
||||
.filter(post -> post.getExpirationDate() == null || post.getExpirationDate().isAfter(LocalDate.now()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Either<Error, Post> getPostById(String postId) {
|
||||
@ -53,6 +47,11 @@ public class PostService {
|
||||
return Either.left(new Error("Cannot find post"));
|
||||
}
|
||||
|
||||
// Check if post is expired
|
||||
if(post.get().getExpirationDate() != null && post.get().getExpirationDate().isBefore(LocalDate.now())) {
|
||||
return Either.left(new Error("This post has expired"));
|
||||
}
|
||||
|
||||
// Conceal personal user information if available
|
||||
if(post.get().getUser() != null) {
|
||||
post.get().getUser().setId(null);
|
||||
@ -63,17 +62,17 @@ public class PostService {
|
||||
}
|
||||
|
||||
List<Post> getPostByTitle(String title) {
|
||||
var postList = postRepository.findPostByTitle(title);
|
||||
|
||||
// Conceal user information
|
||||
postList.forEach(post -> {
|
||||
return postRepository.findPostByTitle(title).stream().map(post -> {
|
||||
// Conceal user information
|
||||
if(post.getUser() != null) {
|
||||
post.getUser().setId(null);
|
||||
post.getUser().setPassword(null);
|
||||
}
|
||||
});
|
||||
|
||||
return postList;
|
||||
return post;
|
||||
})
|
||||
.filter(post -> post.getExpirationDate() == null || post.getExpirationDate().isAfter(LocalDate.now()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Either<Error, String> addNewPost(Post post) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.ceticamarco.bits.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
@ -13,5 +14,6 @@ public interface UserRepository extends JpaRepository<User, String> {
|
||||
@Query("SELECT u FROM User u WHERE u.username = :username")
|
||||
Optional<User> findUserByUsername(@Param("username") String username);
|
||||
|
||||
@Modifying
|
||||
void deleteUserByEmail(String email);
|
||||
}
|
Loading…
Reference in New Issue
Block a user