From e13c617c81bf55ab81b4d998c100087e330b1a97 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sat, 20 Apr 2019 19:12:35 +0800 Subject: [PATCH] Support attachment query by media type. --- .../run/halo/app/model/params/AttachmentQuery.java | 2 ++ .../run/halo/app/repository/AttachmentRepository.java | 10 ++++++++++ .../java/run/halo/app/repository/PostRepository.java | 3 +-- .../java/run/halo/app/service/AttachmentService.java | 8 ++++++++ .../halo/app/service/impl/AttachmentServiceImpl.java | 9 +++++++++ .../web/controller/admin/api/AttachmentController.java | 6 ++++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/run/halo/app/model/params/AttachmentQuery.java b/src/main/java/run/halo/app/model/params/AttachmentQuery.java index 0c6f4a553..4cf7550fc 100644 --- a/src/main/java/run/halo/app/model/params/AttachmentQuery.java +++ b/src/main/java/run/halo/app/model/params/AttachmentQuery.java @@ -15,4 +15,6 @@ public class AttachmentQuery { * Keyword. */ private String keyword; + + private String mediaType; } diff --git a/src/main/java/run/halo/app/repository/AttachmentRepository.java b/src/main/java/run/halo/app/repository/AttachmentRepository.java index a4a3e852f..ce00b91f4 100644 --- a/src/main/java/run/halo/app/repository/AttachmentRepository.java +++ b/src/main/java/run/halo/app/repository/AttachmentRepository.java @@ -1,13 +1,23 @@ package run.halo.app.repository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import run.halo.app.model.entity.Attachment; import run.halo.app.repository.base.BaseRepository; +import java.util.List; + /** * Attachment repository * * @author johnniang */ public interface AttachmentRepository extends BaseRepository, JpaSpecificationExecutor { + + /** + * Find all attachment media type. + * @return list of media type. + */ + @Query(value = "select distinct a.mediaType from Attachment a") + List findAllMediaType(); } diff --git a/src/main/java/run/halo/app/repository/PostRepository.java b/src/main/java/run/halo/app/repository/PostRepository.java index a4d7c9f8c..85eba224e 100644 --- a/src/main/java/run/halo/app/repository/PostRepository.java +++ b/src/main/java/run/halo/app/repository/PostRepository.java @@ -1,9 +1,8 @@ package run.halo.app.repository; -import run.halo.app.model.entity.Post; -import run.halo.app.repository.base.BasePostRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import run.halo.app.model.entity.Post; import run.halo.app.repository.base.BasePostRepository; diff --git a/src/main/java/run/halo/app/service/AttachmentService.java b/src/main/java/run/halo/app/service/AttachmentService.java index c3036de8a..a3eb7055d 100644 --- a/src/main/java/run/halo/app/service/AttachmentService.java +++ b/src/main/java/run/halo/app/service/AttachmentService.java @@ -10,6 +10,8 @@ import run.halo.app.model.entity.Attachment; import run.halo.app.model.params.AttachmentQuery; import run.halo.app.service.base.CrudService; +import java.util.List; + /** * Attachment service. @@ -54,4 +56,10 @@ public interface AttachmentService extends CrudService { */ @NonNull AttachmentOutputDTO convertToDto(@NonNull Attachment attachment); + + /** + * List all media type. + * @return list of media type + */ + List listAllMediaType(); } 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 a4093fc28..0596bc5fe 100644 --- a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java @@ -69,6 +69,10 @@ public class AttachmentServiceImpl extends AbstractCrudService) (root, query, criteriaBuilder) -> { List predicates = new LinkedList<>(); + if (attachmentQuery.getMediaType() != null) { + predicates.add(criteriaBuilder.equal(root.get("mediaType"), attachmentQuery.getMediaType())); + } + if (attachmentQuery.getKeyword() != null) { String likeCondition = String.format("%%%s%%", StringUtils.strip(attachmentQuery.getKeyword())); @@ -151,6 +155,11 @@ public class AttachmentServiceImpl extends AbstractCrudService listAllMediaType() { + return attachmentRepository.findAllMediaType(); + } + /** * Get attachment type from options. * diff --git a/src/main/java/run/halo/app/web/controller/admin/api/AttachmentController.java b/src/main/java/run/halo/app/web/controller/admin/api/AttachmentController.java index a1a084bb9..69a074110 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/AttachmentController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/AttachmentController.java @@ -100,4 +100,10 @@ public class AttachmentController { return result; } + + @GetMapping("mediaTypes") + @ApiOperation("List all of media types") + public List mediaTypes(){ + return attachmentService.listAllMediaType(); + } }