From 839e58f5cbaf8de75126589d16efdebea3f402db Mon Sep 17 00:00:00 2001 From: onewomanonecity <2589071709@qq.com> Date: Fri, 27 Mar 2020 03:31:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E9=9A=90=E8=97=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../article/service/ArticleService.java | 298 +++--- .../provider/ArticleServiceProvider.java | 913 ++++++++++-------- .../article/directive/ArticleDirective.java | 128 +-- .../directive/ArticlePageDirective.java | 218 ++--- .../directive/ArticleSearchPageDirective.java | 170 ++-- .../article/directive/ArticlesDirective.java | 167 ++-- .../directive/CategoryArticlesDirective.java | 169 ++-- .../directive/NextArticleDirective.java | 113 +-- .../directive/PreviousArticleDirective.java | 113 +-- .../directive/RelevantArticlesDirective.java | 138 +-- .../directive/TagArticlesDirective.java | 165 ++-- .../directive/UserArticlesDirective.java | 164 ++-- 12 files changed, 1452 insertions(+), 1304 deletions(-) diff --git a/module-article/module-article-service-api/src/main/java/io/jpress/module/article/service/ArticleService.java b/module-article/module-article-service-api/src/main/java/io/jpress/module/article/service/ArticleService.java index 2d48670e1..1b8b5e35a 100755 --- a/module-article/module-article-service-api/src/main/java/io/jpress/module/article/service/ArticleService.java +++ b/module-article/module-article-service-api/src/main/java/io/jpress/module/article/service/ArticleService.java @@ -1,144 +1,156 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.service; - -import com.jfinal.plugin.activerecord.Page; -import io.jboot.db.model.Columns; -import io.jboot.service.JbootServiceJoiner; -import io.jpress.module.article.model.Article; - -import java.util.List; - -public interface ArticleService extends JbootServiceJoiner { - - /** - * find model by primary key - * - * @param id - * @return - */ - public Article findById(Object id); - - public Article findByTitle(String title); - - - /** - * find all model - * - * @return all

findAll(); - - - /** - * delete model by primary key - * - * @param id - * @return success - */ - public boolean deleteById(Object id); - - - public void removeCacheById(Object id); - - - /** - * 删除多个id - * - * @param ids - * @return - */ - public boolean deleteByIds(Object... ids); - - - /** - * delete model - * - * @param model - * @return - */ - public boolean delete(Article model); - - - /** - * save model to database - * - * @param model - * @return - */ - public Object save(Article model); - - - /** - * save or update model - * - * @param model - * @return if save or update success - */ - public Object saveOrUpdate(Article model); - - - /** - * update data model - * - * @param model - * @return - */ - public boolean update(Article model); - - - public void doUpdateCategorys(long articleId, Long[] categoryIds); - - public void doUpdateCommentCount(long articleId); - - public boolean doChangeStatus(long id, String status); - - public Long findCountByStatus(String status); - - public Article findFirstBySlug(String slug); - - public Article findNextById(long id); - - public Article findPreviousById(long id); - - public List
findListByColumns(Columns columns, String orderBy, Integer count); - - public List
findListByCategoryId(long categoryId, Boolean hasThumbnail, String orderBy, Integer count); - - public List
findRelevantListByArticleId(long ArticleId, String status, Integer count); - - public Page
_paginateByStatus(int page, int pagesize, String title, Long categoryId, String status); - - public Page
_paginateWithoutTrash(int page, int pagesize, String title, Long categoryId); - - public Page
_paginateByUserId(int page, int pagesize, Long userId); - - public Page
paginateInNormal(int page, int pagesize); - - public Page
paginateInNormal(int page, int pagesize, String orderBy); - - public Page
paginateByCategoryIdInNormal(int page, int pagesize, long categoryId, String orderBy); - - public void doIncArticleViewCount(long articleId); - - public void doIncArticleCommentCount(long articleId); - - public Page
search(String queryString, int pageNum, int pageSize); - - public Page
searchIndb(String queryString, int pageNum, int pageSize); - - +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.service; + +import com.jfinal.plugin.activerecord.Page; +import io.jboot.db.model.Columns; +import io.jboot.service.JbootServiceJoiner; +import io.jpress.module.article.model.Article; + +import java.util.List; + +public interface ArticleService extends JbootServiceJoiner { + + /** + * find model by primary key + * + * @param id + * @return + */ + public Article findById(Object id); + + public Article findByTitle(String title); + + + /** + * find all model + * + * @return all

findAll(); + + + /** + * delete model by primary key + * + * @param id + * @return success + */ + public boolean deleteById(Object id); + + + public void removeCacheById(Object id); + + + /** + * 删除多个id + * + * @param ids + * @return + */ + public boolean deleteByIds(Object... ids); + + + /** + * delete model + * + * @param model + * @return + */ + public boolean delete(Article model); + + + /** + * save model to database + * + * @param model + * @return + */ + public Object save(Article model); + + + /** + * save or update model + * + * @param model + * @return if save or update success + */ + public Object saveOrUpdate(Article model); + + + /** + * update data model + * + * @param model + * @return + */ + public boolean update(Article model); + + + public void doUpdateCategorys(long articleId, Long[] categoryIds); + + public void doUpdateCommentCount(long articleId); + + public boolean doChangeStatus(long id, String status); + + public Long findCountByStatus(String status); + + public Article findFirstBySlug(String slug); + + public Article findNextById(long id); + + public Article findNextByIdNotHidden(long id); + + public Article findPreviousById(long id); + + public Article findPreviousByIdNotHidden(long id); + + public List
findListByColumns(Columns columns, String orderBy, Integer count); + + public List
findListByCategoryId(long categoryId, Boolean hasThumbnail, String orderBy, Integer count); + + public List
findListByCategoryIdNotHidden(long categoryId, Boolean hasThumbnail, String orderBy, Integer count); + + public List
findRelevantListByArticleId(long ArticleId, String status, Integer count); + + public List
findRelevantListByArticleIdNotHidden(long ArticleId, String status, Integer count); + + public Page
_paginateByStatus(int page, int pagesize, String title, Long categoryId, String status); + + public Page
_paginateWithoutTrash(int page, int pagesize, String title, Long categoryId); + + public Page
_paginateByUserId(int page, int pagesize, Long userId); + + public Page
paginateInNormal(int page, int pagesize); + + public Page
paginateInNormal(int page, int pagesize, String orderBy); + + public Page
paginateInNormalNotHidden(int page, int pagesize, String orderBy); + + public Page
paginateByCategoryIdInNormalNotHidden(int page, int pagesize, long categoryId, String orderBy); + + public Page
paginateByCategoryIdInNormal(int page, int pagesize, long categoryId, String orderBy); + + public void doIncArticleViewCount(long articleId); + + public void doIncArticleCommentCount(long articleId); + + public Page
search(String queryString, int pageNum, int pageSize); + + public Page
searchIndb(String queryString, int pageNum, int pageSize); + + } \ No newline at end of file diff --git a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java index 5a4c3c936..e9af9b269 100755 --- a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java +++ b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java @@ -1,408 +1,507 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.service.provider; - -import com.jfinal.aop.Inject; -import com.jfinal.kit.LogKit; -import com.jfinal.plugin.activerecord.Db; -import com.jfinal.plugin.activerecord.Model; -import com.jfinal.plugin.activerecord.Page; -import com.jfinal.plugin.activerecord.Record; -import io.jboot.aop.annotation.Bean; -import io.jboot.components.cache.annotation.CacheEvict; -import io.jboot.components.cache.annotation.Cacheable; -import io.jboot.components.cache.annotation.CachesEvict; -import io.jboot.db.model.Column; -import io.jboot.db.model.Columns; -import io.jboot.service.JbootServiceBase; -import io.jboot.utils.StrUtil; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.model.ArticleCategory; -import io.jpress.module.article.service.ArticleCategoryService; -import io.jpress.module.article.service.ArticleCommentService; -import io.jpress.module.article.service.ArticleService; -import io.jpress.module.article.service.search.ArticleSearcher; -import io.jpress.module.article.service.search.ArticleSearcherFactory; -import io.jpress.module.article.service.sitemap.ArticleSitemapManager; -import io.jpress.module.article.service.task.ArticleCommentsCountUpdateTask; -import io.jpress.module.article.service.task.ArticleViewsCountUpdateTask; -import io.jpress.service.UserService; -import io.jpress.web.seoping.SeoManager; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -@Bean -public class ArticleServiceProvider extends JbootServiceBase

implements ArticleService { - - @Inject - private ArticleCategoryService categoryService; - - @Inject - private UserService userService; - - @Inject - private ArticleCommentService commentService; - - private static final String DEFAULT_ORDER_BY = "order_number desc,id desc"; - - @Override - public boolean deleteByIds(Object... ids) { - for (Object id : ids) { - deleteById(id); - } - return true; - } - - - @Override - public void doUpdateCommentCount(long articleId) { - Article article = findById(articleId); - if (article == null) { - return; - } - - long count = commentService.findCountByArticleId(articleId); - article.setCommentCount(count); - article.update(); - } - - @Override - public Page
_paginateByStatus(int page, int pagesize, String title, Long categoryId, String status) { - - return _paginateByBaseColumns(page - , pagesize - , title - , categoryId - , Columns.create("article.status", status)); - } - - @Override - public Page
_paginateWithoutTrash(int page, int pagesize, String title, Long categoryId) { - - return _paginateByBaseColumns(page - , pagesize - , title - , categoryId - , Columns.create().ne("article.status", Article.STATUS_TRASH)); - } - - - public Page
_paginateByBaseColumns(int page, int pagesize, String title, Long categoryId, Columns baseColumns) { - - Columns columns = baseColumns; - columns.add("m.category_id", categoryId); - columns.likeAppendPercent("article.title", title); - - Page
dataPage = DAO.leftJoinIf("article_category_mapping", categoryId != null) - .as("m") - .on("article.id = m.article_id") - .paginateByColumns(page, pagesize, columns, "id desc"); - - - return joinUserInfo(dataPage); - } - - @Override - public Page
_paginateByUserId(int page, int pagesize, Long userId) { - return DAO.paginateByColumn(page, pagesize, Column.create("user_id", userId), DEFAULT_ORDER_BY); - } - - @Override - @Cacheable(name = "articles") - public Page
paginateInNormal(int page, int pagesize) { - return paginateInNormal(page, pagesize, "id desc"); - } - - @Override - @Cacheable(name = "articles") - public Page
paginateInNormal(int page, int pagesize, String orderBy) { - orderBy = StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY); - Columns columns = new Columns(); - columns.add("status", Article.STATUS_NORMAL); - Page
dataPage = DAO.paginateByColumns(page, pagesize, columns, orderBy); - return joinUserInfo(dataPage); - } - - - @Override - @Cacheable(name = "articles") - public Page
paginateByCategoryIdInNormal(int page, int pagesize, long categoryId, String orderBy) { - - Columns columns = new Columns(); - columns.add("m.category_id", categoryId); - columns.add("article.status", Article.STATUS_NORMAL); - - Page
dataPage = DAO.leftJoin("article_category_mapping") - .as("m").on("article.id=m.`article_id`") - .paginateByColumns(page, pagesize, columns, StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY)); - return joinUserInfo(dataPage); - } - - @Override - public void doIncArticleViewCount(long articleId) { - ArticleViewsCountUpdateTask.recordCount(articleId); - } - - @Override - public void doIncArticleCommentCount(long articleId) { - ArticleCommentsCountUpdateTask.recordCount(articleId); - } - - - @Override - public Page
search(String queryString, int pageNum, int pageSize) { - try { - ArticleSearcher searcher = ArticleSearcherFactory.getSearcher(); - Page
page = searcher.search(queryString, pageNum, pageSize); - if (page != null) { - return page; - } - } catch (Exception ex) { - LogKit.error(ex.toString(), ex); - } - return new Page<>(new ArrayList<>(), pageNum, pageSize, 0, 0); - } - - @Override - @Cacheable(name = "articles") - public Page
searchIndb(String queryString, int pageNum, int pageSize) { - Columns columns = Columns.create("status", Article.STATUS_NORMAL) - .likeAppendPercent("title", queryString); - return joinUserInfo(paginateByColumns(pageNum, pageSize, columns, "order_number desc,id desc")); - } - - - private Page
joinUserInfo(Page
page) { - userService.join(page, "user_id"); - return page; - } - - private List
joinUserInfo(List
list) { - userService.join(list, "user_id"); - return list; - } - - private Article joinUserInfo(Article article) { - userService.join(article, "user_id"); - return article; - } - - @Override - @CacheEvict(name = "articles", key = "*") - public boolean doChangeStatus(long id, String status) { - Article article = findById(id); - article.setStatus(status); - return update(article); - } - - @Override - public Long findCountByStatus(String status) { - return DAO.findCountByColumn(Column.create("status", status)); - } - - @Override - public Article findById(Object id) { - return joinUserInfo(super.findById(id)); - } - - @Override - public Article findByTitle(String title) { - return joinUserInfo(DAO.findFirstByColumn(Column.create("title", title))); - } - - @Override - public Article findFirstBySlug(String slug) { - return joinUserInfo(DAO.findFirstByColumn(Column.create("slug", slug))); - } - - - @Override - public Article findNextById(long id) { - Columns columns = Columns.create(); - columns.add(Column.create("id", id, Column.LOGIC_GT)); - columns.add(Column.create("status", Article.STATUS_NORMAL)); - return joinUserInfo(DAO.findFirstByColumns(columns)); - } - - @Override - public Article findPreviousById(long id) { - Columns columns = Columns.create(); - columns.add(Column.create("id", id, Column.LOGIC_LT)); - columns.add(Column.create("status", Article.STATUS_NORMAL)); - return joinUserInfo(DAO.findFirstByColumns(columns, "id desc")); - } - - @Override - @Cacheable(name = "articles", key = "#(columns.cacheKey)-#(orderBy)-#(count)", liveSeconds = 60 * 60) - public List
findListByColumns(Columns columns, String orderBy, Integer count) { - return joinUserInfo(DAO.findListByColumns(columns, orderBy, count)); - } - - @Override - @Cacheable(name = "articles", key = "findListByCategoryId:#(categoryId)-#(hasThumbnail)-#(orderBy)-#(count)", liveSeconds = 60 * 60) - public List
findListByCategoryId(long categoryId, Boolean hasThumbnail, String orderBy, Integer count) { - - Columns columns = Columns - .create("m.category_id", categoryId) - .eq("article.status", Article.STATUS_NORMAL) - .isNotNullIf("article.thumbnail", hasThumbnail != null && hasThumbnail) - .isNullIf("article.thumbnail", hasThumbnail != null && !hasThumbnail); - - List
articles = DAO.leftJoin("article_category_mapping").as("m") - .on("article.id = m.`article_id`") - .findListByColumns(columns, orderBy, count); - - return joinUserInfo(articles); - } - - @Override - @Cacheable(name = "articles") - public List
findRelevantListByArticleId(long articleId, String status, Integer count) { - - List tags = categoryService.findListByArticleId(articleId, ArticleCategory.TYPE_TAG); - if (tags == null || tags.isEmpty()) { - return null; - } - - List tagIds = tags.stream().map(category -> category.getId()).collect(Collectors.toList()); - - Columns columns = Columns.create(); - columns.in("m.category_id", tagIds.toArray()); - columns.ne("article.id", articleId); - columns.eq("article.status", status); - - List
articles = DAO.leftJoin("article_category_mapping").as("m") - .on("article.id = m.`article_id`") - .findListByColumns(columns, count); - - return joinUserInfo(articles); - } - - - @Override - public Object save(Article model) { - Object id = super.save(model); - if (id != null && model.isNormal()) { - ArticleSearcherFactory.getSearcher().addArticle(model); - ArticleSitemapManager.me().rebuild(); - SeoManager.me().ping(model.toPingData()); - SeoManager.me().baiduPush(model.getUrl()); - } - return id; - } - - @Override - public boolean update(Article model) { - boolean success = super.update(model); - if (success) { - - ArticleSitemapManager.me().rebuild(); - - if (model.isNormal()) { - ArticleSearcherFactory.getSearcher().updateArticle(model); - SeoManager.me().ping(model.toPingData()); - SeoManager.me().baiduUpdate(model.getUrl()); - } else { - ArticleSearcherFactory.getSearcher().deleteArticle(model.getId()); - } - } - return success; - } - - - @Override - public boolean delete(Article model) { - boolean success = super.delete(model); - if (success) { - ArticleSearcherFactory.getSearcher().deleteArticle(model.getId()); - ArticleSitemapManager.me().rebuild(); - } - return success; - } - - @Override - @CacheEvict(name = "articles", key = "*") - public void removeCacheById(Object id) { - DAO.deleteIdCacheById(id); - } - - - @Override - - @CachesEvict({ - @CacheEvict(name = "articles", key = "*"), - @CacheEvict(name = "article-category", key = "#(articleId)"), - }) - public void doUpdateCategorys(long articleId, Long[] categoryIds) { - - Db.tx(() -> { - Db.update("delete from article_category_mapping where article_id = ?", articleId); - - if (categoryIds != null && categoryIds.length > 0) { - List records = new ArrayList<>(); - for (long categoryId : categoryIds) { - Record record = new Record(); - record.set("article_id", articleId); - record.set("category_id", categoryId); - records.add(record); - } - Db.batchSave("article_category_mapping", records, records.size()); - } - - return true; - }); - } - - @Override - public boolean deleteById(Object id) { - - //搜索搜索引擎的内容 - ArticleSearcherFactory.getSearcher().deleteArticle(id); - - return Db.tx(() -> { - boolean delOk = ArticleServiceProvider.super.deleteById(id); - if (delOk == false) { - return false; - } - - //删除文章的管理分类 - List records = Db.find("select * from article_category_mapping where article_id = ? ", id); - if (records != null && !records.isEmpty()) { - //更新文章数量 - Db.update("delete from article_category_mapping where article_id = ?", id); - records.forEach(record -> categoryService.doUpdateArticleCount(record.get("category_id"))); - } - - - //删除文章的所有评论 - commentService.deleteByArticleId(id); - - return true; - }); - } - - - @Override - @CachesEvict({ - @CacheEvict(name = "articles", key = "*"), - @CacheEvict(name = "article-category", key = "#(id)", unless = "id == null"), - }) - public void shouldUpdateCache(int action, Model model, Object id) { - super.shouldUpdateCache(action, model, id); - } +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.service.provider; + +import com.jfinal.aop.Inject; +import com.jfinal.kit.LogKit; +import com.jfinal.plugin.activerecord.Db; +import com.jfinal.plugin.activerecord.Model; +import com.jfinal.plugin.activerecord.Page; +import com.jfinal.plugin.activerecord.Record; +import io.jboot.aop.annotation.Bean; +import io.jboot.components.cache.annotation.CacheEvict; +import io.jboot.components.cache.annotation.Cacheable; +import io.jboot.components.cache.annotation.CachesEvict; +import io.jboot.db.model.Column; +import io.jboot.db.model.Columns; +import io.jboot.service.JbootServiceBase; +import io.jboot.utils.StrUtil; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.model.ArticleCategory; +import io.jpress.module.article.service.ArticleCategoryService; +import io.jpress.module.article.service.ArticleCommentService; +import io.jpress.module.article.service.ArticleService; +import io.jpress.module.article.service.search.ArticleSearcher; +import io.jpress.module.article.service.search.ArticleSearcherFactory; +import io.jpress.module.article.service.sitemap.ArticleSitemapManager; +import io.jpress.module.article.service.task.ArticleCommentsCountUpdateTask; +import io.jpress.module.article.service.task.ArticleViewsCountUpdateTask; +import io.jpress.service.UserService; +import io.jpress.web.seoping.SeoManager; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Bean +public class ArticleServiceProvider extends JbootServiceBase

implements ArticleService { + + @Inject + private ArticleCategoryService categoryService; + + @Inject + private UserService userService; + + @Inject + private ArticleCommentService commentService; + + private static final String DEFAULT_ORDER_BY = "order_number desc,id desc"; + + @Override + public boolean deleteByIds(Object... ids) { + for (Object id : ids) { + deleteById(id); + } + return true; + } + + + @Override + public void doUpdateCommentCount(long articleId) { + Article article = findById(articleId); + if (article == null) { + return; + } + + long count = commentService.findCountByArticleId(articleId); + article.setCommentCount(count); + article.update(); + } + + @Override + public Page
_paginateByStatus(int page, int pagesize, String title, Long categoryId, String status) { + + return _paginateByBaseColumns(page + , pagesize + , title + , categoryId + , Columns.create("article.status", status)); + } + + @Override + public Page
_paginateWithoutTrash(int page, int pagesize, String title, Long categoryId) { + + return _paginateByBaseColumns(page + , pagesize + , title + , categoryId + , Columns.create().ne("article.status", Article.STATUS_TRASH)); + } + + + public Page
_paginateByBaseColumns(int page, int pagesize, String title, Long categoryId, Columns baseColumns) { + + Columns columns = baseColumns; + columns.add("m.category_id", categoryId); + columns.likeAppendPercent("article.title", title); + + Page
dataPage = DAO.leftJoinIf("article_category_mapping", categoryId != null) + .as("m") + .on("article.id = m.article_id") + .paginateByColumns(page, pagesize, columns, "id desc"); + + + return joinUserInfo(dataPage); + } + + @Override + public Page
_paginateByUserId(int page, int pagesize, Long userId) { + return DAO.paginateByColumn(page, pagesize, Column.create("user_id", userId), DEFAULT_ORDER_BY); + } + + @Override + @Cacheable(name = "articles") + public Page
paginateInNormal(int page, int pagesize) { + return paginateInNormal(page, pagesize, "id desc"); + } + + @Override + @Cacheable(name = "articles") + public Page
paginateInNormal(int page, int pagesize, String orderBy) { + orderBy = StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY); + Columns columns = new Columns(); + columns.add("status", Article.STATUS_NORMAL); + Page
dataPage = DAO.paginateByColumns(page, pagesize, columns, orderBy); + return joinUserInfo(dataPage); + } + + @Override + @Cacheable(name = "articles") + public Page
paginateInNormalNotHidden(int page, int pagesize, String orderBy) { + orderBy = StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY); + Columns columns = new Columns(); + columns.add("status", Article.STATUS_NORMAL); + columns.ne("flag", "hidden"); + columns.or(); + columns.isNull("flag"); + Page
dataPage = DAO.paginateByColumns(page, pagesize, columns, orderBy); + return joinUserInfo(dataPage); + } + + @Override + @Cacheable(name = "articles") + public Page
paginateByCategoryIdInNormalNotHidden(int page, int pagesize, long categoryId, String orderBy) { + + Columns columns = new Columns(); + columns.add("m.category_id", categoryId); + columns.add("article.status", Article.STATUS_NORMAL); + columns.ne("flag", "hidden"); + columns.or(); + columns.isNull("flag"); + + Page
dataPage = DAO.leftJoin("article_category_mapping") + .as("m").on("article.id=m.`article_id`") + .paginateByColumns(page, pagesize, columns, StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY)); + return joinUserInfo(dataPage); + } + + + @Override + @Cacheable(name = "articles") + public Page
paginateByCategoryIdInNormal(int page, int pagesize, long categoryId, String orderBy) { + + Columns columns = new Columns(); + columns.add("m.category_id", categoryId); + columns.add("article.status", Article.STATUS_NORMAL); + + Page
dataPage = DAO.leftJoin("article_category_mapping") + .as("m").on("article.id=m.`article_id`") + .paginateByColumns(page, pagesize, columns, StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY)); + return joinUserInfo(dataPage); + } + + + + @Override + public void doIncArticleViewCount(long articleId) { + ArticleViewsCountUpdateTask.recordCount(articleId); + } + + @Override + public void doIncArticleCommentCount(long articleId) { + ArticleCommentsCountUpdateTask.recordCount(articleId); + } + + + @Override + public Page
search(String queryString, int pageNum, int pageSize) { + try { + ArticleSearcher searcher = ArticleSearcherFactory.getSearcher(); + Page
page = searcher.search(queryString, pageNum, pageSize); + if (page != null) { + return page; + } + } catch (Exception ex) { + LogKit.error(ex.toString(), ex); + } + return new Page<>(new ArrayList<>(), pageNum, pageSize, 0, 0); + } + + @Override + @Cacheable(name = "articles") + public Page
searchIndb(String queryString, int pageNum, int pageSize) { + Columns columns = Columns.create("status", Article.STATUS_NORMAL) + .likeAppendPercent("title", queryString); + return joinUserInfo(paginateByColumns(pageNum, pageSize, columns, "order_number desc,id desc")); + } + + + private Page
joinUserInfo(Page
page) { + userService.join(page, "user_id"); + return page; + } + + private List
joinUserInfo(List
list) { + userService.join(list, "user_id"); + return list; + } + + private Article joinUserInfo(Article article) { + userService.join(article, "user_id"); + return article; + } + + @Override + @CacheEvict(name = "articles", key = "*") + public boolean doChangeStatus(long id, String status) { + Article article = findById(id); + article.setStatus(status); + return update(article); + } + + @Override + public Long findCountByStatus(String status) { + return DAO.findCountByColumn(Column.create("status", status)); + } + + @Override + public Article findById(Object id) { + return joinUserInfo(super.findById(id)); + } + + @Override + public Article findByTitle(String title) { + return joinUserInfo(DAO.findFirstByColumn(Column.create("title", title))); + } + + @Override + public Article findFirstBySlug(String slug) { + return joinUserInfo(DAO.findFirstByColumn(Column.create("slug", slug))); + } + + + @Override + public Article findNextById(long id) { + Columns columns = Columns.create(); + columns.add(Column.create("id", id, Column.LOGIC_GT)); + columns.add(Column.create("status", Article.STATUS_NORMAL)); + return joinUserInfo(DAO.findFirstByColumns(columns)); + } + + @Override + public Article findPreviousById(long id) { + Columns columns = Columns.create(); + columns.add(Column.create("id", id, Column.LOGIC_LT)); + columns.add(Column.create("status", Article.STATUS_NORMAL)); + return joinUserInfo(DAO.findFirstByColumns(columns, "id desc")); + } + + @Override + public Article findNextByIdNotHidden(long id) { + Columns columns = Columns.create(); + columns.add(Column.create("id", id, Column.LOGIC_GT)); + columns.add(Column.create("status", Article.STATUS_NORMAL)); + columns.ne("flag", "hidden"); + columns.or(); + columns.isNull("flag"); + return joinUserInfo(DAO.findFirstByColumns(columns)); + } + + @Override + public Article findPreviousByIdNotHidden(long id) { + Columns columns = Columns.create(); + columns.add(Column.create("id", id, Column.LOGIC_LT)); + columns.add(Column.create("status", Article.STATUS_NORMAL)); + columns.ne("flag", "hidden"); + columns.or(); + columns.isNull("flag"); + return joinUserInfo(DAO.findFirstByColumns(columns, "id desc")); + } + + @Override + @Cacheable(name = "articles", key = "#(columns.cacheKey)-#(orderBy)-#(count)", liveSeconds = 60 * 60) + public List
findListByColumns(Columns columns, String orderBy, Integer count) { + return joinUserInfo(DAO.findListByColumns(columns, orderBy, count)); + } + + @Override + @Cacheable(name = "articles", key = "findListByCategoryId:#(categoryId)-#(hasThumbnail)-#(orderBy)-#(count)", liveSeconds = 60 * 60) + public List
findListByCategoryId(long categoryId, Boolean hasThumbnail, String orderBy, Integer count) { + + Columns columns = Columns + .create("m.category_id", categoryId) + .eq("article.status", Article.STATUS_NORMAL) + .isNotNullIf("article.thumbnail", hasThumbnail != null && hasThumbnail) + .isNullIf("article.thumbnail", hasThumbnail != null && !hasThumbnail); + + List
articles = DAO.leftJoin("article_category_mapping").as("m") + .on("article.id = m.`article_id`") + .findListByColumns(columns, orderBy, count); + + return joinUserInfo(articles); + } + + @Override + @Cacheable(name = "articles", key = "findListByCategoryIdNotHidden:#(categoryId)-#(hasThumbnail)-#(orderBy)-#(count)", liveSeconds = 60 * 60) + public List
findListByCategoryIdNotHidden(long categoryId, Boolean hasThumbnail, String orderBy, Integer count) { + + Columns columns = Columns + .create("m.category_id", categoryId) + .eq("article.status", Article.STATUS_NORMAL) + .isNotNullIf("article.thumbnail", hasThumbnail != null && hasThumbnail) + .isNullIf("article.thumbnail", hasThumbnail != null && !hasThumbnail) + .ne("flag", "hidden") + .or() + .isNull("flag"); + + List
articles = DAO.leftJoin("article_category_mapping").as("m") + .on("article.id = m.`article_id`") + .findListByColumns(columns, orderBy, count); + + return joinUserInfo(articles); + } + + @Override + @Cacheable(name = "articles") + public List
findRelevantListByArticleIdNotHidden(long articleId, String status, Integer count){ + List tags = categoryService.findListByArticleId(articleId, ArticleCategory.TYPE_TAG); + if (tags == null || tags.isEmpty()) { + return null; + } + + List tagIds = tags.stream().map(category -> category.getId()).collect(Collectors.toList()); + + Columns columns = Columns.create(); + columns.in("m.category_id", tagIds.toArray()); + columns.ne("article.id", articleId); + columns.eq("article.status", status); + columns.ne("flag", "hidden"); + columns.or(); + columns.isNull("flag"); + + List
articles = DAO.leftJoin("article_category_mapping").as("m") + .on("article.id = m.`article_id`") + .findListByColumns(columns, count); + + return joinUserInfo(articles); + } + + @Override + @Cacheable(name = "articles") + public List
findRelevantListByArticleId(long articleId, String status, Integer count) { + + List tags = categoryService.findListByArticleId(articleId, ArticleCategory.TYPE_TAG); + if (tags == null || tags.isEmpty()) { + return null; + } + + List tagIds = tags.stream().map(category -> category.getId()).collect(Collectors.toList()); + + Columns columns = Columns.create(); + columns.in("m.category_id", tagIds.toArray()); + columns.ne("article.id", articleId); + columns.eq("article.status", status); + + List
articles = DAO.leftJoin("article_category_mapping").as("m") + .on("article.id = m.`article_id`") + .findListByColumns(columns, count); + + return joinUserInfo(articles); + } + + + @Override + public Object save(Article model) { + Object id = super.save(model); + if (id != null && model.isNormal()) { + ArticleSearcherFactory.getSearcher().addArticle(model); + ArticleSitemapManager.me().rebuild(); + SeoManager.me().ping(model.toPingData()); + SeoManager.me().baiduPush(model.getUrl()); + } + return id; + } + + @Override + public boolean update(Article model) { + boolean success = super.update(model); + if (success) { + + ArticleSitemapManager.me().rebuild(); + + if (model.isNormal()) { + ArticleSearcherFactory.getSearcher().updateArticle(model); + SeoManager.me().ping(model.toPingData()); + SeoManager.me().baiduUpdate(model.getUrl()); + } else { + ArticleSearcherFactory.getSearcher().deleteArticle(model.getId()); + } + } + return success; + } + + + @Override + public boolean delete(Article model) { + boolean success = super.delete(model); + if (success) { + ArticleSearcherFactory.getSearcher().deleteArticle(model.getId()); + ArticleSitemapManager.me().rebuild(); + } + return success; + } + + @Override + @CacheEvict(name = "articles", key = "*") + public void removeCacheById(Object id) { + DAO.deleteIdCacheById(id); + } + + + @Override + + @CachesEvict({ + @CacheEvict(name = "articles", key = "*"), + @CacheEvict(name = "article-category", key = "#(articleId)"), + }) + public void doUpdateCategorys(long articleId, Long[] categoryIds) { + + Db.tx(() -> { + Db.update("delete from article_category_mapping where article_id = ?", articleId); + + if (categoryIds != null && categoryIds.length > 0) { + List records = new ArrayList<>(); + for (long categoryId : categoryIds) { + Record record = new Record(); + record.set("article_id", articleId); + record.set("category_id", categoryId); + records.add(record); + } + Db.batchSave("article_category_mapping", records, records.size()); + } + + return true; + }); + } + + @Override + public boolean deleteById(Object id) { + + //搜索搜索引擎的内容 + ArticleSearcherFactory.getSearcher().deleteArticle(id); + + return Db.tx(() -> { + boolean delOk = ArticleServiceProvider.super.deleteById(id); + if (delOk == false) { + return false; + } + + //删除文章的管理分类 + List records = Db.find("select * from article_category_mapping where article_id = ? ", id); + if (records != null && !records.isEmpty()) { + //更新文章数量 + Db.update("delete from article_category_mapping where article_id = ?", id); + records.forEach(record -> categoryService.doUpdateArticleCount(record.get("category_id"))); + } + + + //删除文章的所有评论 + commentService.deleteByArticleId(id); + + return true; + }); + } + + + @Override + @CachesEvict({ + @CacheEvict(name = "articles", key = "*"), + @CacheEvict(name = "article-category", key = "#(id)", unless = "id == null"), + }) + public void shouldUpdateCache(int action, Model model, Object id) { + super.shouldUpdateCache(action, model, id); + } } \ No newline at end of file diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleDirective.java index 12b4a9191..6e9a561b8 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleDirective.java @@ -1,63 +1,65 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.utils.StrUtil; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("article") -public class ArticleDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - String idOrSlug = getPara(0, scope); - Article article = getArticle(idOrSlug); - - if (article == null) { - return; - } - - scope.setLocal("article", article); - renderBody(env, scope, writer); - } - - private Article getArticle(String idOrSlug) { - return StrUtil.isNumeric(idOrSlug) - ? service.findById(idOrSlug) - : service.findFirstBySlug(idOrSlug); - } - - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.utils.StrUtil; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("article") +public class ArticleDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + String idOrSlug = getPara(0, scope); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + Article article = getArticle(idOrSlug); + + // 当设置了hidden的flag时,文章不可见 + if (article == null || (hiddenFlag && "hidden".equals(article.getFlag()))) { + return; + } + + scope.setLocal("article", article); + renderBody(env, scope, writer); + } + + private Article getArticle(String idOrSlug) { + return StrUtil.isNumeric(idOrSlug) + ? service.findById(idOrSlug) + : service.findFirstBySlug(idOrSlug); + } + + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlePageDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlePageDirective.java index 354ee9249..e74c63999 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlePageDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlePageDirective.java @@ -1,108 +1,110 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.core.Controller; -import com.jfinal.core.JFinal; -import com.jfinal.plugin.activerecord.Page; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.web.controller.JbootControllerContext; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jboot.web.directive.base.PaginateDirectiveBase; -import io.jpress.JPressOptions; -import io.jpress.commons.directive.DirectveKit; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.model.ArticleCategory; -import io.jpress.module.article.service.ArticleService; - -import javax.servlet.http.HttpServletRequest; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("articlePage") -public class ArticlePageDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - Controller controller = JbootControllerContext.get(); - - int page = controller.getParaToInt(1, 1); - int pageSize = getParaToInt("pageSize", scope, 10); - String orderBy = getPara("orderBy", scope, "id desc"); - - // 可以指定当前的分类ID - Long categoryId = getParaToLong("categoryId", scope, 0L); - ArticleCategory category = controller.getAttr("category"); - - if (categoryId == 0 && category != null) { - categoryId = category.getId(); - } - - Page

articlePage = categoryId == 0 - ? service.paginateInNormal(page, pageSize, orderBy) - : service.paginateByCategoryIdInNormal(page, pageSize, categoryId, orderBy); - - scope.setGlobal("articlePage", articlePage); - renderBody(env, scope, writer); - } - - @Override - public boolean hasEnd() { - return true; - } - - - @JFinalDirective("articlePaginate") - public static class TemplatePaginateDirective extends PaginateDirectiveBase { - - @Override - protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) { - HttpServletRequest request = JbootControllerContext.get().getRequest(); - String url = request.getRequestURI(); - String contextPath = JFinal.me().getContextPath(); - - boolean firstGotoIndex = getPara("firstGotoIndex", scope, false); - - if (pageNumber == 1 && firstGotoIndex) { - return contextPath + "/"; - } - - // 如果当前页面是首页的话 - // 需要改变url的值,因为 上一页或下一页是通过当前的url解析出来的 - if (url.equals(contextPath + "/")) { - url = contextPath + "/article/category/index" - + JPressOptions.getAppUrlSuffix(); - } - return DirectveKit.replacePageNumber(url, pageNumber); - } - - @Override - protected Page getPage(Env env, Scope scope, Writer writer) { - return (Page) scope.get("articlePage"); - } - - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.core.Controller; +import com.jfinal.core.JFinal; +import com.jfinal.plugin.activerecord.Page; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jboot.web.directive.base.PaginateDirectiveBase; +import io.jpress.JPressOptions; +import io.jpress.commons.directive.DirectveKit; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.model.ArticleCategory; +import io.jpress.module.article.service.ArticleService; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("articlePage") +public class ArticlePageDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + Controller controller = JbootControllerContext.get(); + + int page = controller.getParaToInt(1, 1); + int pageSize = getParaToInt("pageSize", scope, 10); + // 当flag设置为hidden,文章列表页不可见,通过hidden参数触发 + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + String orderBy = getPara("orderBy", scope, "id desc"); + + // 可以指定当前的分类ID + Long categoryId = getParaToLong("categoryId", scope, 0L); + ArticleCategory category = controller.getAttr("category"); + + if (categoryId == 0 && category != null) { + categoryId = category.getId(); + } + + Page

articlePage = categoryId == 0 + ? (hiddenFlag ? service.paginateInNormalNotHidden(page, pageSize, orderBy) : service.paginateInNormal(page, pageSize, orderBy)) + : (hiddenFlag ? service.paginateByCategoryIdInNormalNotHidden(page, pageSize, categoryId, orderBy) : service.paginateByCategoryIdInNormal(page, pageSize, categoryId, orderBy)); + + scope.setGlobal("articlePage", articlePage); + renderBody(env, scope, writer); + } + + @Override + public boolean hasEnd() { + return true; + } + + + @JFinalDirective("articlePaginate") + public static class TemplatePaginateDirective extends PaginateDirectiveBase { + + @Override + protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) { + HttpServletRequest request = JbootControllerContext.get().getRequest(); + String url = request.getRequestURI(); + String contextPath = JFinal.me().getContextPath(); + + boolean firstGotoIndex = getPara("firstGotoIndex", scope, false); + + if (pageNumber == 1 && firstGotoIndex) { + return contextPath + "/"; + } + + // 如果当前页面是首页的话 + // 需要改变url的值,因为 上一页或下一页是通过当前的url解析出来的 + if (url.equals(contextPath + "/")) { + url = contextPath + "/article/category/index" + + JPressOptions.getAppUrlSuffix(); + } + return DirectveKit.replacePageNumber(url, pageNumber); + } + + @Override + protected Page getPage(Env env, Scope scope, Writer writer) { + return (Page) scope.get("articlePage"); + } + + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java index 3849cc989..3c8a695f0 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java @@ -1,77 +1,93 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.core.Controller; -import com.jfinal.plugin.activerecord.Page; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.utils.StrUtil; -import io.jboot.web.controller.JbootControllerContext; -import io.jboot.web.directive.JbootPaginateDirective; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("articleSearchPage") -public class ArticleSearchPageDirective extends JbootDirectiveBase { - - @Inject - private ArticleService articleService; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - Controller controller = JbootControllerContext.get(); - - String keyword = controller.getAttr("keyword"); - int page = controller.getAttr("page"); - int pageSize = getParaToInt("pageSize", scope, 10); - - Page

dataPage = StrUtil.isNotBlank(keyword) - ? articleService.search(keyword, page, pageSize) - : null; - - if (dataPage != null) { - scope.setGlobal("articlePage", dataPage); - } - - //需要页面自行判断 articlePage 是否为空 - renderBody(env, scope, writer); - } - - @Override - public boolean hasEnd() { - return true; - } - - - @JFinalDirective("articleSearchPaginate") - public static class SearchPaginateDirective extends JbootPaginateDirective { - @Override - protected Page getPage(Env env, Scope scope, Writer writer) { - return (Page) scope.get("articlePage"); - } - - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import java.util.List; + +import com.jfinal.aop.Inject; +import com.jfinal.core.Controller; +import com.jfinal.plugin.activerecord.Page; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.utils.StrUtil; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.directive.JbootPaginateDirective; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("articleSearchPage") +public class ArticleSearchPageDirective extends JbootDirectiveBase { + + @Inject + private ArticleService articleService; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + Controller controller = JbootControllerContext.get(); + + String keyword = controller.getAttr("keyword"); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + int page = controller.getAttr("page"); + int pageSize = getParaToInt("pageSize", scope, 10); + + Page

dataPage = StrUtil.isNotBlank(keyword) + ? articleService.search(keyword, page, pageSize) + : null; + + if(hiddenFlag && null != dataPage) { + List
articles = dataPage.getList(); + if(null != articles) { + for(Article article: articles ) { + if("hidden".equals(article.getFlag())) { + // 通过linkTo实现无法查看 + article.setLinkTo("##"); + article.setContent("作者已经隐藏了此文章,你无权查看!"); + } + } + } + } + + if (dataPage != null) { + scope.setGlobal("articlePage", dataPage); + } + + //需要页面自行判断 articlePage 是否为空 + renderBody(env, scope, writer); + } + + @Override + public boolean hasEnd() { + return true; + } + + + @JFinalDirective("articleSearchPaginate") + public static class SearchPaginateDirective extends JbootPaginateDirective { + @Override + protected Page getPage(Env env, Scope scope, Writer writer) { + return (Page) scope.get("articlePage"); + } + + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java index 75459dbb9..23d625f1f 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java @@ -1,80 +1,87 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.db.model.Columns; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - -import java.util.List; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("articles") -public class ArticlesDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - String flag = getPara("flag", scope); - String style = getPara("style", scope); - Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); - String orderBy = getPara("orderBy", scope, "id desc"); - int count = getParaToInt("count", scope, 10); - - - Columns columns = Columns.create(); - - columns.add("flag", flag); - columns.add("style", style); - columns.add("status", Article.STATUS_NORMAL); - - if (hasThumbnail != null) { - if (hasThumbnail) { - columns.isNotNull("thumbnail"); - } else { - columns.isNull("thumbnail"); - } - } - - List

articles = service.findListByColumns(columns, orderBy, count); - - if (articles == null || articles.isEmpty()) { - return; - } - - scope.setLocal("articles", articles); - renderBody(env, scope, writer); - } - - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.db.model.Columns; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + +import java.util.List; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("articles") +public class ArticlesDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + String flag = getPara("flag", scope); + String style = getPara("style", scope); + Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); + String orderBy = getPara("orderBy", scope, "id desc"); + // 隐藏部分不想展示的文章 + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + int count = getParaToInt("count", scope, 10); + + + Columns columns = Columns.create(); + + columns.add("flag", flag); + columns.add("style", style); + columns.add("status", Article.STATUS_NORMAL); + + // 当flag设置为articleSoftHidden时,文章列表页不可见,当flag设置为articleHardHidden时,文章列表页,文章内容页不可见 + if(hiddenFlag) { + columns.ne("flag", "hidden"); + } + + if (hasThumbnail != null) { + if (hasThumbnail) { + columns.isNotNull("thumbnail"); + } else { + columns.isNull("thumbnail"); + } + } + + List

articles = service.findListByColumns(columns, orderBy, count); + + if (articles == null || articles.isEmpty()) { + return; + } + + scope.setLocal("articles", articles); + renderBody(env, scope, writer); + } + + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/CategoryArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/CategoryArticlesDirective.java index 27bf4d801..06a8f1a68 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/CategoryArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/CategoryArticlesDirective.java @@ -1,84 +1,85 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.utils.StrUtil; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.model.ArticleCategory; -import io.jpress.module.article.service.ArticleCategoryService; -import io.jpress.module.article.service.ArticleService; - -import java.util.List; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("categoryArticles") -public class CategoryArticlesDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Inject - private ArticleCategoryService categoryService; - - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - Long categoryId = getParaToLong("categoryId", scope); - String flag = getPara("categoryFlag", scope); - - if (StrUtil.isBlank(flag) && categoryId == null) { - throw new IllegalArgumentException("#categoryArticles(categoryFlag=xxx,categoryId=xxx) is error, " + - "categoryFlag or categoryId must not be empty. " + getLocation()); - } - - Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); - String orderBy = getPara("orderBy", scope, "order_number desc,id desc"); - int count = getParaToInt("count", scope, 10); - - ArticleCategory category = categoryId != null - ? categoryService.findById(categoryId) - : categoryService.findFirstByFlag(flag); - if (category == null) { - return; - } - - scope.setLocal("category", category); - - List

articles = service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count); - if (articles == null || articles.isEmpty()) { - return; - } - - scope.setLocal("articles", articles); - renderBody(env, scope, writer); - } - - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.utils.StrUtil; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.model.ArticleCategory; +import io.jpress.module.article.service.ArticleCategoryService; +import io.jpress.module.article.service.ArticleService; + +import java.util.List; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("categoryArticles") +public class CategoryArticlesDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Inject + private ArticleCategoryService categoryService; + + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + Long categoryId = getParaToLong("categoryId", scope); + String flag = getPara("categoryFlag", scope); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + + if (StrUtil.isBlank(flag) && categoryId == null) { + throw new IllegalArgumentException("#categoryArticles(categoryFlag=xxx,categoryId=xxx) is error, " + + "categoryFlag or categoryId must not be empty. " + getLocation()); + } + + Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); + String orderBy = getPara("orderBy", scope, "order_number desc,id desc"); + int count = getParaToInt("count", scope, 10); + + ArticleCategory category = categoryId != null + ? categoryService.findById(categoryId) + : categoryService.findFirstByFlag(flag); + if (category == null) { + return; + } + + scope.setLocal("category", category); + + List

articles = hiddenFlag ? service.findListByCategoryIdNotHidden(categoryId, hasThumbnail, orderBy, count) : service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count); + if (articles == null || articles.isEmpty()) { + return; + } + + scope.setLocal("articles", articles); + renderBody(env, scope, writer); + } + + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/NextArticleDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/NextArticleDirective.java index 70cb33335..263a91458 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/NextArticleDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/NextArticleDirective.java @@ -1,56 +1,57 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.web.controller.JbootControllerContext; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("nextArticle") -public class NextArticleDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - Article article = JbootControllerContext.get().getAttr("article"); - - Article nextArticle = service.findNextById(article.getId()); - if (nextArticle == null) { - return; - } - - scope.setLocal("next", nextArticle); - renderBody(env, scope, writer); - } - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("nextArticle") +public class NextArticleDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + Article article = JbootControllerContext.get().getAttr("article"); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + + Article nextArticle = hiddenFlag ? service.findNextByIdNotHidden(article.getId()) : service.findNextById(article.getId()); + if (nextArticle == null) { + return; + } + + scope.setLocal("next", nextArticle); + renderBody(env, scope, writer); + } + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/PreviousArticleDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/PreviousArticleDirective.java index c83df0d45..3c0ed31be 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/PreviousArticleDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/PreviousArticleDirective.java @@ -1,56 +1,57 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.web.controller.JbootControllerContext; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("previousArticle") -public class PreviousArticleDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - Article article = JbootControllerContext.get().getAttr("article"); - - Article previousArticle = service.findPreviousById(article.getId()); - if (previousArticle == null) { - return; - } - - scope.setLocal("previous", previousArticle); - renderBody(env, scope, writer); - } - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("previousArticle") +public class PreviousArticleDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + Article article = JbootControllerContext.get().getAttr("article"); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + + Article previousArticle = hiddenFlag ? service.findPreviousByIdNotHidden(article.getId()) : service.findPreviousById(article.getId()); + if (previousArticle == null) { + return; + } + + scope.setLocal("previous", previousArticle); + renderBody(env, scope, writer); + } + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/RelevantArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/RelevantArticlesDirective.java index 4f19dff89..8623f3b0c 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/RelevantArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/RelevantArticlesDirective.java @@ -1,68 +1,70 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleCategoryService; -import io.jpress.module.article.service.ArticleService; - -import java.util.List; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - * @Title: 相关文章 - */ -@JFinalDirective("relevantArticles") -public class RelevantArticlesDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Inject - private ArticleCategoryService categoryService; - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - Article article = getPara(0, scope); - if (article == null) { - throw new IllegalArgumentException("#relevantArticles(...) argument must not be null or empty." + getLocation()); - } - - //默认值 3 - int count = getParaToInt(1, scope, 3); - - List

relevantArticles = service.findRelevantListByArticleId(article.getId(), Article.STATUS_NORMAL, count); - - if (relevantArticles == null || relevantArticles.isEmpty()) { - return; - } - - scope.setLocal("relevantArticles", relevantArticles); - renderBody(env, scope, writer); - } - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleCategoryService; +import io.jpress.module.article.service.ArticleService; + +import java.util.List; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + * @Title: 相关文章 + */ +@JFinalDirective("relevantArticles") +public class RelevantArticlesDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Inject + private ArticleCategoryService categoryService; + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + Article article = getPara(0, scope); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + + if (article == null) { + throw new IllegalArgumentException("#relevantArticles(...) argument must not be null or empty." + getLocation()); + } + + //默认值 3 + int count = getParaToInt(1, scope, 3); + + List

relevantArticles = hiddenFlag ? service.findRelevantListByArticleIdNotHidden(article.getId(), Article.STATUS_NORMAL, count) : service.findRelevantListByArticleId(article.getId(), Article.STATUS_NORMAL, count); + + if (relevantArticles == null || relevantArticles.isEmpty()) { + return; + } + + scope.setLocal("relevantArticles", relevantArticles); + renderBody(env, scope, writer); + } + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/TagArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/TagArticlesDirective.java index 399574998..32a2dfc24 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/TagArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/TagArticlesDirective.java @@ -1,82 +1,83 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.utils.StrUtil; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.model.ArticleCategory; -import io.jpress.module.article.service.ArticleCategoryService; -import io.jpress.module.article.service.ArticleService; - -import java.util.List; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("tagArticles") -public class TagArticlesDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - @Inject - private ArticleCategoryService categoryService; - - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - String tag = getPara("tag", scope); - - if (StrUtil.isBlank(tag)) { - throw new IllegalArgumentException("#tagArticles() args is error, tag must not be empty." + getLocation()); - } - - - Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); - String orderBy = getPara("orderBy", scope, "order_number desc,id desc"); - int count = getParaToInt("count", scope, 10); - - ArticleCategory category = categoryService.findFirstByTypeAndSlug(ArticleCategory.TYPE_TAG, tag); - if (category == null) { - return; - } - - scope.setLocal("tag", category); - - List

articles = service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count); - - if (articles == null || articles.isEmpty()) { - return; - } - - scope.setLocal("articles", articles); - renderBody(env, scope, writer); - } - - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.utils.StrUtil; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.model.ArticleCategory; +import io.jpress.module.article.service.ArticleCategoryService; +import io.jpress.module.article.service.ArticleService; + +import java.util.List; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("tagArticles") +public class TagArticlesDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + @Inject + private ArticleCategoryService categoryService; + + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + String tag = getPara("tag", scope); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + + if (StrUtil.isBlank(tag)) { + throw new IllegalArgumentException("#tagArticles() args is error, tag must not be empty." + getLocation()); + } + + + Boolean hasThumbnail = getParaToBool("hasThumbnail", scope); + String orderBy = getPara("orderBy", scope, "order_number desc,id desc"); + int count = getParaToInt("count", scope, 10); + + ArticleCategory category = categoryService.findFirstByTypeAndSlug(ArticleCategory.TYPE_TAG, tag); + if (category == null) { + return; + } + + scope.setLocal("tag", category); + + List

articles = hiddenFlag ? service.findListByCategoryIdNotHidden(category.getId(), hasThumbnail, orderBy, count) : service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count); + + if (articles == null || articles.isEmpty()) { + return; + } + + scope.setLocal("articles", articles); + renderBody(env, scope, writer); + } + + + @Override + public boolean hasEnd() { + return true; + } +} diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/UserArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/UserArticlesDirective.java index 7b7ee5081..930b25376 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/UserArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/UserArticlesDirective.java @@ -1,80 +1,84 @@ -/** - * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). - *

- * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl-3.0.txt - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.jpress.module.article.directive; - -import com.jfinal.aop.Inject; -import com.jfinal.template.Env; -import com.jfinal.template.io.Writer; -import com.jfinal.template.stat.Scope; -import io.jboot.db.model.Columns; -import io.jboot.web.controller.JbootControllerContext; -import io.jboot.web.directive.annotation.JFinalDirective; -import io.jboot.web.directive.base.JbootDirectiveBase; -import io.jpress.model.User; -import io.jpress.module.article.model.Article; -import io.jpress.module.article.service.ArticleService; - -import java.util.List; - -/** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 - */ -@JFinalDirective("userArticles") -public class UserArticlesDirective extends JbootDirectiveBase { - - @Inject - private ArticleService service; - - - @Override - public void onRender(Env env, Scope scope, Writer writer) { - - Long userId = getParaToLong("userId", scope); - User user = JbootControllerContext.get().getAttr("user"); - - if (userId == null && user == null) { - throw new RuntimeException("#userArticles() args is error,userId must not be null." + getLocation()); - } - - if (userId == null) { - userId = user.getId(); - } - - String orderBy = getPara("orderBy", scope, "id desc"); - String status = getPara("status", scope, Article.STATUS_NORMAL); - int count = getParaToInt("count", scope, 10); - - - Columns columns = Columns.create("user_id", userId); - columns.add("status", status); - - List

articles = service.findListByColumns(columns, orderBy, count); - - if (articles == null || articles.isEmpty()) { - return; - } - - scope.setLocal("articles", articles); - renderBody(env, scope, writer); - } - - - @Override - public boolean hasEnd() { - return true; - } -} +/** + * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.article.directive; + +import com.jfinal.aop.Inject; +import com.jfinal.template.Env; +import com.jfinal.template.io.Writer; +import com.jfinal.template.stat.Scope; +import io.jboot.db.model.Columns; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.directive.annotation.JFinalDirective; +import io.jboot.web.directive.base.JbootDirectiveBase; +import io.jpress.model.User; +import io.jpress.module.article.model.Article; +import io.jpress.module.article.service.ArticleService; + +import java.util.List; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + */ +@JFinalDirective("userArticles") +public class UserArticlesDirective extends JbootDirectiveBase { + + @Inject + private ArticleService service; + + + @Override + public void onRender(Env env, Scope scope, Writer writer) { + + Long userId = getParaToLong("userId", scope); + Boolean hiddenFlag = getParaToBool("hiddenFlag", scope, false); + User user = JbootControllerContext.get().getAttr("user"); + + if (userId == null && user == null) { + throw new RuntimeException("#userArticles() args is error,userId must not be null." + getLocation()); + } + + if (userId == null) { + userId = user.getId(); + } + + String orderBy = getPara("orderBy", scope, "id desc"); + String status = getPara("status", scope, Article.STATUS_NORMAL); + int count = getParaToInt("count", scope, 10); + + + Columns columns = Columns.create("user_id", userId); + columns.add("status", status); + if(hiddenFlag) { + columns.ne("flag", "hidden"); + } + + List

articles = service.findListByColumns(columns, orderBy, count); + + if (articles == null || articles.isEmpty()) { + return; + } + + scope.setLocal("articles", articles); + renderBody(env, scope, writer); + } + + + @Override + public boolean hasEnd() { + return true; + } +} -- Gitee From 34d5c679839a08bd594046df2a5ade1bd2530453 Mon Sep 17 00:00:00 2001 From: onewomanonecity <2589071709@qq.com> Date: Sat, 28 Mar 2020 21:14:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9A=90=E8=97=8F?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../provider/ArticleServiceProvider.java | 26 +++++-------------- .../directive/ArticleSearchPageDirective.java | 2 +- .../article/directive/ArticlesDirective.java | 2 +- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java index e9af9b269..21557d9a0 100755 --- a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java +++ b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/provider/ArticleServiceProvider.java @@ -144,9 +144,7 @@ public class ArticleServiceProvider extends JbootServiceBase
implements orderBy = StrUtil.obtainDefaultIfBlank(orderBy, DEFAULT_ORDER_BY); Columns columns = new Columns(); columns.add("status", Article.STATUS_NORMAL); - columns.ne("flag", "hidden"); - columns.or(); - columns.isNull("flag"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); Page
dataPage = DAO.paginateByColumns(page, pagesize, columns, orderBy); return joinUserInfo(dataPage); } @@ -158,9 +156,7 @@ public class ArticleServiceProvider extends JbootServiceBase
implements Columns columns = new Columns(); columns.add("m.category_id", categoryId); columns.add("article.status", Article.STATUS_NORMAL); - columns.ne("flag", "hidden"); - columns.or(); - columns.isNull("flag"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); Page
dataPage = DAO.leftJoin("article_category_mapping") .as("m").on("article.id=m.`article_id`") @@ -284,9 +280,7 @@ public class ArticleServiceProvider extends JbootServiceBase
implements Columns columns = Columns.create(); columns.add(Column.create("id", id, Column.LOGIC_GT)); columns.add(Column.create("status", Article.STATUS_NORMAL)); - columns.ne("flag", "hidden"); - columns.or(); - columns.isNull("flag"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); return joinUserInfo(DAO.findFirstByColumns(columns)); } @@ -295,9 +289,7 @@ public class ArticleServiceProvider extends JbootServiceBase
implements Columns columns = Columns.create(); columns.add(Column.create("id", id, Column.LOGIC_LT)); columns.add(Column.create("status", Article.STATUS_NORMAL)); - columns.ne("flag", "hidden"); - columns.or(); - columns.isNull("flag"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); return joinUserInfo(DAO.findFirstByColumns(columns, "id desc")); } @@ -332,10 +324,8 @@ public class ArticleServiceProvider extends JbootServiceBase
implements .create("m.category_id", categoryId) .eq("article.status", Article.STATUS_NORMAL) .isNotNullIf("article.thumbnail", hasThumbnail != null && hasThumbnail) - .isNullIf("article.thumbnail", hasThumbnail != null && !hasThumbnail) - .ne("flag", "hidden") - .or() - .isNull("flag"); + .isNullIf("article.thumbnail", hasThumbnail != null && !hasThumbnail); + columns.sqlPart("(flag = 'hidden' or flag is null)"); List
articles = DAO.leftJoin("article_category_mapping").as("m") .on("article.id = m.`article_id`") @@ -358,9 +348,7 @@ public class ArticleServiceProvider extends JbootServiceBase
implements columns.in("m.category_id", tagIds.toArray()); columns.ne("article.id", articleId); columns.eq("article.status", status); - columns.ne("flag", "hidden"); - columns.or(); - columns.isNull("flag"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); List
articles = DAO.leftJoin("article_category_mapping").as("m") .on("article.id = m.`article_id`") diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java index 3c8a695f0..53eb7d06e 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticleSearchPageDirective.java @@ -62,7 +62,7 @@ public class ArticleSearchPageDirective extends JbootDirectiveBase { if("hidden".equals(article.getFlag())) { // 通过linkTo实现无法查看 article.setLinkTo("##"); - article.setContent("作者已经隐藏了此文章,你无权查看!"); + article.setContent("该文章已隐藏,你无权查看!"); } } } diff --git a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java index 23d625f1f..690a7b326 100644 --- a/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java +++ b/module-article/module-article-web/src/main/java/io/jpress/module/article/directive/ArticlesDirective.java @@ -58,7 +58,7 @@ public class ArticlesDirective extends JbootDirectiveBase { // 当flag设置为articleSoftHidden时,文章列表页不可见,当flag设置为articleHardHidden时,文章列表页,文章内容页不可见 if(hiddenFlag) { - columns.ne("flag", "hidden"); + columns.sqlPart("(flag = 'hidden' or flag is null)"); } if (hasThumbnail != null) { -- Gitee