Add api: delete attachments permanently in batch

pull/755/head
johnniang 2019-12-02 23:15:31 +08:00
parent 3676a77be2
commit 2ebea696a5
3 changed files with 32 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.cache.lock.CacheLock;
import run.halo.app.model.dto.AttachmentDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.params.AttachmentParam;
@ -75,11 +76,17 @@ public class AttachmentController {
* @param id id
*/
@DeleteMapping("{id:\\d+}")
@ApiOperation("Delete attachment by id")
@ApiOperation("Delete attachment permanently by id")
public AttachmentDTO deletePermanently(@PathVariable("id") Integer id) {
return attachmentService.convertToDto(attachmentService.removePermanently(id));
}
@DeleteMapping
@ApiOperation("Delete attachments permanently in batch by id array")
public List<Attachment> deletePermanentlyInBatch(@RequestBody List<Integer> ids) {
return attachmentService.removePermanently(ids);
}
@PostMapping("upload")
@ApiOperation("Uploads single file")
public AttachmentDTO uploadAttachment(@RequestPart("file") MultipartFile file) {

View File

@ -10,6 +10,7 @@ import run.halo.app.model.entity.Attachment;
import run.halo.app.model.params.AttachmentQuery;
import run.halo.app.service.base.CrudService;
import java.util.Collection;
import java.util.List;
@ -49,6 +50,15 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
@NonNull
Attachment removePermanently(@NonNull Integer id);
/**
* Removes attachment permanently in batch.
*
* @param ids attachment ids must not be null
* @return attachment detail list deleted
*/
@NonNull
List<Attachment> removePermanently(@NonNull Collection<Integer> ids);
/**
* Converts to attachment output dto.
*

View File

@ -6,8 +6,10 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.handler.file.FileHandlers;
@ -22,11 +24,11 @@ import run.halo.app.service.AttachmentService;
import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.HaloUtils;
import run.halo.app.utils.ServiceUtils;
import javax.persistence.criteria.Predicate;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
* AttachmentService implementation
@ -140,6 +142,15 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
return deletedAttachment;
}
@Override
public List<Attachment> removePermanently(@Nullable Collection<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
return ids.stream().map(this::removePermanently).collect(Collectors.toList());
}
@Override
public AttachmentDTO convertToDto(Attachment attachment) {
Assert.notNull(attachment, "Attachment must not be null");