net.guerlab.smart.article.web.controller.user.ArticleController 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.user;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.guerlab.smart.article.core.PermissionConstants;
import net.guerlab.smart.article.core.domain.ArticleDTO;
import net.guerlab.smart.article.core.enums.AuditStatus;
import net.guerlab.smart.article.core.enums.PublishType;
import net.guerlab.smart.article.core.exception.ArticleAuditStatusErrorException;
import net.guerlab.smart.article.core.exception.ArticleInvalidException;
import net.guerlab.smart.article.core.searchparams.ArticleCategorySearchParams;
import net.guerlab.smart.article.service.entity.Article;
import net.guerlab.smart.article.service.service.ArticleConfigService;
import net.guerlab.smart.article.service.service.ArticleService;
import net.guerlab.smart.article.web.controller.AbstractArticleController;
import net.guerlab.smart.platform.commons.Constants;
import net.guerlab.smart.platform.commons.annotation.HasPermission;
import net.guerlab.smart.user.api.OperationLogApi;
import net.guerlab.smart.user.api.UserApi;
import net.guerlab.smart.user.auth.UserContextHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.Collections;
/**
* 文章
*
* @author guer
*/
@Tag(name = "文章")
@RestController("/user/article")
@RequestMapping("/user/article")
public class ArticleController extends AbstractArticleController {
private ArticleConfigService configService;
private OperationLogApi operationLogApi;
private UserApi userApi;
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_MANAGER + ")")
@Operation(description = "添加", security = @SecurityRequirement(name = Constants.TOKEN))
@PostMapping
public ArticleDTO save(@Parameter(name = "对象数据", required = true) @RequestBody ArticleDTO dto) {
Article entity = new Article();
BeanUtils.copyProperties(dto, entity);
if (needAudit()) {
entity.setAuditStatus(AuditStatus.WAIT);
} else {
entity.setAuditStatus(AuditStatus.PASS);
}
getService().insert(entity);
operationLogApi.add("添加文章", UserContextHandler.getUserId(), entity);
return entity.convert();
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_MANAGER + ")")
@Operation(description = "编辑", security = @SecurityRequirement(name = Constants.TOKEN))
@PutMapping("/{id}")
public ArticleDTO update(@Parameter(name = "id", required = true) @PathVariable Long id, @Parameter(name = "对象数据", required = true) @RequestBody ArticleDTO dto) {
Article entity = getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
entity.setArticleCategoryId(dto.getArticleCategoryId());
entity.setSecondaryArticleCategoryIds(dto.getSecondaryArticleCategoryIds());
entity.setTitle(dto.getTitle());
entity.setKeywords(dto.getKeywords());
entity.setDescription(dto.getDescription());
entity.setCoverUrl(dto.getCoverUrl());
entity.setAuthor(dto.getAuthor());
entity.setReleaseTime(dto.getReleaseTime());
entity.setSynopsis(dto.getSynopsis());
entity.setContent(dto.getContent());
entity.setOriginalLink(dto.getOriginalLink());
entity.setAlwaysRedirect(dto.getAlwaysRedirect());
entity.setPublishType(dto.getPublishType());
entity.setPlanPublishTime(dto.getPlanPublishTime());
entity.setAttachments(dto.getAttachments());
entity.setViewNumber(null);
entity.setOrderNum(dto.getOrderNum());
if (needAudit()) {
entity.setAuditStatus(AuditStatus.WAIT);
} else {
entity.setAuditStatus(AuditStatus.PASS);
}
getService().updateById(entity);
operationLogApi.add("编辑文章", UserContextHandler.getUserId(), entity);
return getService().selectById(id).convert();
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_MANAGER + ")")
@Operation(description = "删除", security = @SecurityRequirement(name = Constants.TOKEN))
@DeleteMapping("/{id}")
public void delete(@Parameter(name = "id", required = true) @PathVariable Long id, @Parameter(name = "强制删除标志") @RequestParam(required = false) Boolean force) {
getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
getService().deleteById(id, force);
operationLogApi.add("删除文章", UserContextHandler.getUserId(), id);
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_AUDIT + ")")
@Operation(description = "显示", security = @SecurityRequirement(name = Constants.TOKEN))
@PutMapping("/{id}/show")
public ArticleDTO show(@Parameter(name = "id", required = true) @PathVariable Long id) {
Article entity = getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
if (entity.getAuditStatus() != AuditStatus.PASS) {
throw new ArticleAuditStatusErrorException();
}
entity.setPublished(true);
entity.setPublishType(PublishType.MANUAL);
entity.setPublishTime(LocalDateTime.now());
getService().updateById(entity);
operationLogApi.add("显示文章", UserContextHandler.getUserId(), id);
return getService().selectById(id).convert();
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_AUDIT + ")")
@Operation(description = "不显示", security = @SecurityRequirement(name = Constants.TOKEN))
@PutMapping("/{id}/unShow")
public ArticleDTO unShow(@Parameter(name = "id", required = true) @PathVariable Long id) {
Article entity = getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
entity.setPublished(false);
entity.setPublishType(PublishType.MANUAL);
getService().updateById(entity);
operationLogApi.add("隐藏文章", UserContextHandler.getUserId(), id);
return getService().selectById(id).convert();
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_AUDIT + ")")
@Operation(description = "审核通过", security = @SecurityRequirement(name = Constants.TOKEN))
@PutMapping("/{id}/audit/pass")
public ArticleDTO auditPass(@Parameter(name = "id", required = true) @PathVariable Long id) {
Article entity = getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
if (entity.getAuditStatus() != AuditStatus.WAIT) {
throw new ArticleAuditStatusErrorException();
}
if (entity.getPublishType() == PublishType.AUTOMATIC) {
entity.setPublished(true);
} else if (entity.getPublishType() == PublishType.TIMING) {
entity.setPublished(entity.getPlanPublishTime().isBefore(LocalDateTime.now()));
}
entity.setAuditStatus(AuditStatus.PASS);
getService().updateById(entity);
operationLogApi.add("文章审核通过", UserContextHandler.getUserId(), id);
return getService().selectById(id).convert();
}
@HasPermission("hasKey(" + PermissionConstants.ARTICLE_AUDIT + ")")
@Operation(description = "审核拒绝", security = @SecurityRequirement(name = Constants.TOKEN))
@PutMapping("/{id}/audit/refuse")
public ArticleDTO auditRefuse(@Parameter(name = "id", required = true) @PathVariable Long id) {
Article entity = getService().selectByIdOptional(id).orElseThrow(ArticleInvalidException::new);
if (entity.getAuditStatus() != AuditStatus.WAIT) {
throw new ArticleAuditStatusErrorException();
}
entity.setPublished(false);
entity.setAuditStatus(AuditStatus.REFUSE);
getService().updateById(entity);
operationLogApi.add("文章审核拒绝", UserContextHandler.getUserId(), id);
return getService().selectById(id).convert();
}
@Override
protected Article findOne0(String id) {
Article article = super.findOne0(id);
ArticleCategorySearchParams categorySearchParams = new ArticleCategorySearchParams();
categorySearchParams.setArticleCategoryIds(article.getArticleCategoryIds());
article.setCategories(getCategoryService().selectAll(categorySearchParams));
return article;
}
private boolean needAudit() {
return enableAudit() && notHasAuditPermission();
}
private boolean enableAudit() {
return configService.match("audit", "true");
}
private boolean notHasAuditPermission() {
return !userApi.hasPermission(UserContextHandler.getUserId(), Collections.singletonList(PermissionConstants.ARTICLE_AUDIT));
}
@Autowired
public void setConfigService(ArticleConfigService configService) {
this.configService = configService;
}
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
public void setOperationLogApi(OperationLogApi operationLogApi) {
this.operationLogApi = operationLogApi;
}
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
public void setUserApi(UserApi userApi) {
this.userApi = userApi;
}
}