diff --git a/src/main/java/run/halo/app/model/dto/base/OutputConverter.java b/src/main/java/run/halo/app/model/dto/base/OutputConverter.java index b803e1000..359f0fd78 100644 --- a/src/main/java/run/halo/app/model/dto/base/OutputConverter.java +++ b/src/main/java/run/halo/app/model/dto/base/OutputConverter.java @@ -1,5 +1,7 @@ package run.halo.app.model.dto.base; +import org.springframework.lang.NonNull; + import static run.halo.app.utils.BeanUtils.updateProperties; /** @@ -20,6 +22,7 @@ public interface OutputConverter, DOMAI * @return converted dto data */ @SuppressWarnings("unchecked") + @NonNull default T convertFrom(DOMAIN domain) { updateProperties(domain, this); diff --git a/src/main/java/run/halo/app/service/AttachmentService.java b/src/main/java/run/halo/app/service/AttachmentService.java index 2013b7f9b..7fc836c29 100644 --- a/src/main/java/run/halo/app/service/AttachmentService.java +++ b/src/main/java/run/halo/app/service/AttachmentService.java @@ -1,13 +1,12 @@ package run.halo.app.service; -import run.halo.app.exception.FileOperationException; -import run.halo.app.model.dto.AttachmentOutputDTO; -import run.halo.app.model.entity.Attachment; -import run.halo.app.service.base.CrudService; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; import org.springframework.web.multipart.MultipartFile; +import run.halo.app.exception.FileOperationException; +import run.halo.app.model.dto.AttachmentOutputDTO; +import run.halo.app.model.entity.Attachment; import run.halo.app.service.base.CrudService; @@ -45,4 +44,13 @@ public interface AttachmentService extends CrudService { */ @NonNull Attachment removePermanently(@NonNull Integer id); + + /** + * Converts to attachment output dto. + * + * @param attachment attachment must not be null + * @return an attachment output dto + */ + @NonNull + AttachmentOutputDTO convertToDto(@NonNull Attachment attachment); } diff --git a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java index 639ac91dd..57cfa40ff 100644 --- a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java @@ -1,5 +1,13 @@ package run.halo.app.service.impl; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; +import org.springframework.web.multipart.MultipartFile; import run.halo.app.handler.file.FileHandlers; import run.halo.app.model.dto.AttachmentOutputDTO; import run.halo.app.model.entity.Attachment; @@ -10,15 +18,8 @@ import run.halo.app.repository.AttachmentRepository; import run.halo.app.service.AttachmentService; import run.halo.app.service.OptionService; import run.halo.app.service.base.AbstractCrudService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.lang.NonNull; -import org.springframework.stereotype.Service; -import org.springframework.util.Assert; -import org.springframework.web.multipart.MultipartFile; -import run.halo.app.handler.file.FileHandlers; -import run.halo.app.service.base.AbstractCrudService; + +import java.util.Objects; /** * AttachmentService implementation class @@ -53,7 +54,7 @@ public class AttachmentServiceImpl extends AbstractCrudService attachmentPage = listAll(pageable); // Convert and return - return attachmentPage.map(attachment -> new AttachmentOutputDTO().convertFrom(attachment)); + return attachmentPage.map(this::convertToDto); } @Override @@ -102,6 +103,29 @@ public class AttachmentServiceImpl extends AbstractCrudService