forked from iVMiku/guidance-backend
更新标签
This commit is contained in:
@@ -24,6 +24,17 @@
|
||||
<!-- <artifactId>springfox-boot-starter</artifactId>-->
|
||||
<!-- <version>3.0.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- 集成redis依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||
<version>1.28.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
@@ -76,11 +87,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Sa-Token 权限认证,在线文档:https://sa-token.cc -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- <!– Sa-Token 权限认证,在线文档:https://sa-token.cc –>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>cn.dev33</groupId>-->
|
||||
<!-- <artifactId>sa-token-spring-boot3-starter</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- fastjson2 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.ivmiku.tutorial.entity.Comment;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
@@ -19,6 +20,7 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/comments")
|
||||
@SaCheckLogin
|
||||
public class CommentController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.ivmiku.tutorial.entity.Community;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.CommunityService;
|
||||
@@ -18,7 +19,7 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/communities")
|
||||
|
||||
@SaCheckLogin
|
||||
public class CommunityController {
|
||||
|
||||
// 使用SLF4J的LoggerFactory来创建一个日志记录器
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.InteractionService;
|
||||
@@ -14,6 +15,7 @@ import javax.annotation.Resource;
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/interaction")
|
||||
@SaCheckLogin
|
||||
public class InteractionController {
|
||||
|
||||
@Resource
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
@@ -19,6 +20,7 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@SaCheckLogin
|
||||
public class PostController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostController.class);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.ivmiku.tutorial.entity.PostTag;
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
@@ -14,22 +15,14 @@ import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/post-tag")
|
||||
@SaCheckLogin
|
||||
public class PostTagController {
|
||||
|
||||
// 日志记录器,用于记录控制器中的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostTagController.class);
|
||||
|
||||
// 通过@Autowired注解自动装配PostTagService服务
|
||||
@Autowired
|
||||
private PostTagService postTagService;
|
||||
|
||||
/**
|
||||
* 创建帖子标签接口。
|
||||
* 用户通过POST请求提交帖子标签数据,服务端接收并保存帖子标签。
|
||||
*
|
||||
* @param postTag 提交的帖子标签数据
|
||||
* @return Result 返回操作结果
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result createPostTag(@RequestBody PostTag postTag) {
|
||||
logger.info("开始创建帖子标签");
|
||||
@@ -38,13 +31,6 @@ public class PostTagController {
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帖子标签详情接口。
|
||||
* 根据帖子标签ID获取帖子标签的详细信息。
|
||||
*
|
||||
* @param id 帖子标签的唯一标识ID
|
||||
* @return Result 返回操作结果和帖子标签详情
|
||||
*/
|
||||
@GetMapping("/get/{id}")
|
||||
public Result getPostTag(@PathVariable Long id) {
|
||||
logger.info("开始获取帖子标签ID:{}的详细信息", id);
|
||||
@@ -57,14 +43,6 @@ public class PostTagController {
|
||||
return Result.ok(postTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新帖子标签接口。
|
||||
* 用户通过PUT请求提交更新后的帖子标签数据,服务端接收并更新帖子标签。
|
||||
*
|
||||
* @param id 要更新的帖子标签的唯一标识ID
|
||||
* @param postTag 更新后的帖子标签数据
|
||||
* @return Result 返回操作结果
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
public Result updatePostTag(@PathVariable Long id, @RequestBody PostTag postTag) {
|
||||
logger.info("开始更新帖子标签ID:{}", id);
|
||||
@@ -73,13 +51,6 @@ public class PostTagController {
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除帖子标签接口。
|
||||
* 根据帖子标签ID删除指定的帖子标签。
|
||||
*
|
||||
* @param id 要删除的帖子标签的唯一标识ID
|
||||
* @return Result 返回操作结果
|
||||
*/
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public Result deletePostTag(@PathVariable Long id) {
|
||||
logger.info("开始删除帖子标签ID:{}", id);
|
||||
@@ -88,25 +59,24 @@ public class PostTagController {
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前帖子所有标签列表接口。
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getPostTagList{postId}")
|
||||
public Result getPostTagList(@PathVariable("postId") Long postId) {
|
||||
@GetMapping("/getPostTagList/{postId}")
|
||||
public Result getPostTagList(@PathVariable Long postId) {
|
||||
logger.info("开始获取帖子标签列表");
|
||||
List<Tag> tags = postTagService.getPostTagList(postId);
|
||||
return Result.ok(tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前标签所有帖子列表接口。
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getTagPostList{tagId}")
|
||||
public Result getTagPostList(@PathVariable("tagId") Long tagId) {
|
||||
logger.info("开始获取帖子标签列表");
|
||||
@GetMapping("/getTagPostList/{tagId}")
|
||||
public Result getTagPostList(@PathVariable Long tagId) {
|
||||
logger.info("开始获取标签帖子列表");
|
||||
List<Post> posts = postTagService.getTagPostList(tagId);
|
||||
return Result.ok(posts);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/topTags")
|
||||
public Result getTopTags(@RequestParam(defaultValue = "10") int topN) {
|
||||
logger.info("开始获取前{}个使用次数最多的标签", topN);
|
||||
List<String> tags = postTagService.getTopTags(topN);
|
||||
return Result.ok(tags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.PostTagService;
|
||||
@@ -13,12 +14,12 @@ import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/tag")
|
||||
@SaCheckLogin
|
||||
public class TagController {
|
||||
|
||||
// 日志记录器,用于记录控制器中的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(TagController.class);
|
||||
|
||||
// 通过@Autowired注解自动装配TagService服务
|
||||
@Autowired
|
||||
private TagService tagService;
|
||||
|
||||
|
||||
@@ -51,4 +51,6 @@ public interface PostTagService extends IService<PostTag> {
|
||||
List<Post> getTagPostList(Long tagId);
|
||||
|
||||
List<Tag> getSortedTagList();
|
||||
|
||||
List<String> getTopTags(int topN);
|
||||
}
|
||||
|
||||
@@ -11,9 +11,12 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 帖子标签服务实现类。
|
||||
@@ -21,7 +24,6 @@ import java.util.List;
|
||||
@Service
|
||||
public class PostTagServiceImpl extends ServiceImpl<PostTagMapper, PostTag> implements PostTagService {
|
||||
|
||||
// 日志记录器,用于记录服务层的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostTagServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
@@ -33,39 +35,26 @@ public class PostTagServiceImpl extends ServiceImpl<PostTagMapper, PostTag> impl
|
||||
@Autowired
|
||||
private PostTagMapper postTagMapper;
|
||||
|
||||
/**
|
||||
* 创建帖子标签。
|
||||
* 将帖子标签数据保存到数据库。
|
||||
*
|
||||
* @param postTag 要保存的帖子标签数据
|
||||
*/
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
private static final String TAG_KEY = "tags";
|
||||
|
||||
@Override
|
||||
public void createPostTag(PostTag postTag) {
|
||||
logger.info("开始创建帖子标签");
|
||||
save(postTag);
|
||||
// 更新标签的使用次数
|
||||
redisTemplate.opsForZSet().incrementScore(TAG_KEY, postTag.getTagId().toString(), 1);
|
||||
logger.info("帖子标签创建成功,标签ID:{}", postTag.getPostTagId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取帖子标签详情。
|
||||
* 从数据库中根据帖子标签ID获取帖子标签的详细信息。
|
||||
*
|
||||
* @param postTagId 帖子标签的唯一标识ID
|
||||
* @return PostTag 返回查询到的帖子标签对象
|
||||
*/
|
||||
@Override
|
||||
public PostTag getPostTagById(Long postTagId) {
|
||||
logger.info("开始根据ID获取帖子标签详情,标签ID:{}", postTagId);
|
||||
return getById(postTagId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新帖子标签。
|
||||
* 根据帖子标签ID更新帖子标签的内容。
|
||||
*
|
||||
* @param postTagId 要更新的帖子标签的唯一标识ID
|
||||
* @param postTag 更新后的帖子标签数据
|
||||
*/
|
||||
@Override
|
||||
public void updatePostTag(Long postTagId, PostTag postTag) {
|
||||
logger.info("开始更新帖子标签,标签ID:{}", postTagId);
|
||||
@@ -74,15 +63,14 @@ public class PostTagServiceImpl extends ServiceImpl<PostTagMapper, PostTag> impl
|
||||
logger.info("帖子标签更新成功,标签ID:{}", postTagId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除帖子标签。
|
||||
* 根据帖子标签ID删除指定的帖子标签。
|
||||
*
|
||||
* @param postTagId 要删除的帖子标签的唯一标识ID
|
||||
*/
|
||||
@Override
|
||||
public void deletePostTag(Long postTagId) {
|
||||
logger.info("开始删除帖子标签,标签ID:{}", postTagId);
|
||||
// 删除标签时减少使用次数
|
||||
PostTag postTag = getById(postTagId);
|
||||
if (postTag != null) {
|
||||
redisTemplate.opsForZSet().incrementScore(TAG_KEY, postTag.getTagId().toString(), -1);
|
||||
}
|
||||
removeById(postTagId);
|
||||
logger.info("帖子标签删除成功,标签ID:{}", postTagId);
|
||||
}
|
||||
@@ -104,4 +92,14 @@ public class PostTagServiceImpl extends ServiceImpl<PostTagMapper, PostTag> impl
|
||||
logger.info("开始获取排序后的标签列表");
|
||||
return tagMapper.getSortedTagList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTopTags(int topN) {
|
||||
logger.info("开始获取前{}个使用次数最多的标签", topN);
|
||||
Set<String> tagSet = redisTemplate.opsForZSet().reverseRange(TAG_KEY, 0, topN - 1);
|
||||
if (tagSet == null) {
|
||||
return List.of(); // 或者返回一个空的List
|
||||
}
|
||||
return tagSet.stream().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -11,87 +11,66 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 标签服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
|
||||
|
||||
// 使用SLF4J的LoggerFactory创建一个Logger实例
|
||||
private static final Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private TagMapper tagMapper;
|
||||
|
||||
/**
|
||||
* 创建标签
|
||||
* @param tag 标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public void createTag(Tag tag) {
|
||||
try {
|
||||
// 保存标签到数据库
|
||||
save(tag);
|
||||
logger.info("Tag created successfully: {}", tag);
|
||||
logger.info("标签创建成功:{}", tag);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to create tag: {}", tag, e);
|
||||
logger.error("标签创建失败:{}", tag, e);
|
||||
throw e; // 重新抛出异常以便控制器能捕获并处理
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取标签
|
||||
* @param tagId 标签ID
|
||||
* @return 标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public Tag getTagById(Long tagId) {
|
||||
try {
|
||||
// 根据ID查询标签
|
||||
return getById(tagId);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to get tag by id: {}", tagId, e);
|
||||
return null;
|
||||
logger.error("获取标签ID:{}失败", tagId, e);
|
||||
throw e; // 重新抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签信息
|
||||
* @param tagId 标签ID
|
||||
* @param tag 更新后的标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public void updateTag(Long tagId, Tag tag) {
|
||||
try {
|
||||
// 设置标签ID并更新标签信息
|
||||
tag.setTagId(tagId);
|
||||
updateById(tag);
|
||||
logger.info("Tag updated successfully: {}", tag);
|
||||
logger.info("标签更新成功:{}", tag);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to update tag with id {}: {}", tagId, tag, e);
|
||||
logger.error("更新标签ID:{}失败", tagId, e);
|
||||
throw e; // 重新抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
* @param tagId 标签ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteTag(Long tagId) {
|
||||
try {
|
||||
// 根据ID删除标签
|
||||
removeById(tagId);
|
||||
logger.info("Tag deleted successfully with id: {}", tagId);
|
||||
logger.info("标签删除成功,ID:{}", tagId);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to delete tag with id: {}", tagId, e);
|
||||
logger.error("删除标签ID:{}失败", tagId, e);
|
||||
throw e; // 重新抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getTagList() {
|
||||
List<Tag> tags = tagMapper.selectList(null);
|
||||
if (tags != null) {
|
||||
return tags;
|
||||
try {
|
||||
return tagMapper.selectList(null);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取标签列表失败", e);
|
||||
throw e; // 重新抛出异常
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ wx.miniapp.configs[0].secret=989f155fcc3aee616568473faf1b1d3b
|
||||
spring.data.redis.host=localhost
|
||||
spring.data.redis.port=6379
|
||||
spring.data.redis.database=0
|
||||
spring.data.redis.lettuce.pool.max-active=32
|
||||
spring.data.redis.lettuce.pool.max-idle=16
|
||||
spring.data.redis.lettuce.pool.min-idle=8
|
||||
|
||||
spring.application.name=community
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-ocr</artifactId>
|
||||
<version>3.1.1076</version>
|
||||
<version>3.1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in New Issue
Block a user