remove favorite
This commit is contained in:
@@ -11,6 +11,7 @@ import io.spring.core.user.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -43,8 +44,18 @@ public class ArticleFavoriteApi {
|
||||
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public ResponseEntity unfavoriteArticle(@PathVariable("slug") String slug,
|
||||
@AuthenticationPrincipal User user) {
|
||||
Article article = getArticle(slug);
|
||||
articleFavoriteRepository.find(article.getId(), user.getId()).ifPresent(favorite -> {
|
||||
articleFavoriteRepository.remove(favorite);
|
||||
});
|
||||
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||
}
|
||||
|
||||
private ResponseEntity<HashMap<String, Object>> responseArticleData(final ArticleData articleData) {
|
||||
return ResponseEntity.status(201).body(new HashMap<String, Object>() {{
|
||||
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
||||
put("article", articleData);
|
||||
}});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package io.spring.core.favorite;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ArticleFavoriteRepository {
|
||||
void save(ArticleFavorite articleFavorite);
|
||||
|
||||
Optional<ArticleFavorite> find(String articleId, String userId);
|
||||
|
||||
void remove(ArticleFavorite favorite);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ import org.springframework.stereotype.Component;
|
||||
@Mapper
|
||||
@Component
|
||||
public interface ArticleFavoriteMapper {
|
||||
boolean find(@Param("articleFavorite") ArticleFavorite articleFavorite);
|
||||
ArticleFavorite find(@Param("articleId") String articleId, @Param("userId") String userId);
|
||||
|
||||
void insert(@Param("articleFavorite") ArticleFavorite articleFavorite);
|
||||
|
||||
void delete(@Param("favorite") ArticleFavorite favorite);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public class MyBatisArticleFavoriteRepository implements ArticleFavoriteRepository {
|
||||
private ArticleFavoriteMapper mapper;
|
||||
@@ -16,8 +18,18 @@ public class MyBatisArticleFavoriteRepository implements ArticleFavoriteReposito
|
||||
|
||||
@Override
|
||||
public void save(ArticleFavorite articleFavorite) {
|
||||
if (!mapper.find(articleFavorite)) {
|
||||
if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) != null) {
|
||||
mapper.insert(articleFavorite);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ArticleFavorite> find(String articleId, String userId) {
|
||||
return Optional.ofNullable(mapper.find(articleId, userId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(ArticleFavorite favorite) {
|
||||
mapper.delete(favorite);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user