From a55b469ae4e5cac8546734dd413d8f639a0d221b Mon Sep 17 00:00:00 2001 From: ruibaby Date: Wed, 11 Dec 2019 23:14:12 +0800 Subject: [PATCH] feat: support remove in batch api for post. --- .../halo/app/controller/admin/api/PostController.java | 9 ++++++--- src/main/java/run/halo/app/model/entity/PostMeta.java | 3 +++ src/main/java/run/halo/app/service/PostService.java | 10 ++++++++++ .../run/halo/app/service/impl/PostServiceImpl.java | 9 +++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/run/halo/app/controller/admin/api/PostController.java b/src/main/java/run/halo/app/controller/admin/api/PostController.java index 80df340bd..f6904c4ec 100644 --- a/src/main/java/run/halo/app/controller/admin/api/PostController.java +++ b/src/main/java/run/halo/app/controller/admin/api/PostController.java @@ -1,7 +1,6 @@ package run.halo.app.controller.admin.api; import cn.hutool.core.util.IdUtil; -import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -17,7 +16,6 @@ import run.halo.app.model.params.PostContentParam; import run.halo.app.model.params.PostParam; import run.halo.app.model.params.PostQuery; import run.halo.app.model.vo.PostDetailVO; -import run.halo.app.model.vo.PostListVO; import run.halo.app.service.OptionService; import run.halo.app.service.PostService; @@ -140,10 +138,15 @@ public class PostController { @DeleteMapping("{postId:\\d+}") public void deletePermanently(@PathVariable("postId") Integer postId) { - // Remove it postService.removeById(postId); } + @DeleteMapping + @ApiOperation("Delete posts permanently in batch by id array") + public List deletePermanentlyInBatch(@RequestBody List ids) { + return postService.removeByIds(ids); + } + @GetMapping(value = {"preview/{postId:\\d+}", "{postId:\\d+}/preview"}) @ApiOperation("Get preview link") public String preview(@PathVariable("postId") Integer postId) { diff --git a/src/main/java/run/halo/app/model/entity/PostMeta.java b/src/main/java/run/halo/app/model/entity/PostMeta.java index de0ba59c6..edd7bd0df 100644 --- a/src/main/java/run/halo/app/model/entity/PostMeta.java +++ b/src/main/java/run/halo/app/model/entity/PostMeta.java @@ -1,5 +1,7 @@ package run.halo.app.model.entity; +import lombok.EqualsAndHashCode; + import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @@ -12,5 +14,6 @@ import javax.persistence.Entity; */ @Entity(name = "PostMeta") @DiscriminatorValue("0") +@EqualsAndHashCode(callSuper = true) public class PostMeta extends BaseMeta { } diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index dc0795920..ee2e08b7e 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -13,6 +13,7 @@ import run.halo.app.model.vo.PostDetailVO; import run.halo.app.model.vo.PostListVO; import run.halo.app.service.base.BasePostService; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -94,6 +95,15 @@ public interface PostService extends BasePostService { @Override Post getBy(@NonNull PostStatus status, @NonNull String url); + /** + * Removes posts in batch. + * + * @param ids ids must not be null. + * @return a list of deleted post. + */ + @NonNull + List removeByIds(@NonNull Collection ids); + /** * Lists year archives. * diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 09c1f5003..373f93699 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -15,6 +15,7 @@ import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import run.halo.app.event.logger.LogEvent; import run.halo.app.event.post.PostVisitEvent; import run.halo.app.model.dto.BaseMetaDTO; @@ -159,6 +160,14 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe return post; } + @Override + public List removeByIds(Collection ids) { + if (CollectionUtils.isEmpty(ids)) { + return Collections.emptyList(); + } + return ids.stream().map(this::removeById).collect(Collectors.toList()); + } + @Override public Post getByUrl(String url) { Post post = super.getByUrl(url);