All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.guerlab.smart.article.web.controller.AbstractArticleController Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2018-2021 guerlab.net and other contributors.
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.guerlab.smart.article.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import net.guerlab.commons.collection.CollectionUtil;
import net.guerlab.smart.article.core.domain.ArticleCategoryDTO;
import net.guerlab.smart.article.core.domain.ArticleDTO;
import net.guerlab.smart.article.core.exception.ArticleInvalidException;
import net.guerlab.smart.article.core.searchparams.ArticleCategorySearchParams;
import net.guerlab.smart.article.core.searchparams.ArticleSearchParams;
import net.guerlab.smart.article.service.entity.ArticleCategory;
import net.guerlab.smart.article.service.service.AbstractArticleService;
import net.guerlab.smart.article.service.service.ArticleCategoryService;
import net.guerlab.smart.platform.commons.Constants;
import net.guerlab.smart.platform.commons.util.BeanConvertUtils;
import net.guerlab.spring.commons.dto.Convert;
import net.guerlab.web.result.ListObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 抽象文章查询
 *
 * @author guer
 */
public abstract class AbstractArticleController, S extends AbstractArticleService> {

    private S service;

    private ArticleCategoryService categoryService;

    @Operation(description = "查询详情", security = @SecurityRequirement(name = Constants.TOKEN))
    @GetMapping("/{id}")
    public ArticleDTO findOne(@Parameter(name = "文章ID/唯一key", required = true) @PathVariable String id) {
        return findOne0(id).convert();
    }

    protected E findOne0(String id) {
        E article;
        if (Pattern.matches(AbstractArticleService.NUMBER_REG, id)) {
            article = getService().selectById(Long.parseLong(id));
        } else {
            article = getService().selectByUniqueKey(id);
        }

        if (article == null) {
            throw new ArticleInvalidException();
        }
        return article;
    }

    @Operation(description = "查询列表", security = @SecurityRequirement(name = Constants.TOKEN))
    @GetMapping
    public ListObject findList(ArticleSearchParams searchParams) {
        beforeFindList(searchParams);
        ListObject result = BeanConvertUtils.toListObject(getService().queryPage(searchParams));
        if (searchParams.getQueryCategory() != null && searchParams.getQueryCategory()) {
            fillCategories(result.getList());
        }
        return result;
    }

    @Operation(description = "查询全部", security = @SecurityRequirement(name = Constants.TOKEN))
    @GetMapping("/all")
    public List findAll(ArticleSearchParams searchParams) {
        beforeFindList(searchParams);
        List result = BeanConvertUtils.toList(getService().queryAll(searchParams));
        if (searchParams.getQueryCategory() != null && searchParams.getQueryCategory()) {
            fillCategories(result);
        }
        return result;
    }

    protected void beforeFindList(ArticleSearchParams searchParams) {

    }

    private void fillCategories(Collection list) {
        if (list == null || list.isEmpty()) {
            return;
        }

        Collection allCategoryIds = list.stream().map(ArticleDTO::getArticleCategoryIds).flatMap(Collection::stream).collect(Collectors.toSet());

        if (allCategoryIds.isEmpty()) {
            return;
        }

        ArticleCategorySearchParams categorySearchParams = new ArticleCategorySearchParams();
        categorySearchParams.setArticleCategoryIds(allCategoryIds);

        Map categoryMap = CollectionUtil
                .toMap(getCategoryService().selectAll(categorySearchParams), ArticleCategory::getArticleCategoryId,
                        ArticleCategory::convert);

        list.forEach((article) -> article.setCategories(
                article.getArticleCategoryIds().stream().map(categoryMap::get).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedList::new))));
    }

    public S getService() {
        return this.service;
    }

    public ArticleCategoryService getCategoryService() {
        return categoryService;
    }

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public void setService(S service) {
        this.service = service;
    }

    @Autowired
    public void setCategoryService(ArticleCategoryService categoryService) {
        this.categoryService = categoryService;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy