|
@@ -0,0 +1,49 @@
|
|
|
+package com.mooctest.crowd.domain.repository;
|
|
|
+
|
|
|
+import com.mooctest.crowd.domain.dao.TechnicalArticlesDao;
|
|
|
+import com.mooctest.crowd.domain.domainobject.TechnicalArticles;
|
|
|
+import com.mooctest.crowd.domain.model.TechnicalArticlesPO;
|
|
|
+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 TechnicalArticlesRepo implements ITechnicalArticlesRepo{
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TechnicalArticlesDao technicalarticlesDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<TechnicalArticles> getTechnicalArticles(Pageable pageable, String keyword) {
|
|
|
+ Specifications<TechnicalArticlesPO> where = Specifications.where(getArticlesByIsNotDeleted(keyword));
|
|
|
+ return technicalarticlesDao.findAll(where, pageable).map(TechnicalArticlesPO -> Converter.convert(TechnicalArticles.class, TechnicalArticlesPO));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Specification<TechnicalArticlesPO> getArticlesByIsNotDeleted(String keyword) {
|
|
|
+ return new Specification<TechnicalArticlesPO>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<TechnicalArticlesPO> a, CriteriaQuery<?> q, CriteriaBuilder cb) {
|
|
|
+ Predicate predicate = cb.conjunction();
|
|
|
+ if(keyword != null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.like(a.<String>get("name"), "%" + StringUtils.trim(keyword) + "%")
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return predicate;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|