feat: 'unFavorite Article' Controller, Service, Repository Implement.
This commit is contained in:
@@ -52,4 +52,9 @@ public class ArticleController {
|
||||
public ArticleResponse favoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug){
|
||||
return articleService.favoriteArticle(userAuth, slug);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{slug}/favorite")
|
||||
public ArticleResponse unFavoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug){
|
||||
return articleService.unFavoriteArticle(userAuth, slug);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface ArticleService {
|
||||
void deleteArticle(UserAuth userAuth, String slug);
|
||||
|
||||
ArticleResponse favoriteArticle(UserAuth userAuth, String slug);
|
||||
|
||||
ArticleResponse unFavoriteArticle(UserAuth userAuth, String slug);
|
||||
}
|
||||
|
||||
@@ -122,6 +122,25 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleResponse unFavoriteArticle(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);
|
||||
}
|
||||
|
||||
Favorite favorite = favoriteRepository.findByArticleIdAndUserId(article.get().getId(), userAuth.getId()).orElseThrow(() -> {
|
||||
throw new CustomException(Error.ALREADY_UN_FAVORITE_ARTICLE);
|
||||
});
|
||||
favoriteRepository.delete(favorite);
|
||||
return convertDtoWithUser(article.get(),userAuth);
|
||||
}
|
||||
|
||||
private String initSlug(String title) {
|
||||
return title.toLowerCase().replace(' ', '-');
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ public enum Error {
|
||||
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),
|
||||
ALREADY_FAVORITE_ARTICLE("already favorite Aritlce", HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
ALREADY_FAVORITE_ARTICLE("already favorite Article", HttpStatus.UNPROCESSABLE_ENTITY),
|
||||
ALREADY_UN_FAVORITE_ARTICLE("already unfavorite Article", HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
|
||||
|
||||
private final String message;
|
||||
|
||||
Reference in New Issue
Block a user