refactor(controller/service/repository): 重构分类属性、文章和消息相关功能
重构分类属性相关类名从Category_attribute改为Categoryattribute 优化文章和消息的分页查询功能,新增分页DTO类 移除旧的分页DTO类PageDto,新增ArriclePageDto和MessagePageDto 调整消息统计逻辑,区分文章评论和独立消息 更新安全配置,开放消息新增接口权限
This commit is contained in:
@@ -67,6 +67,8 @@ public class SecurityConfig {
|
||||
.antMatchers(HttpMethod.POST,"/api/articles/view/**").permitAll()
|
||||
// 所有GET请求公开
|
||||
.antMatchers(HttpMethod.GET,"/api/**").permitAll()
|
||||
// 公开评论新增接口
|
||||
.antMatchers(HttpMethod.POST,"/api/messages").permitAll()
|
||||
// 新增、删除、修改操作需要管理员权限
|
||||
.antMatchers(HttpMethod.POST,"/api/**").hasRole("ADMIN")
|
||||
.antMatchers(HttpMethod.PUT,"/api/**").hasRole("ADMIN")
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.qf.myafterprojecy.controller;
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Article;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.PageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArriclePageDto;
|
||||
import com.qf.myafterprojecy.service.IArticleService;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -66,9 +66,8 @@ public class ArticleController {
|
||||
// api/articles/status/page?status=1&page=1&size=2
|
||||
// get 只能这样不能传递json
|
||||
@GetMapping("/status/page")
|
||||
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(PageDto pageDto) {
|
||||
System.out.println(pageDto);
|
||||
return articleService.getArticlesByStatusWithPagination(pageDto.getStatus(), pageDto.getPage(), pageDto.getSize());
|
||||
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(ArriclePageDto pageDto) {
|
||||
return articleService.getArticlesByStatusWithPagination(pageDto);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +78,15 @@ public class ArticleController {
|
||||
public ResponseMessage<List<Article>> getAllArticles() {
|
||||
return articleService.getAllArticles();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章数量
|
||||
* @param status 文章状态(0:未发表 1:已发表 2:已删除)
|
||||
* @return 返回文章数量
|
||||
*/
|
||||
@GetMapping("/count/{status}")
|
||||
public ResponseMessage<Integer> getArticleCount(@PathVariable Integer status) {
|
||||
return articleService.getArticleCount(status);
|
||||
}
|
||||
/**
|
||||
* 根据标题查询文章列表
|
||||
* @param title 文章标题
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.qf.myafterprojecy.controller;
|
||||
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Category_attribute;
|
||||
import com.qf.myafterprojecy.pojo.Categoryattribute;
|
||||
import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto;
|
||||
import com.qf.myafterprojecy.service.ICategoryAttributeService;
|
||||
|
||||
@@ -31,12 +31,12 @@ public class CategoryAttributeController {
|
||||
* @return 属性信息
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseMessage<Category_attribute> getAttributeById(@PathVariable Integer id) {
|
||||
public ResponseMessage<Categoryattribute> getAttributeById(@PathVariable Integer id) {
|
||||
log.info("接收根据ID获取分类属性的请求: ID={}", id);
|
||||
return categoryAttributeService.getCategoryAttributeById(id);
|
||||
}
|
||||
@GetMapping
|
||||
public ResponseMessage<List<Category_attribute>> getAttributeCount() {
|
||||
public ResponseMessage<List<Categoryattribute>> getAttributeCount() {
|
||||
log.info("接收获取分类属性数量的请求");
|
||||
return categoryAttributeService.getAllCategoryAttributes();
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class CategoryAttributeController {
|
||||
* @return 属性列表
|
||||
*/
|
||||
@GetMapping("/category/{categoryId}")
|
||||
public ResponseMessage<List<Category_attribute>> getAttributesByCategory(@PathVariable Integer categoryId) {
|
||||
public ResponseMessage<List<Categoryattribute>> getAttributesByCategory(@PathVariable Integer categoryId) {
|
||||
log.info("接收根据分类ID获取属性列表的请求: 分类ID={}", categoryId);
|
||||
return categoryAttributeService.getAttributesByCategoryId(categoryId);
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class CategoryAttributeController {
|
||||
*/
|
||||
@PostMapping
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseMessage<Category_attribute> createAttribute(@Valid @RequestBody CategoryAttributeDto dto) {
|
||||
public ResponseMessage<Categoryattribute> createAttribute(@Valid @RequestBody CategoryAttributeDto dto) {
|
||||
log.info("接收创建分类属性的请求: 分类ID={}, 属性名称={}",
|
||||
dto.getCategoryid(), dto.getAttributename());
|
||||
return categoryAttributeService.saveCategoryAttribute(dto);
|
||||
@@ -73,7 +73,7 @@ public class CategoryAttributeController {
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseMessage<Category_attribute> updateAttribute(
|
||||
public ResponseMessage<Categoryattribute> updateAttribute(
|
||||
@PathVariable Integer id,
|
||||
@Valid @RequestBody CategoryAttributeDto dto) {
|
||||
log.info("接收更新分类属性的请求: ID={}, 分类ID={}, 属性名称={}",
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.qf.myafterprojecy.controller;
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Message;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessagePageDto;
|
||||
import com.qf.myafterprojecy.service.IMessageService;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -30,7 +31,25 @@ public class MessageController {
|
||||
logger.info("接收获取所有消息的请求");
|
||||
return messageService.getAllMessages();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询消息
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public ResponseMessage<List<Message>> getMessagesByPage(MessagePageDto messagePageDto) {
|
||||
logger.info("接收分页查询消息的请求: {}", messagePageDto);
|
||||
return messageService.getMessagesByPage(messagePageDto);
|
||||
}
|
||||
/**
|
||||
* 获取消息数量
|
||||
* @param articleId 文章ID (可选)
|
||||
* @return 消息数量
|
||||
* 文章ID为null时返回所有消息数量
|
||||
*/
|
||||
@GetMapping("/count")
|
||||
public ResponseMessage<Integer> getMessageCount( Integer articleId) {
|
||||
logger.info("接收获取消息数量的请求: {}", articleId);
|
||||
return messageService.getMessageCountByArticleId(articleId);
|
||||
}
|
||||
/**
|
||||
* 根据ID获取消息
|
||||
*/
|
||||
@@ -76,14 +95,6 @@ public class MessageController {
|
||||
return messageService.searchMessagesByNickname(nickname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定文章的评论数量
|
||||
*/
|
||||
@GetMapping("/count/article/{articleId}")
|
||||
public ResponseMessage<Long> getMessageCountByArticleId(@PathVariable Integer articleId) {
|
||||
logger.info("接收获取文章评论数量的请求: {}", articleId);
|
||||
return messageService.getMessageCountByArticleId(articleId);
|
||||
}
|
||||
/**
|
||||
* 创建新消息
|
||||
*/
|
||||
|
||||
@@ -66,7 +66,6 @@ public class NonsenseController {
|
||||
* @return 创建结果
|
||||
*/
|
||||
@PostMapping
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseMessage<Nonsense> saveNonsense(@Valid @RequestBody NonsenseDto nonsenseDto) {
|
||||
logger.info("请求保存疯言疯语内容");
|
||||
return nonsenseService.saveNonsense(nonsenseDto);
|
||||
|
||||
@@ -9,8 +9,8 @@ import java.time.LocalDateTime;
|
||||
public class Category {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "typeid")
|
||||
private Integer typeid;
|
||||
@Column(name = "categoryid")
|
||||
private Integer categoryid;
|
||||
|
||||
@NotBlank(message = "分类名称不能为空")
|
||||
@Column(name = "typename")
|
||||
@@ -26,12 +26,12 @@ public class Category {
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
// Getters and Setters
|
||||
public Integer getTypeid() {
|
||||
return typeid;
|
||||
public Integer getCategoryid() {
|
||||
return categoryid;
|
||||
}
|
||||
|
||||
public void setTypeid(Integer typeid) {
|
||||
this.typeid = typeid;
|
||||
public void setCategoryid(Integer categoryid) {
|
||||
this.categoryid = categoryid;
|
||||
}
|
||||
|
||||
public String getTypename() {
|
||||
|
||||
@@ -4,7 +4,7 @@ import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "category_attribute")
|
||||
public class Category_attribute {
|
||||
public class Categoryattribute {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "attributeid")
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.qf.myafterprojecy.pojo.dto;
|
||||
public class ArriclePageDto {
|
||||
|
||||
private Integer status;
|
||||
private String title;
|
||||
private Integer attributeid;
|
||||
private Integer categoryid;
|
||||
private Integer pagenum;
|
||||
private Integer pagesize;
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
public Integer getPagenum() {
|
||||
return pagenum;
|
||||
}
|
||||
public void setPagenum(Integer pagenum) {
|
||||
this.pagenum = pagenum;
|
||||
}
|
||||
public Integer getPagesize() {
|
||||
return pagesize;
|
||||
}
|
||||
public void setPagesize(Integer pagesize) {
|
||||
this.pagesize = pagesize;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public Integer getAttributeid() {
|
||||
return attributeid;
|
||||
}
|
||||
public void setAttributeid(Integer attributeid) {
|
||||
this.attributeid = attributeid;
|
||||
}
|
||||
public Integer getCategoryid() {
|
||||
return categoryid;
|
||||
}
|
||||
public void setCategoryid(Integer categoryid) {
|
||||
this.categoryid = categoryid;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ArPageDto{" +
|
||||
"status=" + status +
|
||||
", pagenum=" + pagenum +
|
||||
", pagesize=" + pagesize +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import javax.validation.constraints.NotBlank;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class CategoryDto {
|
||||
private Integer typeid;
|
||||
private Integer Categoryid;
|
||||
|
||||
@NotBlank(message = "分类名称不能为空")
|
||||
private String typename;
|
||||
@@ -16,12 +16,12 @@ public class CategoryDto {
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
// Getters and Setters
|
||||
public Integer getTypeid() {
|
||||
return typeid;
|
||||
public Integer getCategoryid() {
|
||||
return Categoryid;
|
||||
}
|
||||
|
||||
public void setTypeid(Integer typeid) {
|
||||
this.typeid = typeid;
|
||||
public void setCategoryid(Integer Categoryid) {
|
||||
this.Categoryid = Categoryid;
|
||||
}
|
||||
|
||||
public String getTypename() {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.qf.myafterprojecy.pojo.dto;
|
||||
|
||||
public class MessagePageDto {
|
||||
private Integer pageNum;
|
||||
private Integer pageSize;
|
||||
private Integer articleid;
|
||||
|
||||
public Integer getPageNum() {
|
||||
return pageNum;
|
||||
}
|
||||
public void setPageNum(Integer pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
public void setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
public Integer getArticleid() {
|
||||
return articleid;
|
||||
}
|
||||
public void setArticleid(Integer articleid) {
|
||||
this.articleid = articleid;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MessagePageDto{" +
|
||||
"pageNum=" + pageNum +
|
||||
", pageSize=" + pageSize +
|
||||
", articleid=" + articleid +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.qf.myafterprojecy.pojo.dto;
|
||||
public class PageDto {
|
||||
|
||||
private Integer status;
|
||||
private Integer page;
|
||||
private Integer size;
|
||||
// 数据验证
|
||||
public void validate() {
|
||||
if ( status > 0 && status <= 2) {
|
||||
throw new IllegalArgumentException("Status must be 0 or 2");
|
||||
}
|
||||
if (page < 0) {
|
||||
throw new IllegalArgumentException("Page number must be non-negative");
|
||||
}
|
||||
if (size <= 0 || size > 100) {
|
||||
throw new IllegalArgumentException("Page size must be between 1 and 100");
|
||||
}
|
||||
}
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
public Integer getPage() {
|
||||
return page;
|
||||
}
|
||||
public void setPage(Integer page) {
|
||||
this.page = page;
|
||||
}
|
||||
public Integer getSize() {
|
||||
return size;
|
||||
}
|
||||
public void setSize(Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageDto{" +
|
||||
"status=" + status +
|
||||
", page=" + page +
|
||||
", size=" + size +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -97,5 +97,39 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
|
||||
* @return 返回符合状态条件的文章分页结果
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = :status ORDER BY a.createdAt DESC")
|
||||
Page<Article> findByStatusWithPagination(@Param("status") Integer status, Pageable pageable);
|
||||
Page<Article> findByStatusWithPagination(@Param("status") Integer status, Pageable pageable);
|
||||
/**
|
||||
* 根据属性ID数组分页查询文章列表
|
||||
* @param attributeids 文章属性ID数组
|
||||
* @param status 文章状态,0-草稿,1-已发布,2-已删除
|
||||
* @param pageable 分页参数,包含页码、每页大小和排序信息
|
||||
* @return 返回符合状态条件的文章分页结果
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = :status AND a.attributeid IN :attributeids ORDER BY a.createdAt DESC")
|
||||
Page<Article> findByStatusWithPagination(@Param("status") Integer status, @Param("attributeids") List<Integer> attributeids, Pageable pageable);
|
||||
/**
|
||||
* 根据属性ID分页查询文章列表
|
||||
* @param attributeid 文章属性ID
|
||||
* @param status 文章状态,0-草稿,1-已发布,2-已删除
|
||||
* @param pageable 分页参数,包含页码、每页大小和排序信息
|
||||
* @return 返回符合状态条件的文章分页结果
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = :status AND a.attributeid = :attributeid ORDER BY a.createdAt DESC")
|
||||
Page<Article> findByStatusWithPagination(@Param("status") Integer status, @Param("attributeid") Integer attributeid, Pageable pageable);
|
||||
/**
|
||||
* 根据文章标题分页模糊查询文章列表
|
||||
* @param title 文章标题
|
||||
* @param status 文章状态,0-草稿,1-已发布,2-已删除
|
||||
* @param pageable 分页参数,包含页码、每页大小和排序信息
|
||||
* @return 返回符合状态条件的文章分页结果
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = :status AND a.title LIKE %:title% ORDER BY a.createdAt DESC")
|
||||
Page<Article> findByStatusWithPagination(@Param("status") Integer status, @Param("title") String title, Pageable pageable);
|
||||
/**
|
||||
* 统计指定状态的文章数量
|
||||
* @param status 文章状态,0-草稿,1-已发布,2-已删除
|
||||
* @return 返回符合状态条件的文章数量
|
||||
*/
|
||||
@Query("SELECT COUNT(a) FROM Article a WHERE a.status = :status")
|
||||
Integer countByStatus(@Param("status") Integer status);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.qf.myafterprojecy.repository;
|
||||
|
||||
import com.qf.myafterprojecy.pojo.Category_attribute;
|
||||
import com.qf.myafterprojecy.pojo.Categoryattribute;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@@ -10,23 +10,23 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CategoryAttributeRepository extends JpaRepository<Category_attribute, Integer> {
|
||||
public interface CategoryAttributeRepository extends JpaRepository<Categoryattribute, Integer> {
|
||||
|
||||
/**
|
||||
* 根据分类ID查询分类属性列表
|
||||
* @param categoryid 分类ID
|
||||
* @return 返回该分类下的所有属性列表
|
||||
*/
|
||||
@Query("SELECT ca FROM Category_attribute ca WHERE ca.categoryid = :categoryid")
|
||||
List<Category_attribute> findByCategoryId(@Param("categoryid") Integer categoryid);
|
||||
@Query("SELECT ca FROM Categoryattribute ca WHERE ca.categoryid = :categoryid")
|
||||
List<Categoryattribute> findByCategoryId(@Param("categoryid") Integer categoryid);
|
||||
|
||||
/**
|
||||
* 根据属性ID查询属性信息
|
||||
* @param attributeid 属性ID
|
||||
* @return 返回属性对象
|
||||
*/
|
||||
@Query("SELECT ca FROM Category_attribute ca WHERE ca.attributeid = :attributeid")
|
||||
Optional<Category_attribute> findByAttributeId(@Param("attributeid") Integer attributeid);
|
||||
@Query("SELECT ca FROM Categoryattribute ca WHERE ca.attributeid = :attributeid")
|
||||
Optional<Categoryattribute> findByAttributeId(@Param("attributeid") Integer attributeid);
|
||||
|
||||
/**
|
||||
* 检查分类下是否存在指定名称的属性
|
||||
@@ -42,5 +42,5 @@ public interface CategoryAttributeRepository extends JpaRepository<Category_attr
|
||||
* @param attributename 属性名称
|
||||
* @return 属性对象
|
||||
*/
|
||||
Optional<Category_attribute> findByCategoryidAndAttributename(Integer categoryid, String attributename);
|
||||
Optional<Categoryattribute> findByCategoryidAndAttributename(Integer categoryid, String attributename);
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.qf.myafterprojecy.repository;
|
||||
|
||||
import com.qf.myafterprojecy.pojo.Message;
|
||||
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@@ -60,10 +64,33 @@ public interface MessageRepository extends JpaRepository<Message, Integer> {
|
||||
void incrementLikes(@Param("messageId") Integer messageId);
|
||||
// 统计指定文章的评论数量
|
||||
/**
|
||||
* 统计指定文章的评论数量
|
||||
* @param articleId 文章ID
|
||||
* @return 评论数量
|
||||
* 根据文章ID分页查询消息
|
||||
* @param articleid 文章ID
|
||||
* @param pageable 分页信息
|
||||
* @return 分页消息列表
|
||||
*/
|
||||
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = :articleId")
|
||||
Long countByArticleId(@Param("articleId") Integer articleId);
|
||||
@Query("SELECT m FROM Message m WHERE m.articleid = :articleId ORDER BY m.createdAt DESC")
|
||||
Page<Message> findByArticleId(@Param("articleId") Integer articleId, PageRequest pageable);
|
||||
/**
|
||||
* 根据页查询消息
|
||||
* @param pageable 分页信息
|
||||
* @return 分页消息列表
|
||||
*/
|
||||
@Query("SELECT m FROM Message m WHERE m.articleid IS NULL ORDER BY m.createdAt DESC")
|
||||
Page<Message> findAllMessages(PageRequest pageable);
|
||||
/**
|
||||
* 统计指定文章下的回复消息数量
|
||||
* @param articleId 文章ID
|
||||
* @return 回复消息数量
|
||||
*/
|
||||
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = :articleId AND m.parentid IS NULL")
|
||||
Integer countReplyByArticleId(@Param("articleId") Integer articleId);
|
||||
|
||||
/**
|
||||
* 统计指定文章id parentid为空的回复消息数量
|
||||
* @param articleId 文章ID
|
||||
* @return 回复消息数量
|
||||
*/
|
||||
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid IS NULL AND m.parentid IS NULL")
|
||||
Integer countReplyByArticleIdIsNull();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.qf.myafterprojecy.service;
|
||||
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Article;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArriclePageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
@@ -25,7 +26,12 @@ public interface IArticleService {
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
ResponseMessage<List<Article>> getArticlesByStatus(Integer status);
|
||||
|
||||
/**
|
||||
* 获取文章数量
|
||||
* @param status 文章状态(0:未发表 1:已发表 2:已删除)
|
||||
* @return 返回文章数量
|
||||
*/
|
||||
ResponseMessage<Integer> getArticleCount(Integer status);
|
||||
/**
|
||||
* 创建新文章
|
||||
* 仅限AUTHOR角色用户访问
|
||||
@@ -98,5 +104,5 @@ public interface IArticleService {
|
||||
* @param size 每页大小
|
||||
* @return 返回包含分页文章列表的ResponseMessage对象
|
||||
*/
|
||||
ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size);
|
||||
ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(ArriclePageDto arriclePageDto);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.qf.myafterprojecy.service;
|
||||
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Category_attribute;
|
||||
import com.qf.myafterprojecy.pojo.Categoryattribute;
|
||||
import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto;
|
||||
|
||||
import java.util.List;
|
||||
@@ -11,28 +11,28 @@ public interface ICategoryAttributeService {
|
||||
* 获取全部分类属性
|
||||
* @return 所有分类属性列表
|
||||
*/
|
||||
ResponseMessage<List<Category_attribute>> getAllCategoryAttributes();
|
||||
ResponseMessage<List<Categoryattribute>> getAllCategoryAttributes();
|
||||
|
||||
/**
|
||||
* 根据ID获取分类属性
|
||||
* @param id 属性ID
|
||||
* @return 分类属性信息
|
||||
*/
|
||||
ResponseMessage<Category_attribute> getCategoryAttributeById(Integer id);
|
||||
ResponseMessage<Categoryattribute> getCategoryAttributeById(Integer id);
|
||||
|
||||
/**
|
||||
* 根据分类ID获取属性列表
|
||||
* @param categoryId 分类ID
|
||||
* @return 属性列表
|
||||
*/
|
||||
ResponseMessage<List<Category_attribute>> getAttributesByCategoryId(Integer categoryId);
|
||||
ResponseMessage<List<Categoryattribute>> getAttributesByCategoryId(Integer categoryId);
|
||||
|
||||
/**
|
||||
* 创建新的分类属性
|
||||
* @param dto 分类属性数据
|
||||
* @return 创建结果
|
||||
*/
|
||||
ResponseMessage<Category_attribute> saveCategoryAttribute(CategoryAttributeDto dto);
|
||||
ResponseMessage<Categoryattribute> saveCategoryAttribute(CategoryAttributeDto dto);
|
||||
|
||||
/**
|
||||
* 更新分类属性
|
||||
@@ -40,7 +40,7 @@ public interface ICategoryAttributeService {
|
||||
* @param dto 分类属性数据
|
||||
* @return 更新结果
|
||||
*/
|
||||
ResponseMessage<Category_attribute> updateCategoryAttribute(Integer id, CategoryAttributeDto dto);
|
||||
ResponseMessage<Categoryattribute> updateCategoryAttribute(Integer id, CategoryAttributeDto dto);
|
||||
|
||||
/**
|
||||
* 删除分类属性
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.qf.myafterprojecy.service;
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Message;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessagePageDto;
|
||||
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -14,7 +17,18 @@ public interface IMessageService {
|
||||
* ResponseMessage是响应消息的包装类,Iterable<Message>表示可迭代的消息集合
|
||||
*/
|
||||
ResponseMessage<Iterable<Message>> getAllMessages();
|
||||
|
||||
/**
|
||||
* 分页
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
ResponseMessage<List<Message>> getMessagesByPage(MessagePageDto messagePageDto);
|
||||
/**
|
||||
* 获取回复消息条数 如果id为空获取文章id为空的消息条数
|
||||
* @param articleId 文章id
|
||||
* @return 回复消息条数
|
||||
*/
|
||||
ResponseMessage<Integer> getMessageCountByArticleId(Integer articleId);
|
||||
/**
|
||||
* 根据消息ID获取消息的方法
|
||||
*
|
||||
@@ -82,11 +96,4 @@ public interface IMessageService {
|
||||
// 删除所有评论
|
||||
ResponseMessage<Void> deleteAllMessages();
|
||||
|
||||
/**
|
||||
* 获取指定文章的评论数量
|
||||
*
|
||||
* @param articleId 文章ID
|
||||
* @return 评论数量
|
||||
*/
|
||||
ResponseMessage<Long> getMessageCountByArticleId(Integer articleId);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.qf.myafterprojecy.service.impl;
|
||||
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Article;
|
||||
import com.qf.myafterprojecy.pojo.Categoryattribute;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArriclePageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
import com.qf.myafterprojecy.repository.ArticleRepository;
|
||||
import com.qf.myafterprojecy.repository.CategoryAttributeRepository;
|
||||
@@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class ArticleService implements IArticleService {
|
||||
@@ -63,6 +66,28 @@ public class ArticleService implements IArticleService {
|
||||
return ResponseMessage.error("获取文章失败");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取文章数量
|
||||
* @param status 文章状态(0:未发表 1:已发表 2:已删除)
|
||||
* @return 返回文章数量
|
||||
*/
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<Integer> getArticleCount(Integer status) {
|
||||
try {
|
||||
if (status == null) {
|
||||
return ResponseMessage.badRequest("文章状态不能为空");
|
||||
}
|
||||
if (status < 0 || status > 2) {
|
||||
return ResponseMessage.badRequest("文章状态值必须在0到2之间");
|
||||
}
|
||||
Integer count = articleRepository.countByStatus(status);
|
||||
return ResponseMessage.success(count, "获取文章数量成功");
|
||||
} catch (Exception e) {
|
||||
log.error("获取文章数量失败: {}", e.getMessage());
|
||||
return ResponseMessage.error("获取文章数量失败");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据状态获取文章列表
|
||||
* @param status 文章状态(0:未发表 1:已发表 2:已删除)
|
||||
@@ -103,23 +128,43 @@ public class ArticleService implements IArticleService {
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size) {
|
||||
try {
|
||||
if (status == null) {
|
||||
return ResponseMessage.badRequest("文章状态不能为空");
|
||||
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(ArriclePageDto arriclePageDto) {
|
||||
if (arriclePageDto.getPagenum() == null || arriclePageDto.getPagenum() < 0) {
|
||||
arriclePageDto.setPagenum(0); // 默认第一页
|
||||
}
|
||||
if (status < 0 || status > 2) {
|
||||
if (arriclePageDto.getPagesize() == null || arriclePageDto.getPagesize() <= 0 || arriclePageDto.getPagesize() > 100) {
|
||||
arriclePageDto.setPagesize(10); // 默认每页10条,最大100条
|
||||
}
|
||||
try {
|
||||
// 如果文章状态值是否在0到2之间则根据文章状态查询文章列表
|
||||
if (arriclePageDto.getStatus() < 0 || arriclePageDto.getStatus() > 2) {
|
||||
return ResponseMessage.badRequest("文章状态值必须在0到2之间");
|
||||
}
|
||||
if (page == null || page < 0) {
|
||||
page = 0; // 默认第一页
|
||||
PageRequest pageRequest = PageRequest.of(arriclePageDto.getPagenum(), arriclePageDto.getPagesize());
|
||||
// 如果文章分类ID不为空则根据文章分类ID查询文章列表
|
||||
if (arriclePageDto.getCategoryid() != null && arriclePageDto.getCategoryid() > 0) {
|
||||
// 如果文章分类ID不为空则根据文章分类ID查询文章列表
|
||||
List<Categoryattribute> categoryAttribute = categoryAttributeRepository.findByCategoryId(arriclePageDto.getCategoryid());
|
||||
if (categoryAttribute.isEmpty()) {
|
||||
return ResponseMessage.badRequest("分类下没有属性");
|
||||
}
|
||||
// 如果文章属性ID数组不为空则根据文章属性ID数组查询文章列表
|
||||
List<Integer> attributeids = categoryAttribute.stream().map(Categoryattribute::getAttributeid).collect(Collectors.toList());
|
||||
// 根据分类ID对应的属性ID数组分页查询文章列表
|
||||
Page<Article> articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), attributeids, pageRequest);
|
||||
return ResponseMessage.success(articlePage, "根据分类ID分页查询文章成功");
|
||||
}
|
||||
if (size == null || size <= 0 || size > 100) {
|
||||
size = 10; // 默认每页10条,最大100条
|
||||
// 如果文章属性ID不为空则根据文章属性ID查询文章列表
|
||||
if (arriclePageDto.getAttributeid() != null && arriclePageDto.getAttributeid() > 0) {
|
||||
Page<Article> articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), arriclePageDto.getAttributeid(), pageRequest);
|
||||
return ResponseMessage.success(articlePage, "根据属性ID分页查询文章成功");
|
||||
}
|
||||
|
||||
PageRequest pageRequest = PageRequest.of(page, size);
|
||||
Page<Article> articlePage = articleRepository.findByStatusWithPagination(status, pageRequest);
|
||||
// 如果文章标题不为空则根据文章标题查询文章列表
|
||||
if (arriclePageDto.getTitle() != null && !arriclePageDto.getTitle().isEmpty()) {
|
||||
Page<Article> articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), arriclePageDto.getTitle(), pageRequest);
|
||||
return ResponseMessage.success(articlePage, "根据标题分页查询文章成功");
|
||||
}
|
||||
Page<Article> articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), pageRequest);
|
||||
return ResponseMessage.success(articlePage, "根据状态分页查询文章成功");
|
||||
} catch (Exception e) {
|
||||
log.error("根据状态分页查询文章列表失败: {}", e.getMessage());
|
||||
@@ -224,8 +269,6 @@ public class ArticleService implements IArticleService {
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<List<Article>> getArticlesByCategory(Integer categoryId) {
|
||||
try {
|
||||
// 为了兼容旧接口,这里需要调整逻辑
|
||||
// 旧接口使用typeid,但我们现在用attributeid
|
||||
// 可以考虑查询该分类下的所有属性,然后获取相关文章
|
||||
log.warn("使用了旧接口getArticlesByCategory,请考虑迁移到getArticlesByAttribute");
|
||||
return ResponseMessage.success(articleRepository.findPublishedByAttribute(categoryId), "获取分类文章成功");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.qf.myafterprojecy.service.impl;
|
||||
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Category_attribute;
|
||||
import com.qf.myafterprojecy.pojo.Categoryattribute;
|
||||
import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto;
|
||||
import com.qf.myafterprojecy.repository.CategoryAttributeRepository;
|
||||
import com.qf.myafterprojecy.service.ICategoryAttributeService;
|
||||
@@ -30,9 +30,9 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<List<Category_attribute>> getAllCategoryAttributes() {
|
||||
public ResponseMessage<List<Categoryattribute>> getAllCategoryAttributes() {
|
||||
try {
|
||||
List<Category_attribute> attributes = categoryAttributeRepository.findAll();
|
||||
List<Categoryattribute> attributes = categoryAttributeRepository.findAll();
|
||||
return ResponseMessage.success(attributes, "获取所有分类属性成功");
|
||||
} catch (DataAccessException e) {
|
||||
log.error("获取所有分类属性失败: {}", e.getMessage());
|
||||
@@ -47,13 +47,13 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<Category_attribute> getCategoryAttributeById(Integer id) {
|
||||
public ResponseMessage<Categoryattribute> getCategoryAttributeById(Integer id) {
|
||||
try {
|
||||
if (id == null || id <= 0) {
|
||||
return ResponseMessage.badRequest("属性ID无效");
|
||||
}
|
||||
|
||||
Category_attribute attribute = categoryAttributeRepository.findById(id)
|
||||
Categoryattribute attribute = categoryAttributeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("分类属性不存在"));
|
||||
|
||||
return ResponseMessage.success(attribute, "获取分类属性成功");
|
||||
@@ -76,13 +76,13 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseMessage<List<Category_attribute>> getAttributesByCategoryId(Integer categoryId) {
|
||||
public ResponseMessage<List<Categoryattribute>> getAttributesByCategoryId(Integer categoryId) {
|
||||
try {
|
||||
if (categoryId == null || categoryId <= 0) {
|
||||
return ResponseMessage.badRequest("分类ID无效");
|
||||
}
|
||||
|
||||
List<Category_attribute> attributes = categoryAttributeRepository.findByCategoryId(categoryId);
|
||||
List<Categoryattribute> attributes = categoryAttributeRepository.findByCategoryId(categoryId);
|
||||
return ResponseMessage.success(attributes, "获取分类属性列表成功");
|
||||
} catch (DataAccessException e) {
|
||||
log.error("获取分类属性列表失败: {}", e.getMessage());
|
||||
@@ -97,7 +97,7 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResponseMessage<Category_attribute> saveCategoryAttribute(CategoryAttributeDto dto) {
|
||||
public ResponseMessage<Categoryattribute> saveCategoryAttribute(CategoryAttributeDto dto) {
|
||||
try {
|
||||
// 检查属性名称是否已存在于该分类下
|
||||
if (categoryAttributeRepository.existsByCategoryidAndAttributename(
|
||||
@@ -105,10 +105,10 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
return ResponseMessage.badRequest("该分类下已存在同名属性");
|
||||
}
|
||||
|
||||
Category_attribute attribute = new Category_attribute();
|
||||
Categoryattribute attribute = new Categoryattribute();
|
||||
BeanUtils.copyProperties(dto, attribute);
|
||||
|
||||
Category_attribute savedAttribute = categoryAttributeRepository.save(attribute);
|
||||
Categoryattribute savedAttribute = categoryAttributeRepository.save(attribute);
|
||||
log.info("成功创建分类属性: {}, 分类ID: {}",
|
||||
savedAttribute.getAttributename(), savedAttribute.getCategoryid());
|
||||
|
||||
@@ -127,13 +127,13 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResponseMessage<Category_attribute> updateCategoryAttribute(Integer id, CategoryAttributeDto dto) {
|
||||
public ResponseMessage<Categoryattribute> updateCategoryAttribute(Integer id, CategoryAttributeDto dto) {
|
||||
try {
|
||||
if (id == null || id <= 0) {
|
||||
return ResponseMessage.badRequest("属性ID无效");
|
||||
}
|
||||
|
||||
Category_attribute attribute = categoryAttributeRepository.findById(id)
|
||||
Categoryattribute attribute = categoryAttributeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("分类属性不存在"));
|
||||
|
||||
// 如果修改了属性名称,检查新名称是否已存在
|
||||
@@ -146,7 +146,7 @@ public class CategoryAttributeService implements ICategoryAttributeService {
|
||||
BeanUtils.copyProperties(dto, attribute);
|
||||
attribute.setAttributeid(id); // 确保ID不变
|
||||
|
||||
Category_attribute updatedAttribute = categoryAttributeRepository.save(attribute);
|
||||
Categoryattribute updatedAttribute = categoryAttributeRepository.save(attribute);
|
||||
log.info("成功更新分类属性: ID={}, 名称={}",
|
||||
updatedAttribute.getAttributeid(), updatedAttribute.getAttributename());
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.qf.myafterprojecy.service.impl;
|
||||
import com.qf.myafterprojecy.exceptopn.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.Message;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessageDto;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessagePageDto;
|
||||
import com.qf.myafterprojecy.repository.MessageRepository;
|
||||
import com.qf.myafterprojecy.service.IMessageService;
|
||||
|
||||
@@ -10,7 +11,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -231,20 +235,52 @@ public class MessageService implements IMessageService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseMessage<Long> getMessageCountByArticleId(Integer articleId) {
|
||||
if (articleId == null || articleId <= 0) {
|
||||
logger.warn("获取文章评论数量时ID无效: {}", articleId);
|
||||
return ResponseMessage.badRequest("文章ID无效");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseMessage<List<Message>> getMessagesByPage(MessagePageDto messagePageDto) {
|
||||
if (messagePageDto == null) {
|
||||
logger.warn("分页查询消息时参数为空");
|
||||
return ResponseMessage.badRequest("分页参数不能为空");
|
||||
}
|
||||
if (messagePageDto.getPageNum() == null) {
|
||||
logger.warn("分页查询消息时页码无效: {}", messagePageDto.getPageNum());
|
||||
return ResponseMessage.badRequest("页码无效");
|
||||
}
|
||||
if (messagePageDto.getPageSize() == null || messagePageDto.getPageSize() <= 0) {
|
||||
logger.warn("分页查询消息时每页数量无效: {}", messagePageDto.getPageSize());
|
||||
return ResponseMessage.badRequest("每页数量无效");
|
||||
}
|
||||
try {
|
||||
logger.info("获取文章评论数量: {}", articleId);
|
||||
Long count = messageRepository.countByArticleId(articleId);
|
||||
// 如何文章id为空,默认根据分页基础信息查询消息
|
||||
PageRequest pageable = PageRequest.of(messagePageDto.getPageNum(), messagePageDto.getPageSize());
|
||||
if (messagePageDto.getArticleid() != null && messagePageDto.getArticleid() > 0) {
|
||||
// 如果文章ID存在,根据文章ID查询消息
|
||||
Page<Message> messagePage = messageRepository.findByArticleId(messagePageDto.getArticleid(), pageable);
|
||||
return ResponseMessage.success(messagePage.getContent(), "查询成功");
|
||||
}
|
||||
// 如果文章ID不存在,根据分页基础信息查询所有消息
|
||||
Page<Message> messagePage = messageRepository.findAllMessages(pageable);
|
||||
return ResponseMessage.success(messagePage.getContent(), "查询成功");
|
||||
} catch (DataAccessException e) {
|
||||
logger.error("分页查询消息失败: {}", messagePageDto, e);
|
||||
return ResponseMessage.error("查询消息失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
// 获取回复消息条数 如果id为空获取文章id为空的消息条数
|
||||
@Override
|
||||
public ResponseMessage<Integer> getMessageCountByArticleId(Integer articleId) {
|
||||
|
||||
try {
|
||||
logger.info("获取文章回复数量: {}", articleId);
|
||||
if (articleId == null || articleId <= 0) {
|
||||
Integer count = messageRepository.countReplyByArticleIdIsNull();
|
||||
return ResponseMessage.success(count, "查询成功");
|
||||
}
|
||||
Integer count = messageRepository.countReplyByArticleId(articleId);
|
||||
return ResponseMessage.success(count, "查询成功");
|
||||
} catch (DataAccessException e) {
|
||||
logger.error("获取文章评论数量失败: {}", articleId, e);
|
||||
return ResponseMessage.error("查询评论数量失败:" + e.getMessage());
|
||||
logger.error("获取文章回复数量失败: {}", articleId, e);
|
||||
return ResponseMessage.error("查询回复数量失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user