feat : Favorite Article API Implement Controller, Service, Repository
This commit is contained in:
@@ -47,4 +47,9 @@ public class ArticleController {
|
||||
public void deleteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
articleService.deleteArticle(userAuth, slug);
|
||||
}
|
||||
|
||||
@PostMapping("/{slug}/favorite")
|
||||
public ArticleResponse favoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug){
|
||||
return articleService.favoriteArticle(userAuth, slug);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,5 @@ public interface ArticleService {
|
||||
|
||||
void deleteArticle(UserAuth userAuth, String slug);
|
||||
|
||||
ArticleResponse favoriteArticle(UserAuth userAuth, String slug);
|
||||
}
|
||||
|
||||
@@ -47,14 +47,6 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
Pageable pageable = null;
|
||||
List<Article> articles = null;
|
||||
|
||||
if(articleParam.getOffset() != null){
|
||||
pageable = (Pageable) PageRequest.of(articleParam.getOffset(),articleParam.getLimit());
|
||||
}
|
||||
|
||||
if(articleParam.getTag() != null){
|
||||
articles = articleRepository.findByTag(articleParam.getTag(),pageable);
|
||||
}
|
||||
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@@ -113,6 +105,24 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
}
|
||||
}
|
||||
|
||||
public ArticleResponse favoriteArticle(UserAuth userAuth, String slug){
|
||||
Optional<Article> article = articleRepository.findAll().stream().filter(findArticle ->
|
||||
findArticle.getSlug().equals(slug)).findAny();
|
||||
Optional<User> user = userRepository.findById(userAuth.getId());
|
||||
if(article.isEmpty()){
|
||||
throw new CustomException(Error.ARTICLE_NOT_FOUND);
|
||||
}
|
||||
if(user.isEmpty()){
|
||||
throw new CustomException(Error.USER_NOT_FOUND);
|
||||
}
|
||||
|
||||
favoriteRepository.findByArticleIdAndAuthorId(article.get().getId(), userAuth.getId()).ifPresent(favoriteStatus -> {throw new CustomException(Error.ALREADY_FAVORITE_ARTICLE);});
|
||||
Favorite favorite = Favorite.builder().article(article.get()).author(user.get()).build();
|
||||
favoriteRepository.save(favorite);
|
||||
return convertDtoWithUser(article.get(),userAuth);
|
||||
|
||||
}
|
||||
|
||||
private String initSlug(String title) {
|
||||
return title.toLowerCase().replace(' ', '-');
|
||||
}
|
||||
|
||||
@@ -23,8 +23,12 @@ public class ProfileServiceImpl implements ProfileService {
|
||||
@Override
|
||||
public ProfileResponse getProfile(UserAuth userAuth, String username) {
|
||||
Optional<User> wantFindUser = Optional.ofNullable(userRepository.findByUsername(username).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND)));
|
||||
Boolean followStatus = profileRepository.findByFolloweeIdAndFollowerId(userAuth.getId(), wantFindUser.get().getId()).isPresent();
|
||||
|
||||
Boolean followStatus = null;
|
||||
if(userAuth == null){
|
||||
followStatus = false;
|
||||
}else {
|
||||
followStatus = profileRepository.findByFolloweeIdAndFollowerId(userAuth.getId(), wantFindUser.get().getId()).isPresent();
|
||||
}
|
||||
return convertProfile(followStatus, wantFindUser);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ public enum Error {
|
||||
USER_NOT_FOUND("user not found check your info",HttpStatus.NOT_FOUND),
|
||||
ALREADY_FOLLOW("already follow",HttpStatus.UNPROCESSABLE_ENTITY),
|
||||
ALREADY_UNFOLLOW("already unfollow",HttpStatus.UNPROCESSABLE_ENTITY),
|
||||
ARTICLE_NOT_FOUND("article not found check your slug", HttpStatus.NOT_FOUND);
|
||||
ARTICLE_NOT_FOUND("article not found check your slug", HttpStatus.NOT_FOUND),
|
||||
ALREADY_FAVORITE_ARTICLE("already favorite Aritlce", HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
|
||||
|
||||
private final String message;
|
||||
|
||||
Reference in New Issue
Block a user