|
@@ -0,0 +1,51 @@
|
|
|
+package com.mooctest.crowd.domain.repository;
|
|
|
+
|
|
|
+import com.mooctest.crowd.domain.dao.ToolDao;
|
|
|
+import com.mooctest.crowd.domain.domainobject.DeletedStatus;
|
|
|
+import com.mooctest.crowd.domain.domainobject.Tool;
|
|
|
+import com.mooctest.crowd.domain.model.ToolPO;
|
|
|
+import com.mooctest.crowd.domain.util.Converter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.data.jpa.domain.Specifications;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class ToolRepo implements IToolRepo{
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ToolDao toolDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<Tool> getAllTool(Pageable pageable, String keyword) {
|
|
|
+ Specifications<ToolPO> where = Specifications.where(getToolByIsNotDeleted(keyword));
|
|
|
+ return toolDao.findAll(where, pageable).map(ToolPO->Converter.convert(Tool.class, ToolPO));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Specification<ToolPO> getToolByIsNotDeleted(String keyword) {
|
|
|
+ return new Specification<ToolPO>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<ToolPO> a, CriteriaQuery<?> q, CriteriaBuilder cb) {
|
|
|
+ Predicate predicate = cb.conjunction();
|
|
|
+ if(keyword != null) {
|
|
|
+ Predicate title=cb.like(a.get("name"), "%" + StringUtils.trim(keyword) + "%");
|
|
|
+ Predicate description=cb.like(a.get("description"), "%" + StringUtils.trim(keyword) + "%");
|
|
|
+ predicate.getExpressions().add(cb.or(title,description));
|
|
|
+ }
|
|
|
+ predicate.getExpressions().add(cb.equal(a.get("isDeleted"), DeletedStatus.isNotDeleted));
|
|
|
+ return predicate;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|