完善论坛接口逻辑

master
panqihua 5 years ago
parent 6681836d79
commit 9c00d463c6
  1. 31
      src/main/java/com/community/pocket/api/android/ForumController.java
  2. 8
      src/main/java/com/community/pocket/entity/po/Forum.java
  3. 18
      src/main/java/com/community/pocket/entity/po/ForumContent.java
  4. 2
      src/main/java/com/community/pocket/entity/po/android/MyInfo.java
  5. 74
      src/main/java/com/community/pocket/entity/vo/android/ForumContentVo.java
  6. 29
      src/main/java/com/community/pocket/entity/vo/android/ForumDataResponse.java
  7. 7
      src/main/java/com/community/pocket/entity/vo/android/ForumMyResponse.java
  8. 7
      src/main/java/com/community/pocket/entity/vo/android/ForumNewResponse.java
  9. 27
      src/main/java/com/community/pocket/entity/vo/android/ForumReplyForm.java
  10. 79
      src/main/java/com/community/pocket/entity/vo/android/ForumVo.java
  11. 86
      src/main/java/com/community/pocket/entity/vo/android/Info.java
  12. 7
      src/main/java/com/community/pocket/entity/vo/android/InfoResponse.java
  13. 17
      src/main/java/com/community/pocket/entity/vo/android/InfoWithScore.java
  14. 17
      src/main/java/com/community/pocket/entity/vo/android/QueryForum.java
  15. 46
      src/main/java/com/community/pocket/repository/android/ForumContentDao.java
  16. 24
      src/main/java/com/community/pocket/repository/android/ForumDao.java
  17. 19
      src/main/java/com/community/pocket/repository/android/UserDao.java

@ -5,9 +5,7 @@ import com.community.pocket.entity.po.ForumContent;
import com.community.pocket.entity.po.Notice;
import com.community.pocket.entity.po.android.Active;
import com.community.pocket.entity.po.android.Complain;
import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.entity.po.android.Token;
import com.community.pocket.entity.vo.android.ActiveForm;
import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.android.*;
@ -78,7 +76,7 @@ public class ForumController {
//加载最新帖子
@GetMapping("/forum/new")
public ForumNewResponse loadForumNew() {
List<Forum> forumList = forumDao.loadNewForum();
List<ForumVo> forumList = forumDao.loadNewForum();
if (forumList != null) {
ForumNewResponse response = new ForumNewResponse(Result.OK, ForumNewResponse.Msg.ok);
response.setForumList(forumList);
@ -147,7 +145,7 @@ public class ForumController {
@GetMapping("/forum/my")
public ForumMyResponse loadForumMy(Token token) {
if (tokenDao.checkToken(token)) {
List<Forum> forumList = forumDao.loadMyForum(token.getUsername());
List<ForumVo> forumList = forumDao.loadMyForum(token.getUsername());
ForumMyResponse response = new ForumMyResponse(Result.OK, ForumMyResponse.Msg.ok);
response.setForumList(forumList);
return response;
@ -160,7 +158,7 @@ public class ForumController {
@GetMapping("/my/info")
public InfoResponse loadInfo(Token token) {
if (tokenDao.checkToken(token)) {
MyInfo myInfo = userDao.queryUser(token.getUsername());
Info myInfo = userDao.queryUser(token.getUsername());
if (myInfo != null) {
InfoResponse response = new InfoResponse(Result.OK, InfoResponse.Msg.ok);
response.setMyInfo(myInfo);
@ -172,4 +170,27 @@ public class ForumController {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.token);
}
}
//获取帖子详情数据
@GetMapping("/forum/content")
public ForumDataResponse loadData(QueryForum queryForum) {
List<ForumContentVo> forumContents = forumContentDao.find(queryForum);
ForumDataResponse response = new ForumDataResponse(Result.OK, ForumDataResponse.Msg.ok);
response.setForumContentList(forumContents);
return response;
}
//回帖
@PostMapping("/forum/content/reply")
public ForumDataResponse sendReply(ForumReplyForm replyForm) {
if (tokenDao.checkToken(replyForm)) {
if (forumContentDao.save(replyForm) != null && forumDao.update(replyForm)) {
return new ForumDataResponse(Result.OK, ForumDataResponse.Msg.reply_ok);
} else {
return new ForumDataResponse(Result.FAIL, ForumDataResponse.Msg.reply_fail);
}
} else {
return new ForumDataResponse(Result.FAIL, ForumDataResponse.Msg.token);
}
}
}

@ -2,7 +2,6 @@ package com.community.pocket.entity.po;
import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.util.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@ -10,9 +9,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Document(TableName.forum)
public class Forum {
@JsonIgnore
private ObjectId id;
//发帖时间
private Long time;
//帖子标题
@ -24,7 +21,6 @@ public class Forum {
//缩略内容
private String content;
@JsonIgnore
private MyInfo myInfo;
/**
* 帖子类型
@ -98,10 +94,6 @@ public class Forum {
return id;
}
public String getForumId(){
return this.id.toString();
}
public void setId(ObjectId id) {
this.id = id;
}

@ -9,14 +9,16 @@ import org.springframework.data.mongodb.core.mapping.Document;
*/
@Document(TableName.forumContent)
public class ForumContent {
//帖子id
//帖子详情id
private ObjectId id;
//帖子id
private String forumId;
//用户名
private String username;
//发帖时间
private Long time;
//楼层
private Integer tower;
private Long tower;
//帖子正文
private String content;
@ -44,11 +46,11 @@ public class ForumContent {
this.time = time;
}
public Integer getTower() {
public Long getTower() {
return tower;
}
public void setTower(Integer tower) {
public void setTower(Long tower) {
this.tower = tower;
}
@ -59,4 +61,12 @@ public class ForumContent {
public void setContent(String content) {
this.content = content;
}
public String getForumId() {
return forumId;
}
public void setForumId(String forumId) {
this.forumId = forumId;
}
}

@ -1,7 +1,6 @@
package com.community.pocket.entity.po.android;
import com.community.pocket.util.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@ -14,7 +13,6 @@ public class MyInfo {
@Id
private String username;
//密码
@JsonIgnore
private String password;
//信用分
private Integer creditScore;

@ -0,0 +1,74 @@
package com.community.pocket.entity.vo.android;
public class ForumContentVo {
//帖子详情id
private String id;
//帖子id
private String forumId;
//用户名
private String username;
//发帖时间
private Long time;
//楼层
private Long tower;
//帖子正文
private String content;
private InfoWithScore myInfo;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getForumId() {
return forumId;
}
public void setForumId(String forumId) {
this.forumId = forumId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public Long getTower() {
return tower;
}
public void setTower(Long tower) {
this.tower = tower;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public InfoWithScore getMyInfo() {
return myInfo;
}
public void setMyInfo(InfoWithScore myInfo) {
this.myInfo = myInfo;
}
}

@ -0,0 +1,29 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.vo.Result;
import java.util.List;
public class ForumDataResponse extends AndroidResponse<ForumDataResponse.Msg> {
private List<ForumContentVo> forumContentList;
public ForumDataResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public List<ForumContentVo> getForumContentList() {
return forumContentList;
}
public void setForumContentList(List<ForumContentVo> forumContentList) {
this.forumContentList = forumContentList;
}
public enum Msg implements CustomMessage {
ok,
fail,
reply_ok,
reply_fail,
token
}
}

@ -1,6 +1,5 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.vo.Result;
import java.util.List;
@ -9,17 +8,17 @@ import java.util.List;
* 我的帖子响应结果
*/
public class ForumMyResponse extends AndroidResponse<ForumMyResponse.Msg>{
private List<Forum> forumList;
private List<ForumVo> forumList;
public ForumMyResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public List<Forum> getForumList() {
public List<ForumVo> getForumList() {
return forumList;
}
public void setForumList(List<Forum> forumList) {
public void setForumList(List<ForumVo> forumList) {
this.forumList = forumList;
}

@ -1,6 +1,5 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.vo.Result;
import java.util.List;
@ -10,13 +9,13 @@ import java.util.List;
*/
public class ForumNewResponse extends AndroidResponse<ForumNewResponse.Msg> {
private List<Forum> forumList;
private List<ForumVo> forumList;
public List<Forum> getForumList() {
public List<ForumVo> getForumList() {
return forumList;
}
public void setForumList(List<Forum> forumList) {
public void setForumList(List<ForumVo> forumList) {
this.forumList = forumList;
}

@ -0,0 +1,27 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.Token;
/**
* 回复表单
*/
public class ForumReplyForm extends Token {
private String forumId;
private String content;
public String getForumId() {
return forumId;
}
public void setForumId(String forumId) {
this.forumId = forumId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

@ -0,0 +1,79 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Forum;
/**
* 帖子列表
*/
public class ForumVo {
//帖子
private String id;
//发帖时间
private Long time;
//帖子标题
private String title;
//回复数
private Integer reply;
//帖子类型
private Forum.ForumType forumType;
//缩略内容
private String content;
//发帖人
private String username;
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getReply() {
return reply;
}
public void setReply(Integer reply) {
this.reply = reply;
}
public Forum.ForumType getForumType() {
return forumType;
}
public void setForumType(Forum.ForumType forumType) {
this.forumType = forumType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

@ -0,0 +1,86 @@
package com.community.pocket.entity.vo.android;
import java.util.List;
public class Info {
//用户名
private String username;
//信用分
private Integer creditScore;
//头像
private String headImg;
//最近发帖数
private Integer recentPosts;
//最近访客数
private Integer recentVisitors;
//手机号
private String mobie;
//邮箱
private String email;
//信用分历史记录
private List<Integer> scoreHistory;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getCreditScore() {
return creditScore;
}
public void setCreditScore(Integer creditScore) {
this.creditScore = creditScore;
}
public String getHeadImg() {
return headImg;
}
public void setHeadImg(String headImg) {
this.headImg = headImg;
}
public Integer getRecentPosts() {
return recentPosts;
}
public void setRecentPosts(Integer recentPosts) {
this.recentPosts = recentPosts;
}
public Integer getRecentVisitors() {
return recentVisitors;
}
public void setRecentVisitors(Integer recentVisitors) {
this.recentVisitors = recentVisitors;
}
public String getMobie() {
return mobie;
}
public void setMobie(String mobie) {
this.mobie = mobie;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Integer> getScoreHistory() {
return scoreHistory;
}
public void setScoreHistory(List<Integer> scoreHistory) {
this.scoreHistory = scoreHistory;
}
}

@ -1,23 +1,22 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.entity.vo.Result;
/**
* 个人信息响应
*/
public class InfoResponse extends AndroidResponse<InfoResponse.Msg>{
private MyInfo myInfo;
private Info myInfo;
public InfoResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public MyInfo getMyInfo() {
public Info getMyInfo() {
return myInfo;
}
public void setMyInfo(MyInfo myInfo) {
public void setMyInfo(Info myInfo) {
this.myInfo = myInfo;
}

@ -0,0 +1,17 @@
package com.community.pocket.entity.vo.android;
/**
*
*/
public class InfoWithScore {
//信用分
private Integer creditScore;
public Integer getCreditScore() {
return creditScore;
}
public void setCreditScore(Integer creditScore) {
this.creditScore = creditScore;
}
}

@ -0,0 +1,17 @@
package com.community.pocket.entity.vo.android;
/**
* 查找帖子
*/
public class QueryForum {
//帖子id
private String forumId;
public String getForumId() {
return forumId;
}
public void setForumId(String forumId) {
this.forumId = forumId;
}
}

@ -2,27 +2,69 @@ package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.po.ForumContent;
import com.community.pocket.entity.vo.android.ForumContentVo;
import com.community.pocket.entity.vo.android.ForumForm;
import com.community.pocket.entity.vo.android.ForumReplyForm;
import com.community.pocket.entity.vo.android.QueryForum;
import com.community.pocket.repository.BaseDao;
import com.community.pocket.util.TableName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 帖子正文详情
*/
@Repository
public class ForumContentDao extends BaseDao<ForumContent> {
private static final Logger LOG = LoggerFactory.getLogger(ForumDao.class);
@Autowired
private ForumDao forumDao;
//关联用户表查询
private static final LookupOperation lookupOperation = LookupOperation.newLookup().
from(TableName.info). //关联从表名
localField("username"). //主表关联字段
foreignField("_id").//从表关联的字段
as("myInfo"); //查询结果名
//查找帖子详情
public List<ForumContentVo> find(QueryForum queryForum) {
Aggregation aggregation = Aggregation.newAggregation(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.match(Criteria.where("forumId").is(queryForum.getForumId())));
return mongoTemplate.aggregate(aggregation, TableName.forumContent, ForumContentVo.class).getMappedResults();
}
/**
* 保存回复
*/
public ForumContent save(ForumReplyForm forumReplyForm) {
ForumContent forumContent = new ForumContent();
forumContent.setContent(forumReplyForm.getContent());
forumContent.setForumId(forumReplyForm.getForumId());
forumContent.setTime(System.currentTimeMillis());
long tower = mongoTemplate.count(new Query(Criteria.where("forumId").is(forumContent.getForumId())), entityClass());
forumContent.setTower(tower + 1);
forumContent.setUsername(forumReplyForm.getUsername());
return save(forumContent);
}
/**
* 保存帖子详情
*/
public ForumContent save(ForumForm forumForm, Forum forum) {
ForumContent forumContent = new ForumContent();
forumContent.setContent(forumForm.getContent());
forumContent.setId(forum.getId());
forumContent.setForumId(forum.getId().toString());
forumContent.setTime(forum.getTime());
forumContent.setTower(1);
forumContent.setTower(1L);
forumContent.setUsername(forum.getUsername());
return save(forumContent);
}

@ -1,12 +1,10 @@
package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.vo.android.ActiveForm;
import com.community.pocket.entity.vo.android.ComplainForm;
import com.community.pocket.entity.vo.android.ForumForm;
import com.community.pocket.entity.vo.android.ForumHot;
import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.BaseDao;
import com.community.pocket.util.TableName;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@ -16,6 +14,7 @@ import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
@ -32,6 +31,12 @@ public class ForumDao extends BaseDao<Forum> {
@Value("${forum.content-length}")
private int contentLength;
//添加回复数
public boolean update(ForumReplyForm forumReplyForm) {
return mongoTemplate.updateFirst(new Query(Criteria.where("id").is(new ObjectId(forumReplyForm.getForumId()))),
new Update().inc("reply", 1), entityClass()).wasAcknowledged();
}
//关联用户表查询
private static final LookupOperation lookupOperation = LookupOperation.newLookup().
from(TableName.info). //关联从表名
@ -45,7 +50,7 @@ public class ForumDao extends BaseDao<Forum> {
List<Forum> forumList =mongoTemplate.find(new Query(Criteria.where("forumType").is(type)).with(Sort.by("reply").descending()).limit(num),entityClass());
for(Forum forum:forumList){
ForumHot forumHot=new ForumHot();
forumHot.setForumId(forum.getForumId());
forumHot.setForumId(forum.getId().toString());
forumHot.setReply(forum.getReply());
forumHot.setTitle(forum.getTitle());
forumHots.add(forumHot);
@ -54,23 +59,24 @@ public class ForumDao extends BaseDao<Forum> {
}
//查找帖子
private List<Forum> loadForum(AggregationOperation ...operation) {
private List<ForumVo> loadForum(AggregationOperation... operation) {
try {
Aggregation aggregation = Aggregation.newAggregation(operation);
return mongoTemplate.aggregate(aggregation,TableName.forum, Forum.class).getMappedResults();
return mongoTemplate.aggregate(aggregation, TableName.forum, ForumVo.class).getMappedResults();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//查找所有帖子
public List<Forum> loadNewForum(){
public List<ForumVo> loadNewForum() {
return loadForum(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.sort(Sort.by("time").descending()));
}
//查找最新帖子
public List<Forum> loadMyForum(String username){
public List<ForumVo> loadMyForum(String username) {
return loadForum(lookupOperation, Aggregation.unwind("myInfo"), Aggregation.match(Criteria.where("username").is(username)), Aggregation.sort(Sort.by("time").descending()));
}

@ -128,8 +128,23 @@ public class UserDao extends BaseDao<MyInfo> {
}
//获取个人信息
public MyInfo queryUser(String username){
return mongoTemplate.findOne(new Query(Criteria.where("username").is(username)),entityClass());
public Info queryUser(String username) {
MyInfo myInfo = mongoTemplate.findOne(new Query(Criteria.where("username").is(username)), entityClass());
if (myInfo != null) {
Info info = new Info();
info.setUsername(myInfo.getUsername());
info.setCreditScore(myInfo.getCreditScore());
info.setEmail(myInfo.getEmail());
info.setHeadImg(myInfo.getHeadImg());
info.setMobie(myInfo.getMobie());
info.setRecentPosts(myInfo.getRecentPosts());
info.setRecentVisitors(myInfo.getRecentVisitors());
info.setScoreHistory(myInfo.getScoreHistory());
return info;
} else {
return null;
}
}
//修改密码

Loading…
Cancel
Save