feat(article): 实现管理端文章模块的增删改查功能
- 新增文章表、内容表、标签关联及分类关联的数据库设计与实现 - 实现文章发布、删除、分页查询、详情查看及更新接口 - 文章发布时支持分类验证和标签新增与绑定 - 删除操作会级联删除文章关联的分类和标签关系 - 查询详情接口返回文章基本信息、正文内容、分类及标签列表 - 支持根据标签ID列表批量查询标签信息 - 管理分类接口新增根据ID查询分类详情功能 - 删除分类、标签时增加文章关联校验,防止误删 - 统一返回结构,异常时抛出业务异常,规范日志输出 - 统一使用JPA进行数据库操作,保障事务一致性 - 优化查询性能,添加必要的索引及外键约束 - 补充对应请求和响应的VO类,支持参数校验与业务传递
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.article.*;
|
||||
import com.hanserwei.admin.service.AdminArticleService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 管理端文章控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/article")
|
||||
public class AdminArticleController {
|
||||
|
||||
@Resource
|
||||
private AdminArticleService articleService;
|
||||
|
||||
/**
|
||||
* 发布文章
|
||||
*/
|
||||
@PostMapping("/publish")
|
||||
@ApiOperationLog(description = "文章发布")
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
public Response<?> publishArticle(@RequestBody @Validated PublishArticleReqVO publishArticleReqVO) {
|
||||
return articleService.publishArticle(publishArticleReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章
|
||||
*/
|
||||
@PostMapping("/delete")
|
||||
@ApiOperationLog(description = "文章删除")
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
public Response<?> deleteArticle(@RequestBody @Validated DeleteArticleReqVO deleteArticleReqVO) {
|
||||
return articleService.deleteArticle(deleteArticleReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文章分页数据
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
@ApiOperationLog(description = "查询文章分页数据")
|
||||
public PageResponse<FindArticlePageListRspVO> findArticlePageList(@RequestBody @Validated FindArticlePageListReqVO findArticlePageListReqVO) {
|
||||
return articleService.findArticlePageList(findArticlePageListReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文章详情
|
||||
*/
|
||||
@PostMapping("/detail")
|
||||
@ApiOperationLog(description = "查询文章详情")
|
||||
public Response<FindArticleDetailRspVO> findArticleDetail(@RequestBody @Validated FindArticleDetailReqVO findArticlePageListReqVO) {
|
||||
return articleService.findArticleDetail(findArticlePageListReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
@ApiOperationLog(description = "更新文章")
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
public Response<?> updateArticle(@RequestBody @Validated UpdateArticleReqVO updateArticleReqVO) {
|
||||
return articleService.updateArticle(updateArticleReqVO);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.*;
|
||||
import com.hanserwei.admin.service.AdminCategoryService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
@@ -11,10 +8,7 @@ import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -64,4 +58,13 @@ public class AdminCategoryController {
|
||||
return adminCategoryService.findCategorySelectList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类ID查询
|
||||
*/
|
||||
@GetMapping("/category/find/{id}")
|
||||
@ApiOperationLog(description = "根据分类ID查询")
|
||||
public Response<FindCategoryByIdRspVO> findCategoryById(@PathVariable Long id) {
|
||||
return adminCategoryService.findCategoryById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.tag.*;
|
||||
import com.hanserwei.admin.service.AdminTagService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
@@ -63,5 +61,13 @@ public class AdminTagController {
|
||||
return adminTagService.searchTag(searchTagReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID列表获取标签
|
||||
*/
|
||||
@PostMapping("/tag/list/ids")
|
||||
@ApiOperationLog(description = "根据ID列表获取标签")
|
||||
public Response<List<FindTagsByIdsRspVO>> findTagsByIds(@RequestBody @Validated FindTagsByIdsReqVO findTagsByIdsReqVO) {
|
||||
return adminTagService.findTagsByIds(findTagsByIdsReqVO);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class DeleteArticleReqVO {
|
||||
|
||||
/**
|
||||
* 文章ID
|
||||
*/
|
||||
@NotNull(message = "文章 ID 不能为空")
|
||||
private Long id;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindArticleDetailReqVO {
|
||||
|
||||
/**
|
||||
* 文章 ID
|
||||
*/
|
||||
@NotNull(message = "文章 ID 不能为空")
|
||||
private Long id;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindArticleDetailRspVO {
|
||||
|
||||
/**
|
||||
* 文章 ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 文章封面
|
||||
*/
|
||||
private String cover;
|
||||
|
||||
/**
|
||||
* 文章内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 分类 ID
|
||||
*/
|
||||
private Long categoryId;
|
||||
|
||||
/**
|
||||
* 标签 ID 集合
|
||||
*/
|
||||
private List<Long> tagIds;
|
||||
|
||||
/**
|
||||
* 摘要
|
||||
*/
|
||||
private String summary;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import com.hanserwei.common.model.BasePageQuery;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindArticlePageListReqVO extends BasePageQuery {
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 发布的起始日期
|
||||
*/
|
||||
private LocalDate startDate;
|
||||
|
||||
/**
|
||||
* 发布的结束日期
|
||||
*/
|
||||
private LocalDate endDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindArticlePageListRspVO {
|
||||
|
||||
/**
|
||||
* 文章 ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 文章封面
|
||||
*/
|
||||
private String cover;
|
||||
|
||||
/**
|
||||
* 发布时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
|
||||
/*
|
||||
* 发布文章请求参数
|
||||
*/
|
||||
public class PublishArticleReqVO {
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
@NotBlank(message = "文章标题不能为空")
|
||||
@Length(min = 1, max = 40, message = "文章标题字数需大于 1 小于 40")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 文章内容
|
||||
*/
|
||||
@NotBlank(message = "文章内容不能为空")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 文章封面图片URL
|
||||
*/
|
||||
@NotBlank(message = "文章封面不能为空")
|
||||
private String cover;
|
||||
|
||||
/**
|
||||
* 文章摘要
|
||||
*/
|
||||
private String summary;
|
||||
|
||||
/**
|
||||
* 文章分类ID
|
||||
*/
|
||||
@NotNull(message = "文章分类不能为空")
|
||||
private Long categoryId;
|
||||
|
||||
/**
|
||||
* 文章标签列表
|
||||
*/
|
||||
@NotEmpty(message = "文章标签不能为空")
|
||||
private List<String> tags;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.hanserwei.admin.model.vo.article;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 更新文章请求参数
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class UpdateArticleReqVO {
|
||||
|
||||
/**
|
||||
* 文章ID
|
||||
*/
|
||||
@NotNull(message = "文章 ID 不能为空")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
@NotBlank(message = "文章标题不能为空")
|
||||
@Length(min = 1, max = 40, message = "文章标题字数需大于 1 小于 40")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 文章内容
|
||||
*/
|
||||
@NotBlank(message = "文章内容不能为空")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 文章封面图片URL
|
||||
*/
|
||||
@NotBlank(message = "文章封面不能为空")
|
||||
private String cover;
|
||||
|
||||
/**
|
||||
* 文章摘要
|
||||
*/
|
||||
private String summary;
|
||||
|
||||
/**
|
||||
* 文章分类ID
|
||||
*/
|
||||
@NotNull(message = "文章分类不能为空")
|
||||
private Long categoryId;
|
||||
|
||||
/**
|
||||
* 文章标签列表
|
||||
*/
|
||||
@NotEmpty(message = "文章标签不能为空")
|
||||
private List<String> tags;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hanserwei.admin.model.vo.category;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindCategoryByIdRspVO {
|
||||
/**
|
||||
* 分类 ID
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindTagsByIdsReqVO {
|
||||
/**
|
||||
* 标签 ID 集合
|
||||
*/
|
||||
@NotEmpty(message = "标签 ID 集合不能为空")
|
||||
private List<Long> tagIds;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@Builder
|
||||
public class FindTagsByIdsRspVO {
|
||||
/**
|
||||
* 标签 ID
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 标签名称
|
||||
*/
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.article.*;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
public interface AdminArticleService {
|
||||
/**
|
||||
* 发布文章
|
||||
*
|
||||
* @param publishArticleReqVO 文章发布参数
|
||||
* @return 响应
|
||||
*/
|
||||
Response<?> publishArticle(PublishArticleReqVO publishArticleReqVO);
|
||||
|
||||
/**
|
||||
* 删除文章
|
||||
*
|
||||
* @param deleteArticleReqVO 删除文章参数
|
||||
* @return 响应
|
||||
*/
|
||||
Response<?> deleteArticle(DeleteArticleReqVO deleteArticleReqVO);
|
||||
|
||||
/**
|
||||
* 查询文章分页数据
|
||||
*
|
||||
* @param findArticlePageListReqVO 查询文章分页请求参数
|
||||
* @return 响应
|
||||
*/
|
||||
PageResponse<FindArticlePageListRspVO> findArticlePageList(FindArticlePageListReqVO findArticlePageListReqVO);
|
||||
|
||||
/**
|
||||
* 查询文章详情
|
||||
*
|
||||
* @param findArticleDetailReqVO 查询文章详情参数
|
||||
* @return 响应
|
||||
*/
|
||||
Response<FindArticleDetailRspVO> findArticleDetail(FindArticleDetailReqVO findArticleDetailReqVO);
|
||||
|
||||
/**
|
||||
* 更新文章
|
||||
*
|
||||
* @param updateArticleReqVO 更新文章参数
|
||||
* @return 响应
|
||||
*/
|
||||
Response<?> updateArticle(UpdateArticleReqVO updateArticleReqVO);
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.*;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
@@ -40,6 +37,13 @@ public interface AdminCategoryService {
|
||||
*
|
||||
* @return Select 列表数据
|
||||
*/
|
||||
Response<List<SelectRspVO>> findCategorySelectList();
|
||||
Response<List<SelectRspVO>> findCategorySelectList();
|
||||
|
||||
/**
|
||||
* 根据分类 ID 查询分类
|
||||
*
|
||||
* @param id 分类 ID
|
||||
* @return 查询结果
|
||||
*/
|
||||
Response<FindCategoryByIdRspVO> findCategoryById(Long id);
|
||||
}
|
||||
|
||||
@@ -39,4 +39,12 @@ public interface AdminTagService {
|
||||
* @return 响应结果
|
||||
*/
|
||||
Response<List<SelectRspVO>> searchTag(SearchTagReqVO searchTagReqVO);
|
||||
|
||||
/**
|
||||
* 根据 ID 列表获取标签
|
||||
*
|
||||
* @param findTagsByIdsReqVO 根据 ID 列表获取标签请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
Response<List<FindTagsByIdsRspVO>> findTagsByIds(FindTagsByIdsReqVO findTagsByIdsReqVO);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.article.*;
|
||||
import com.hanserwei.admin.service.AdminArticleService;
|
||||
import com.hanserwei.common.domain.dataobject.*;
|
||||
import com.hanserwei.common.domain.repository.*;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.exception.BizException;
|
||||
import com.hanserwei.common.utils.PageHelper;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AdminArticleServiceImpl implements AdminArticleService {
|
||||
|
||||
@Resource
|
||||
private ArticleRepository articleRepository;
|
||||
@Resource
|
||||
private ArticleTagRelRepository articleTagRelRepository;
|
||||
@Resource
|
||||
private ArticleCategoryRelRepository articleCategoryRelRepository;
|
||||
@Resource
|
||||
private ArticleContentRepository articleContentRepository;
|
||||
@Resource
|
||||
private CategoryRepository categoryRepository;
|
||||
@Resource
|
||||
private TagRepository tagRepository;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Response<?> publishArticle(PublishArticleReqVO publishArticleReqVO) {
|
||||
Article article = new Article();
|
||||
BeanUtils.copyProperties(publishArticleReqVO, article);
|
||||
Article saved = articleRepository.save(article);
|
||||
// 拿到保存后的文章id
|
||||
Long articleId = saved.getId();
|
||||
ArticleContent articleContent = ArticleContent.builder()
|
||||
.articleId(articleId)
|
||||
.content(publishArticleReqVO.getContent())
|
||||
.build();
|
||||
articleContentRepository.save(articleContent);
|
||||
// 处理分类
|
||||
Long categoryId = publishArticleReqVO.getCategoryId();
|
||||
categoryRepository.findById(categoryId)
|
||||
.ifPresentOrElse(p -> {
|
||||
ArticleCategoryRel articleCategoryRel = ArticleCategoryRel.builder()
|
||||
.articleId(articleId)
|
||||
.categoryId(categoryId)
|
||||
.build();
|
||||
articleCategoryRelRepository.save(articleCategoryRel);
|
||||
}, () -> {
|
||||
log.warn("==>文章分类不存在: {}", categoryId);
|
||||
throw new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
|
||||
});
|
||||
// 保存标签
|
||||
List<String> tags = publishArticleReqVO.getTags();
|
||||
insertTags(articleId, tags);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Response<?> deleteArticle(DeleteArticleReqVO deleteArticleReqVO) {
|
||||
Long id = deleteArticleReqVO.getId();
|
||||
articleRepository.deleteById(id);
|
||||
articleCategoryRelRepository.deleteByArticleId(id);
|
||||
articleTagRelRepository.deleteByArticleId(id);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResponse<FindArticlePageListRspVO> findArticlePageList(FindArticlePageListReqVO findArticlePageListReqVO) {
|
||||
return PageHelper.findPageList(
|
||||
articleRepository,
|
||||
findArticlePageListReqVO,
|
||||
findArticlePageListReqVO.getTitle(),
|
||||
"title",
|
||||
findArticlePageListReqVO.getStartDate(),
|
||||
findArticlePageListReqVO.getEndDate(),
|
||||
article -> FindArticlePageListRspVO.builder()
|
||||
.id(article.getId())
|
||||
.title(article.getTitle())
|
||||
.cover(article.getCover())
|
||||
.createTime(LocalDateTime.ofInstant(article.getCreateTime(), ZoneId.systemDefault()))
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response<FindArticleDetailRspVO> findArticleDetail(FindArticleDetailReqVO findArticleDetailReqVO) {
|
||||
Long id = findArticleDetailReqVO.getId();
|
||||
Article article = articleRepository.findById(id)
|
||||
.orElseThrow(() -> {
|
||||
log.warn("==>文章不存在: {}", id);
|
||||
return new BizException(ResponseCodeEnum.ARTICLE_NOT_EXIST);
|
||||
});
|
||||
// 文章正文详情
|
||||
ArticleContent articleContent = articleContentRepository.findByArticleId(id);
|
||||
// 所属分类
|
||||
ArticleCategoryRel articleCategoryRel = articleCategoryRelRepository.findByArticleId(id);
|
||||
// 对应标签集合
|
||||
List<ArticleTagRel> articleTagRelList = articleTagRelRepository.findByArticleId(id);
|
||||
List<Long> tags = articleTagRelList.stream().map(ArticleTagRel::getTagId).toList();
|
||||
// 封装响应结果
|
||||
FindArticleDetailRspVO findArticleDetailRspVO = new FindArticleDetailRspVO();
|
||||
BeanUtils.copyProperties(article, findArticleDetailRspVO);
|
||||
findArticleDetailRspVO.setTagIds(tags);
|
||||
findArticleDetailRspVO.setCategoryId(articleCategoryRel.getCategoryId());
|
||||
findArticleDetailRspVO.setContent(articleContent.getContent());
|
||||
return Response.success(findArticleDetailRspVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Response<?> updateArticle(UpdateArticleReqVO updateArticleReqVO) {
|
||||
Long id = updateArticleReqVO.getId();
|
||||
Article article = articleRepository.findById(id)
|
||||
.orElseThrow(() -> {
|
||||
log.warn("==>欲更新的文章不存在: {}", id);
|
||||
return new BizException(ResponseCodeEnum.ARTICLE_NOT_EXIST);
|
||||
});
|
||||
BeanUtils.copyProperties(updateArticleReqVO, article);
|
||||
// 更新文章正文
|
||||
ArticleContent articleContent = articleContentRepository.findByArticleId(id);
|
||||
articleContent.setContent(updateArticleReqVO.getContent());
|
||||
articleContentRepository.save(articleContent);
|
||||
// 更新分类
|
||||
Long categoryId = updateArticleReqVO.getCategoryId();
|
||||
// 验证该ID的分类是否存在
|
||||
categoryRepository.findById(categoryId).orElseThrow(() -> {
|
||||
log.warn("==>欲更新的文章其分类不存在: {}", categoryId);
|
||||
return new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
|
||||
});
|
||||
// 先删除关联的分类记录,再插入新的关联记录
|
||||
articleCategoryRelRepository.deleteByArticleId(id);
|
||||
ArticleCategoryRel articleCategoryRel = ArticleCategoryRel.builder()
|
||||
.articleId(id)
|
||||
.categoryId(categoryId)
|
||||
.build();
|
||||
articleCategoryRelRepository.save(articleCategoryRel);
|
||||
// 删除该文章的标签关联
|
||||
articleTagRelRepository.deleteByArticleId(id);
|
||||
insertTags(id, updateArticleReqVO.getTags());
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入标签
|
||||
*
|
||||
* @param articleId 文章ID
|
||||
* @param tags 标签列表(可能是ID字符串或标签名称)
|
||||
*/
|
||||
private void insertTags(Long articleId, List<String> tags) {
|
||||
if (CollectionUtils.isEmpty(tags)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String tagStr : tags) {
|
||||
Long tagId;
|
||||
|
||||
// 判断是否为数字ID
|
||||
try {
|
||||
tagId = Long.parseLong(tagStr);
|
||||
// 验证该ID的标签是否存在
|
||||
Optional<Tag> tagOptional = tagRepository.findById(tagId);
|
||||
if (tagOptional.isEmpty()) {
|
||||
log.warn("==>标签ID不存在: {}", tagId);
|
||||
throw new BizException(ResponseCodeEnum.TAG_NOT_EXISTED);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// 不是数字,说明是新标签名称,需要先保存标签
|
||||
Tag newTag = Tag.builder()
|
||||
.name(tagStr)
|
||||
.build();
|
||||
Tag savedTag = tagRepository.save(newTag);
|
||||
tagId = savedTag.getId();
|
||||
log.info("==>新增标签: {}, ID: {}", tagStr, tagId);
|
||||
}
|
||||
|
||||
// 保存文章-标签关联关系
|
||||
ArticleTagRel articleTagRel = ArticleTagRel.builder()
|
||||
.articleId(articleId)
|
||||
.tagId(tagId)
|
||||
.build();
|
||||
articleTagRelRepository.save(articleTagRel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.*;
|
||||
import com.hanserwei.admin.service.AdminCategoryService;
|
||||
import com.hanserwei.common.domain.dataobject.ArticleCategoryRel;
|
||||
import com.hanserwei.common.domain.dataobject.Category;
|
||||
import com.hanserwei.common.domain.repository.ArticleCategoryRelRepository;
|
||||
import com.hanserwei.common.domain.repository.CategoryRepository;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.exception.BizException;
|
||||
@@ -26,6 +25,8 @@ public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
|
||||
@Resource
|
||||
private CategoryRepository categoryRepository;
|
||||
@Resource
|
||||
private ArticleCategoryRelRepository articleCategoryRelRepository;
|
||||
|
||||
@Override
|
||||
public Response<?> addCategory(AddCategoryReqVO addCategoryReqVO) {
|
||||
@@ -60,6 +61,10 @@ public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
|
||||
@Override
|
||||
public Response<?> deleteCategory(DeleteCategoryReqVO deleteCategoryReqVO) {
|
||||
List<ArticleCategoryRel> articleTagRelList = articleCategoryRelRepository.findByCategoryId((deleteCategoryReqVO.getId()));
|
||||
if (!CollectionUtils.isEmpty(articleTagRelList)) {
|
||||
throw new BizException(ResponseCodeEnum.CATEGORY_HAS_ARTICLE);
|
||||
}
|
||||
return categoryRepository.findById(deleteCategoryReqVO.getId())
|
||||
.map(tag -> {
|
||||
tag.setIsDeleted(true);
|
||||
@@ -84,4 +89,14 @@ public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
}
|
||||
return Response.success(selectRspVOS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<FindCategoryByIdRspVO> findCategoryById(Long id) {
|
||||
Category category = categoryRepository.findById(id)
|
||||
.orElseThrow(() -> new BizException(ResponseCodeEnum.CATEGORY_NOT_EXIST));
|
||||
return Response.success(FindCategoryByIdRspVO.builder()
|
||||
.id(category.getId())
|
||||
.name(category.getName())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.tag.*;
|
||||
import com.hanserwei.admin.service.AdminTagService;
|
||||
import com.hanserwei.common.domain.dataobject.ArticleTagRel;
|
||||
import com.hanserwei.common.domain.dataobject.Tag;
|
||||
import com.hanserwei.common.domain.repository.ArticleTagRelRepository;
|
||||
import com.hanserwei.common.domain.repository.TagRepository;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.exception.BizException;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageHelper;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
@@ -21,6 +24,8 @@ public class AdminTagServiceImpl implements AdminTagService {
|
||||
|
||||
@Resource
|
||||
private TagRepository tagRepository;
|
||||
@Resource
|
||||
private ArticleTagRelRepository articleTagRelRepository;
|
||||
|
||||
|
||||
/**
|
||||
@@ -78,6 +83,11 @@ public class AdminTagServiceImpl implements AdminTagService {
|
||||
|
||||
@Override
|
||||
public Response<?> deleteTag(DeleteTagReqVO deleteTagReqVO) {
|
||||
// 检查是否有关联的文章
|
||||
List<ArticleTagRel> articleTagRelsList = articleTagRelRepository.findByTagId(deleteTagReqVO.getId());
|
||||
if (!articleTagRelsList.isEmpty()) {
|
||||
throw new BizException(ResponseCodeEnum.TAG_HAS_ARTICLE);
|
||||
}
|
||||
|
||||
return tagRepository.findById(deleteTagReqVO.getId())
|
||||
.map(tag -> {
|
||||
@@ -92,7 +102,7 @@ public class AdminTagServiceImpl implements AdminTagService {
|
||||
public Response<List<SelectRspVO>> searchTag(SearchTagReqVO searchTagReqVO) {
|
||||
// 使用模糊查询获取标签列表
|
||||
List<Tag> tags = tagRepository.findByNameContaining(searchTagReqVO.getKey());
|
||||
|
||||
|
||||
// 将标签转换为下拉列表格式
|
||||
List<SelectRspVO> vos = tags.stream()
|
||||
.map(tag -> SelectRspVO.builder()
|
||||
@@ -100,7 +110,19 @@ public class AdminTagServiceImpl implements AdminTagService {
|
||||
.value(tag.getId())
|
||||
.build())
|
||||
.toList();
|
||||
|
||||
|
||||
return Response.success(vos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<List<FindTagsByIdsRspVO>> findTagsByIds(FindTagsByIdsReqVO findTagsByIdsReqVO) {
|
||||
List<Tag> tags = tagRepository.queryAllByIdIn(findTagsByIdsReqVO.getTagIds());
|
||||
List<FindTagsByIdsRspVO> vos = tags.stream()
|
||||
.map(tag -> FindTagsByIdsRspVO.builder()
|
||||
.id(tag.getId())
|
||||
.name(tag.getName())
|
||||
.build())
|
||||
.toList();
|
||||
return Response.success(vos);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user