Commit 98e0f115 authored by 刘基明's avatar 刘基明

接口设计

parent bb74dfcc
package com.tanpu.community.api.beans;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@ApiModel
@Data
public class CommentReq {
@NotEmpty(message = "主题id不能为空")
private String themeId;
@NotEmpty(message = "评论内容不能为空")
private String comment;
}
......@@ -15,17 +15,17 @@ public class TopicDataAnalysDTO {
@ApiModelProperty(value = "话题名称")
private String topicTitle;
@ApiModelProperty(value = "总浏览量")
private Long viewTotalAmount;
private Integer viewTotalAmount;
@ApiModelProperty(value = "话题页面浏览量")
private Long viewPageAmount;
private Integer viewPageAmount;
@ApiModelProperty(value = "发帖数")
private Long themeAmount;
private Integer themeAmount;
@ApiModelProperty(value = "回帖数")
private Long commentAmount;
private Integer commentAmount;
@ApiModelProperty(value = "总用户数")
private Long userTotalAmount;
private Integer userTotalAmount;
@ApiModelProperty(value = "发帖人数")
private Long posterAmount;
private Integer posterAmount;
@ApiModelProperty(value = "回帖人数")
private Long replIierAmount;
private Integer replIierAmount;
}
......@@ -3,7 +3,7 @@ package com.tanpu.community.api.constants;
public class RedisKeyConstant {
//话题页浏览量
public static final String TOPIC_VIEW_AMOUNT_="TOPIC_PAGE_VIEW_AMOUNT_";
public static final String TOPIC_PAGE_VIEW_AMOUNT_ ="TOPIC_PAGE_VIEW_AMOUNT_";
//话题总浏览量=总浏览量+带这个话题的帖子量
public static final String TOPIC_TOTAL_VIEW_AMOUNT_="TOPIC_TOTAL_VIEW_AMOUNT_";
//点赞量
......@@ -19,13 +19,13 @@ public class RedisKeyConstant {
//回帖数
public static final String TOPIC_COMMENT_AMOUNT_="TOPIC_COMMENT_AMOUNT_";
//总用户数=访问话题页+发帖+回帖(去重)
public static final String TOPIC_TOTAOL_USER_AMOUNT_="TOPIC_TOTAOL_USER_AMOUNT_";
public static final String TOPIC_TOTAL_USER_AMOUNT_ ="TOPIC_TOTAL_USER_AMOUNT_";
//访问话题人数
public static final String TOPIC_USER_VIEW_AMOUNT_="TOPIC_USER_VIEW_AMOUNT_";
//发帖人数
public static final String TOPIC_USER_POST_AMOUNT_ ="TOPIC_USER_POST_AMOUNT_";
public static final String TOPIC_POST_USER_AMOUNT_ ="TOPIC_POST_USER_AMOUNT_";
//回帖人数
public static final String TOPIC_USER_COMMET_AMOUNT_ ="TOPIC_USER_COMMET_AMOUNT_";
public static final String TOPIC_COMMENT_USER_AMOUNT_ ="TOPIC_COMMENT_USER_AMOUNT_";
public static final String THEME_VIEW_AMOUNT_="THEME_VIEW_AMOUNT_";
public static final String THEME_LIKE_AMOUNT_="THEME_LIKE_AMOUNT_";
......
package com.tanpu.community.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.max-active}")
private int maxActive;
@Value("${spring.redis.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.max-idle}")
private int maxIdle;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// maximum connection
jedisPoolConfig.setMaxTotal(maxActive);
// Maximum wait time when no connection is available in the pool
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// Maximum number of idle connections
jedisPoolConfig.setMinIdle(maxIdle);
// Other properties can be added by yourself
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
.poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
return template;
}
/**
* cache
*/
@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
.withCacheConfiguration("tempCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
.withCacheConfiguration("longCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
}
@Bean("communityKeyGenerator")
public KeyGenerator keyGenerator() {
return new RedisConfig.CommunityKeyGenerator();
}
public static class CommunityKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
// todo prefix 加到common
return "new_community_" + target.getClass().getSimpleName() + "_"
+ method.getName() + "_"
+ StringUtils.arrayToDelimitedString(params, "_");
}
}
}
//package com.tanpu.community.config;
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.cache.interceptor.KeyGenerator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.RedisPassword;
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.util.StringUtils;
//import redis.clients.jedis.JedisPoolConfig;
//
//import java.lang.reflect.Method;
//import java.time.Duration;
//
//@Configuration
//@EnableCaching
//public class RedisConfig {
//
// @Value("${spring.redis.host}")
// private String host;
// @Value("${spring.redis.port}")
// private int port;
// @Value("${spring.redis.password}")
// private String password;
// @Value("${spring.redis.timeout}")
// private int timeout;
// @Value("${spring.redis.max-active}")
// private int maxActive;
// @Value("${spring.redis.max-wait}")
// private long maxWaitMillis;
// @Value("${spring.redis.max-idle}")
// private int maxIdle;
//
// @Bean
// public JedisPoolConfig jedisPoolConfig() {
// JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// // maximum connection
// jedisPoolConfig.setMaxTotal(maxActive);
// // Maximum wait time when no connection is available in the pool
// jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// // Maximum number of idle connections
// jedisPoolConfig.setMinIdle(maxIdle);
// // Other properties can be added by yourself
// return jedisPoolConfig;
// }
//
// @Bean
// public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
// JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
// .poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
// RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
// redisStandaloneConfiguration.setHostName(host);
// redisStandaloneConfiguration.setPort(port);
// redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
// return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
// }
//
// @Bean
// public RedisTemplate<String, Object> redisTemplate() {
// RedisTemplate<String, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
//
// return template;
// }
//
// /**
// * cache
// */
// @Bean
// public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
// return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
// .withCacheConfiguration("tempCache",
// RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
// .withCacheConfiguration("longCache",
// RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
// }
//
// @Bean("communityKeyGenerator")
// public KeyGenerator keyGenerator() {
// return new RedisConfig.CommunityKeyGenerator();
// }
//
// public static class CommunityKeyGenerator implements KeyGenerator {
// public Object generate(Object target, Method method, Object... params) {
// // todo prefix 加到common
// return "new_community_" + target.getClass().getSimpleName() + "_"
// + method.getName() + "_"
// + StringUtils.arrayToDelimitedString(params, "_");
// }
// }
//}
package com.tanpu.community.controller;
import com.tanpu.community.api.beans.CommentReq;
import com.tanpu.community.api.beans.ForwardThemeDTO;
import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.manager.ThemeManager;
......@@ -52,9 +53,9 @@ public class ThemeController {
@ApiOperation("评论")
@RequestMapping(value = "/comment")
@ResponseBody
public String commetOnTheme(String themeId,String commet){
String user="liujm";
// themeManager.comment(themeId,user,commet);
public String commetOnTheme(@RequestBody CommentReq req){
String userId="liujm";
themeManager.comment(req,userId);
return "success";
}
......
package com.tanpu.community.manager;
import com.tanpu.community.api.beans.CommentReq;
import com.tanpu.community.api.beans.ForwardThemeDTO;
import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.api.constants.BlockTypeEnum;
......@@ -104,11 +105,12 @@ public class ThemeManager {
}
// 评论(对主题)
public void commentToTheme(String themeId, String userId, String comment) {
public void comment(CommentReq req, String userId) {
CommentEntity commentEntity = CommentEntity.builder()
.targetId(themeId)
.targetId(req.getThemeId())
.authorId(userId)
.content(comment)
.createBy(userId)
.content(req.getComment())
.commentType(CommentTypeEnum.THEME.getCode())
.build();
......
......@@ -12,7 +12,9 @@ import com.tanpu.community.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
......@@ -83,8 +85,11 @@ public class TopicManager {
public TopicDTO getDetail(String topicId) {
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("找不到话题,id:" + topicId);
}
TopicDTO topicDTO = ConvertUtil.topicEntityToDTO(topicEntity);
topicDTO.setViewAmount(redisService.getLong(RedisKeyConstant.TOPIC_VIEW_AMOUNT_ + topicId));
topicDTO.setViewAmount(redisService.getLong(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId));
topicDTO.setLikeAmount(redisService.getLong(RedisKeyConstant.TOPIC_LIKE_AMOUNT_ + topicId));
topicDTO.setUserAmount(redisService.getLong(RedisKeyConstant.TOPIC_USER_AMOUNT_ + topicId));
return topicDTO;
......@@ -111,6 +116,10 @@ public class TopicManager {
Long likeAmountByThemeIds = collectionService.getLikeAmountByThemeIds(themeIds);
Long bookAmountByThemeIds = collectionService.getBookAmountByThemeIds(themeIds);
Long commentAmountByThemeIds = commentService.getCommentAmountByThemeIds(themeIds);
Set<String> postUsers = themeService.getPostUserAmount(themeIds);
Set<String> commentUsers = commentService.getCommentUserAmount(themeIds);
HashSet<String> totalUsers = new HashSet<>(postUsers);
totalUsers.addAll(commentUsers);
redisService.set(RedisKeyConstant.TOPIC_LIKE_AMOUNT_ + topicId, likeAmountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_BOOK_AMOUNT_ + topicId, bookAmountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_COMMENT_AMOUNT_ + topicId, commentAmountByThemeIds);
......@@ -118,10 +127,11 @@ public class TopicManager {
redisService.set(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_THEME_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_TOTAOL_USER_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_DISCUSS_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_USER_POST_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_USER_COMMET_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_POST_USER_AMOUNT_ + topicId, (long) postUsers.size());
redisService.set(RedisKeyConstant.TOPIC_COMMENT_USER_AMOUNT_ + topicId, (long) commentUsers.size());
redisService.set(RedisKeyConstant.TOPIC_TOTAL_USER_AMOUNT_ + topicId, (long) totalUsers.size());
}
......@@ -131,17 +141,19 @@ public class TopicManager {
public TopicDataAnalysDTO queryDataAnalysis(String topicId) {
TopicDataAnalysDTO topicDataAnalysDTO = new TopicDataAnalysDTO();
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("话题未找到,id:" + topicId);
}
this.refreshRedisCache();
topicDataAnalysDTO.setId(topicId);
topicDataAnalysDTO.setTopicTitle(topicEntity.getTopicTitle());
// topicDataAnalysDTO.setCommentAmount();
// topicDataAnalysDTO.setPosterAmount();
// topicDataAnalysDTO.setReplIierAmount();
// topicDataAnalysDTO.setThemeAmount();
// topicDataAnalysDTO.setUserTotalAmount();
// topicDataAnalysDTO.setViewTotalAmount();
// topicDataAnalysDTO.setViewPageAmount();
// topicDataAnalysDTO.setCommentAmount();
topicDataAnalysDTO.setCommentAmount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_AMOUNT_ + topicId));
topicDataAnalysDTO.setPosterAmount(redisService.getInteger(RedisKeyConstant.TOPIC_POST_USER_AMOUNT_ + topicId));
topicDataAnalysDTO.setReplIierAmount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_USER_AMOUNT_ + topicId));
topicDataAnalysDTO.setThemeAmount(redisService.getInteger(RedisKeyConstant.TOPIC_THEME_AMOUNT_ + topicId));
topicDataAnalysDTO.setUserTotalAmount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_USER_AMOUNT_ + topicId));
topicDataAnalysDTO.setViewTotalAmount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId));
topicDataAnalysDTO.setViewPageAmount(redisService.getInteger(RedisKeyConstant.TOPIC_PAGE_VIEW_AMOUNT_ + topicId));
return topicDataAnalysDTO;
}
}
......
......@@ -40,7 +40,7 @@ public class CollectionService {
//根据用户、主题、类型查询
public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) {
return collectionMapper.selectOne(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type)
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getAuthorId, userId)
.eq(CollectionEntity::getTargetId, themeId));
}
......
......@@ -7,6 +7,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class CommentService {
......@@ -29,4 +31,10 @@ public class CommentService {
.in(CommentEntity::getTargetId, themeIds)))
.size();
}
public Set<String> getCommentUserAmount(List<String> themeIds) {
return commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
.in(CommentEntity::getTargetId, themeIds)))
.stream().map(CommentEntity::getAuthorId).collect(Collectors.toSet());
}
}
......@@ -7,6 +7,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ThemeService {
......@@ -41,4 +43,11 @@ public class ThemeService {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.orderByDesc(ThemeEntity::getUpdateTime));
}
//查询对应话题的发表用户集合
public Set<String> getPostUserAmount(List<String> themeIds) {
return themeMapper.selectBatchIds(themeIds).stream()
.map(ThemeEntity::getAuthorId)
.collect(Collectors.toSet());
}
}
......@@ -21,13 +21,13 @@ public class TopicService {
private TopicMapper topicMapper;
public List<TopicEntity> queryAll(){
public List<TopicEntity> queryAll() {
return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
public void addTopic(String topicTitle,String userId){
public void addTopic(String topicTitle, String userId) {
TopicEntity entity = TopicEntity.builder()
.topicTitle(topicTitle)
.createBy(userId)
......@@ -40,44 +40,44 @@ public class TopicService {
}
public void updateTopicToTop(String topicId){
public void updateTopicToTop(String topicId) {
TopicEntity topicEntity = new TopicEntity();
topicEntity.setIsTop(TopicStatusEnum.TRUE.getCode());
topicMapper.update(topicEntity,new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId,topicId));
topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId, topicId));
}
public void updateTopicNotTop(String topicId){
public void updateTopicNotTop(String topicId) {
TopicEntity topicEntity = new TopicEntity();
topicEntity.setIsTop(TopicStatusEnum.FALSE.getCode());
topicMapper.update(topicEntity,new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId,topicId));
topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId, topicId));
}
public void updateTopicToConceal(String topicId){
public void updateTopicToConceal(String topicId) {
TopicEntity topicEntity = new TopicEntity();
topicEntity.setIsConceal(TopicStatusEnum.TRUE.getCode());
topicMapper.update(topicEntity,new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId,topicId));
topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId, topicId));
}
public void updateTopicNotConceal(String topicId){
public void updateTopicNotConceal(String topicId) {
TopicEntity topicEntity = new TopicEntity();
topicEntity.setIsConceal(TopicStatusEnum.FALSE.getCode());
topicMapper.update(topicEntity,new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId,topicId));
topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getId, topicId));
}
public TopicEntity queryById(String topicId) {
return topicMapper.selectById(topicId);
}
public void modifyViewAmount(String topicId,long amount){
public void modifyViewAmount(String topicId, long amount) {
TopicEntity topicEntity = topicMapper.selectById(topicId);
Long oldAmount = topicEntity.getViewAmountModify();
topicEntity.setViewAmountModify(topicEntity.getViewAmountModify() + amount);
topicMapper.update(topicEntity,new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getViewAmountModify,oldAmount));
topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
.eq(TopicEntity::getViewAmountModify, oldAmount));
return;
}
}
......@@ -54,7 +54,7 @@ CREATE TABLE `comment` (
`content` text COMMENT '文本内容',
`author_id` varchar(64) NOT NULL COMMENT '作者id',
`target_id` varchar(64) NOT NULL COMMENT '评论的目标id',
`is_block` int(4) NOT NULL COMMENT '是否屏蔽',
`is_block` int(4) NOT NULL DEFAULT '0' COMMENT '是否屏蔽',
`create_by` varchar(64) DEFAULT '',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`update_by` varchar(64) DEFAULT '',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment