Support attachment query by media type.

pull/146/head
ruibaby 2019-04-20 19:12:35 +08:00
parent bd9dd69674
commit e13c617c81
6 changed files with 36 additions and 2 deletions

View File

@ -15,4 +15,6 @@ public class AttachmentQuery {
* Keyword.
*/
private String keyword;
private String mediaType;
}

View File

@ -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<Attachment, Integer>, JpaSpecificationExecutor<Attachment> {
/**
* Find all attachment media type.
* @return list of media type.
*/
@Query(value = "select distinct a.mediaType from Attachment a")
List<String> findAllMediaType();
}

View File

@ -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;

View File

@ -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<Attachment, Integer> {
*/
@NonNull
AttachmentOutputDTO convertToDto(@NonNull Attachment attachment);
/**
* List all media type.
* @return list of media type
*/
List<String> listAllMediaType();
}

View File

@ -69,6 +69,10 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
return (Specification<Attachment>) (root, query, criteriaBuilder) -> {
List<Predicate> 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<Attachment, Integ
return attachmentOutputDTO;
}
@Override
public List<String> listAllMediaType() {
return attachmentRepository.findAllMediaType();
}
/**
* Get attachment type from options.
*

View File

@ -100,4 +100,10 @@ public class AttachmentController {
return result;
}
@GetMapping("mediaTypes")
@ApiOperation("List all of media types")
public List<String> mediaTypes(){
return attachmentService.listAllMediaType();
}
}