重构接口,加入"项目"模块
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
package com.lxyer.bbs.base;
|
||||
|
||||
import com.lxyer.bbs.base.user.UserRecord;
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
import com.lxyer.bbs.base.user.UserInfo;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.source.CacheSource;
|
||||
import org.redkale.source.DataSource;
|
||||
import org.redkale.source.FilterExpress;
|
||||
import org.redkale.source.FilterNode;
|
||||
import org.redkale.util.Sheet;
|
||||
import org.redkalex.cache.RedisCacheSource;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Lxy at 2017/10/3 13:50.
|
||||
@@ -36,8 +32,12 @@ public class BaseService<F extends UF,I extends UI> implements Service {
|
||||
|
||||
protected static final boolean winos = System.getProperty("os.name").contains("Window");
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
public DataSource getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
protected Sheet<I> createInfo(Sheet<F> fSheet){
|
||||
/*protected Sheet<I> createInfo(Sheet<F> fSheet){
|
||||
Sheet<I> sheet = new Sheet<>();
|
||||
|
||||
if (fSheet == null || fSheet.getTotal() < 1){
|
||||
@@ -53,7 +53,7 @@ public class BaseService<F extends UF,I extends UI> implements Service {
|
||||
}
|
||||
|
||||
return sheet;
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 批量设置用户信息
|
||||
@@ -61,7 +61,7 @@ public class BaseService<F extends UF,I extends UI> implements Service {
|
||||
* @param <I>
|
||||
* @return
|
||||
*/
|
||||
protected <I extends UI> Sheet<I> setIUser(Sheet<I> ufSheet){
|
||||
/*protected <I extends UI> Sheet<I> setIUser(Sheet<I> ufSheet){
|
||||
int[] userIds = ufSheet.stream().mapToInt(I::getUserid).toArray();
|
||||
|
||||
List<UserRecord> users = source.queryList(UserRecord.class, FilterNode.create("userId", FilterExpress.IN, userIds));
|
||||
@@ -70,16 +70,23 @@ public class BaseService<F extends UF,I extends UI> implements Service {
|
||||
x.setUser(user);
|
||||
});
|
||||
return ufSheet;
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 将含有用户外键的实体,转为info,并加入用户信息
|
||||
* @param uf
|
||||
* @return
|
||||
*/
|
||||
protected I setIUser(I uf){
|
||||
/*protected I setIUser(I uf){
|
||||
UserRecord user = source.find(UserRecord.class, uf.getUserid());
|
||||
|
||||
return (I) uf.setUser(user);
|
||||
}*/
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
public int currentUserId(String sessionid){
|
||||
if (sessionid == null) return 0;
|
||||
Object userid = sessions.getAndRefresh(sessionid, sessionExpireSeconds);
|
||||
return userid == null ? 0 : (Integer)userid;
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package com.lxyer.bbs.base;
|
||||
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
|
||||
/**
|
||||
* user foreign key (userId)
|
||||
* Created by liangxianyou at 2018/6/9 14:50.
|
||||
|
9
src/com/lxyer/bbs/base/iface/C.java
Normal file
9
src/com/lxyer/bbs/base/iface/C.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.lxyer.bbs.base.iface;
|
||||
|
||||
/**
|
||||
* 创建信息
|
||||
* Created by liangxianyou at 2018/6/16 17:43.
|
||||
*/
|
||||
public interface C<I extends CI> {
|
||||
I createInfo();
|
||||
}
|
7
src/com/lxyer/bbs/base/iface/CI.java
Normal file
7
src/com/lxyer/bbs/base/iface/CI.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.lxyer.bbs.base.iface;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/6/16 18:39.
|
||||
*/
|
||||
public interface CI<I extends CI> {
|
||||
}
|
32
src/com/lxyer/bbs/base/iface/CService.java
Normal file
32
src/com/lxyer/bbs/base/iface/CService.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.lxyer.bbs.base.iface;
|
||||
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/6/16 17:56.
|
||||
*/
|
||||
public interface CService<I extends UI> {
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
default <A extends C> Sheet<I> createInfo(Sheet<A> fSheet){
|
||||
Sheet<I> sheet = new Sheet<>();
|
||||
|
||||
if (fSheet == null || fSheet.getTotal() < 1){
|
||||
sheet.setTotal(0);
|
||||
sheet.setRows(new ArrayList<>());
|
||||
}else {
|
||||
int total = (int)fSheet.getTotal();
|
||||
List<I> rows = new ArrayList<>(total);
|
||||
fSheet.forEach(x->rows.add((I)x.createInfo()));
|
||||
|
||||
sheet.setTotal(total);
|
||||
sheet.setRows(rows);
|
||||
}
|
||||
|
||||
return sheet;
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package com.lxyer.bbs.base;
|
||||
package com.lxyer.bbs.base.iface;
|
||||
|
||||
import com.lxyer.bbs.base.user.UserRecord;
|
||||
|
38
src/com/lxyer/bbs/base/iface/UIService.java
Normal file
38
src/com/lxyer/bbs/base/iface/UIService.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.lxyer.bbs.base.iface;
|
||||
|
||||
import com.lxyer.bbs.base.user.UserRecord;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.source.DataSource;
|
||||
import org.redkale.source.FilterExpress;
|
||||
import org.redkale.source.FilterNode;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/6/16 18:25.
|
||||
*/
|
||||
public interface UIService<I extends UI> extends CService<I> {
|
||||
|
||||
DataSource getSource();
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
default Sheet<I> setIUser(Sheet<I> sheet){
|
||||
int[] userids = sheet.stream().mapToInt(I::getUserid).toArray();
|
||||
|
||||
List<UserRecord> users = getSource().queryList(UserRecord.class, FilterNode.create("userid", FilterExpress.IN, userids));
|
||||
sheet.forEach(x->{
|
||||
UserRecord user = users.stream().filter(u -> u.getUserid() == x.getUserid()).findAny().orElse(null);
|
||||
x.setUser(user);
|
||||
});
|
||||
return sheet;
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
default I setIUser(I i){
|
||||
UserRecord user = getSource().find(UserRecord.class, i.getUserid());
|
||||
|
||||
return (I) i.setUser(user);
|
||||
}
|
||||
|
||||
}
|
@@ -52,12 +52,6 @@ public class UserService extends BaseService {
|
||||
sessions.getAndRefresh(sessionid, sessionExpireSeconds);
|
||||
return userid == null ? null : findUserInfo((Integer) userid);
|
||||
}
|
||||
@RestMapping(name = "userid")
|
||||
public int currentUserId(String sessionid){
|
||||
if (sessionid == null) return 0;
|
||||
Object userid = sessions.getAndRefresh(sessionid, sessionExpireSeconds);
|
||||
return userid == null ? 0 : (Integer)userid;
|
||||
}
|
||||
|
||||
@RestMapping(name = "info", comment = "用户信息")
|
||||
public UserInfo findUserInfo(int userid) {
|
||||
|
@@ -3,6 +3,8 @@ package com.lxyer.bbs.comment;
|
||||
import javax.persistence.*;
|
||||
|
||||
import com.lxyer.bbs.base.UF;
|
||||
import com.lxyer.bbs.base.iface.C;
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
import com.lxyer.bbs.base.kit.LxyKit;
|
||||
import org.redkale.convert.json.*;
|
||||
|
||||
@@ -14,7 +16,7 @@ import java.io.Serializable;
|
||||
*/
|
||||
@Cacheable(interval = 5*60)
|
||||
@Table(catalog = "redbbs", name = "sys_comment", comment = "[评论表]")
|
||||
public class Comment implements Serializable, UF {
|
||||
public class Comment implements Serializable, C<CommentInfo> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.lxyer.bbs.comment;
|
||||
|
||||
import com.lxyer.bbs.base.UI;
|
||||
import com.lxyer.bbs.base.iface.CI;
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
import com.lxyer.bbs.base.user.UserRecord;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
@@ -11,7 +12,7 @@ import java.io.Serializable;
|
||||
*
|
||||
* @author lxyer
|
||||
*/
|
||||
public class CommentInfo implements UI<CommentInfo>,Serializable {
|
||||
public class CommentInfo implements UI<CommentInfo>,Serializable, CI<CommentInfo> {
|
||||
|
||||
@Column(comment = "[评论id]")
|
||||
private int commentid;
|
||||
|
@@ -2,6 +2,8 @@ package com.lxyer.bbs.comment;
|
||||
|
||||
import com.lxyer.bbs.base.BaseService;
|
||||
import com.lxyer.bbs.base.entity.ActLog;
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
import com.lxyer.bbs.base.iface.UIService;
|
||||
import com.lxyer.bbs.base.kit.LxyKit;
|
||||
import com.lxyer.bbs.base.kit.RetCodes;
|
||||
import com.lxyer.bbs.base.user.UserService;
|
||||
@@ -27,10 +29,10 @@ import static com.lxyer.bbs.base.kit.RetCodes.RET_COMMENT_PARA_ILLEGAL;
|
||||
* Created by Lxy at 2017/11/29 10:00.
|
||||
*/
|
||||
@RestService(automapping = true, comment = "评论服务")
|
||||
public class CommentService extends BaseService<Comment, CommentInfo> {
|
||||
public class CommentService extends BaseService implements UIService<CommentInfo> {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
/*@Resource
|
||||
private UserService userService;*/
|
||||
|
||||
@RestMapping(name = "save", comment = "评论保存")
|
||||
public RetResult commentSave(@RestSessionid String sessionid, @RestParam(name = "bean") Comment comment){
|
||||
@@ -46,7 +48,7 @@ public class CommentService extends BaseService<Comment, CommentInfo> {
|
||||
return RetCodes.retResult(RET_COMMENT_CONTENT_ILLEGAL, "评论内容无效");
|
||||
|
||||
if (comment.getCommentid() < 1) {
|
||||
int userid = userService.currentUserId(sessionid);
|
||||
int userid = currentUserId(sessionid);
|
||||
comment.setUserid(userid);
|
||||
comment.setCreatetime(System.currentTimeMillis());
|
||||
//todo:@用户处理
|
||||
@@ -63,7 +65,7 @@ public class CommentService extends BaseService<Comment, CommentInfo> {
|
||||
|
||||
@RestMapping(name = "query", auth = false,comment = "查询评论")
|
||||
public Sheet<CommentInfo> commentQuery(@RestSessionid String sessionid , int contentId, Flipper flipper){
|
||||
int userid = userService.currentUserId(sessionid);
|
||||
int userid = currentUserId(sessionid);
|
||||
|
||||
flipper.setSort("supportnum DESC,commentid ASC");
|
||||
Sheet<Comment> comments = source.querySheet(Comment.class, flipper, FilterNode.create("contentid", contentId));
|
||||
@@ -89,42 +91,35 @@ public class CommentService extends BaseService<Comment, CommentInfo> {
|
||||
Sheet<Comment> comments = source.querySheet(Comment.class, new Flipper().sort("createtime DESC"), FilterNode.create("userid", userid));
|
||||
|
||||
int[] contentIds = comments.stream().mapToInt(x -> x.getCommentid()).toArray();
|
||||
|
||||
List<Content> contents = source.queryList(Content.class, SelectColumn.createIncludes("contentid","title"), FilterNode.create("contentid", FilterExpress.IN, contentIds));
|
||||
|
||||
Sheet<CommentInfo> infos = new Sheet<>();
|
||||
List<CommentInfo> list = new ArrayList<>();
|
||||
|
||||
comments.forEach(x->{
|
||||
CommentInfo info = x.createInfo();
|
||||
Sheet<CommentInfo> infos = createInfo(comments);
|
||||
infos.forEach(x->{
|
||||
Content content = contents.stream().filter(k -> k.getContentid() == x.getContentid()).findFirst().orElse(new Content());
|
||||
info.setTitle(content.getTitle());
|
||||
list.add(info);
|
||||
x.setTitle(content.getTitle());
|
||||
});
|
||||
|
||||
infos.setRows(list);
|
||||
infos.setTotal(comments.getTotal());
|
||||
return infos;
|
||||
}
|
||||
|
||||
@RestMapping(name = "support", comment = "评论点赞")
|
||||
public RetResult support(@RestSessionid String sessionid, int commentid, int ok){
|
||||
int userid = userService.currentUserId(sessionid);
|
||||
int userid = currentUserId(sessionid);
|
||||
|
||||
ActLog actLog = source.find(ActLog.class, FilterNode.create("userid", userid).and("tid", commentid).and("cate", 10));
|
||||
if (actLog == null && ok == 1){
|
||||
actLog = new ActLog(10, commentid, userid);
|
||||
actLog.setCreatetime(System.currentTimeMillis());
|
||||
source.insert(actLog);
|
||||
}else if (actLog != null && actLog.getStatus() != ok){
|
||||
actLog.setStatus((short) -10);
|
||||
source.update(actLog);
|
||||
}else {
|
||||
return RetCodes.retResult(-1, ok == 1 ? "已赞" : "已取消赞");
|
||||
}
|
||||
source.findAsync(ActLog.class, FilterNode.create("userid", userid).and("tid", commentid).and("cate", 10)).thenAccept(actLog -> {
|
||||
if (actLog == null && ok == 1){
|
||||
actLog = new ActLog(10, commentid, userid);
|
||||
actLog.setCreatetime(System.currentTimeMillis());
|
||||
source.insert(actLog);
|
||||
}else if (actLog != null && actLog.getStatus() != ok){
|
||||
actLog.setStatus((short) -10);
|
||||
source.update(actLog);
|
||||
}/*else {
|
||||
return RetCodes.retResult(-1, ok == 1 ? "已赞" : "已取消赞");
|
||||
}*/
|
||||
|
||||
int count = source.getNumberResult(ActLog.class, FilterFunc.COUNT, 0, "logid", FilterNode.create("tid", commentid).and("status", 10)).intValue();
|
||||
source.updateColumn(Comment.class, commentid,"supportnum", count);
|
||||
int count = source.getNumberResult(ActLog.class, FilterFunc.COUNT, 0, "logid", FilterNode.create("tid", commentid).and("status", 10)).intValue();
|
||||
source.updateColumn(Comment.class, commentid,"supportnum", count);
|
||||
});
|
||||
|
||||
return RetResult.success();
|
||||
}
|
||||
|
@@ -1,12 +1,11 @@
|
||||
package com.lxyer.bbs.content;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import com.jfinal.kit.Kv;
|
||||
import com.lxyer.bbs.base.UF;
|
||||
import com.lxyer.bbs.base.iface.C;
|
||||
import com.lxyer.bbs.base.kit.LxyKit;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -15,7 +14,7 @@ import java.io.Serializable;
|
||||
*/
|
||||
@Cacheable(interval = 5*60)
|
||||
@Table(catalog = "redbbs", name = "sys_content", comment = "[内容表]")
|
||||
public class Content implements Serializable, UF<ContentInfo> {
|
||||
public class Content implements Serializable, C<ContentInfo> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.lxyer.bbs.content;
|
||||
|
||||
import com.lxyer.bbs.base.UI;
|
||||
import com.lxyer.bbs.base.iface.CI;
|
||||
import com.lxyer.bbs.base.iface.UI;
|
||||
import com.lxyer.bbs.base.user.UserRecord;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
@@ -9,7 +10,7 @@ import java.io.Serializable;
|
||||
/**
|
||||
* Created by Lxy at 2017/11/26 20:52.
|
||||
*/
|
||||
public class ContentInfo implements UI<ContentInfo>,Serializable {
|
||||
public class ContentInfo implements UI<ContentInfo>,Serializable, CI {
|
||||
|
||||
private int contentid;
|
||||
private int userid;
|
||||
|
@@ -3,6 +3,7 @@ package com.lxyer.bbs.content;
|
||||
import com.jfinal.kit.Kv;
|
||||
import com.lxyer.bbs.base.BaseService;
|
||||
import com.lxyer.bbs.base.entity.ActLog;
|
||||
import com.lxyer.bbs.base.iface.UIService;
|
||||
import com.lxyer.bbs.base.kit.RetCodes;
|
||||
import com.lxyer.bbs.base.user.UserInfo;
|
||||
import com.lxyer.bbs.base.user.UserService;
|
||||
@@ -18,7 +19,7 @@ import javax.annotation.Resource;
|
||||
* Created by Lxy at 2017/11/26 9:33.
|
||||
*/
|
||||
@RestService(automapping = true, comment = "内容管理")
|
||||
public class ContentService extends BaseService<Content,ContentInfo>{
|
||||
public class ContentService extends BaseService implements UIService<ContentInfo> {
|
||||
|
||||
@Resource
|
||||
protected UserService userService;
|
||||
@@ -32,6 +33,7 @@ public class ContentService extends BaseService<Content,ContentInfo>{
|
||||
public Sheet<ContentInfo> contentQuery(Flipper flipper, FilterNode filterNode){
|
||||
Sheet<Content> contents = source.querySheet(Content.class, flipper, filterNode);
|
||||
|
||||
createInfo(contents);
|
||||
Sheet<ContentInfo> infos = createInfo(contents);
|
||||
setIUser(infos);
|
||||
|
||||
@@ -134,7 +136,7 @@ public class ContentService extends BaseService<Content,ContentInfo>{
|
||||
|
||||
@RestMapping(name = "collectquery", comment = "收藏列表")
|
||||
public Sheet<ContentInfo> collectQuery(@RestSessionid String sessionid){
|
||||
int userid = userService.currentUserId(sessionid);
|
||||
int userid = currentUserId(sessionid);
|
||||
|
||||
Flipper flipper = new Flipper().sort("createtime DESC");
|
||||
FilterNode filterNode = FilterNode.create("cate", 20).and("status", 10).and("userid", userid);
|
||||
|
@@ -69,7 +69,7 @@ public class ContentServlet extends BaseServlet {
|
||||
@Override
|
||||
public String get() {
|
||||
UserRecord user = request.currentUser();
|
||||
if (user == null || user.getUserid() > 10_0003)
|
||||
if (user == null || user.getRoleid() != 0)
|
||||
contentService.incrViewNum(contentid);
|
||||
return "";
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package com.lxyer.bbs.servlet;
|
||||
import com.jfinal.kit.Kv;
|
||||
import com.lxyer.bbs.base.BaseServlet;
|
||||
import com.lxyer.bbs.base.user.UserInfo;
|
||||
import com.lxyer.bbs.comment.CommentInfo;
|
||||
import com.lxyer.bbs.content.ContentInfo;
|
||||
import org.redkale.net.http.HttpMapping;
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
@@ -12,7 +13,6 @@ import org.redkale.source.FilterNode;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
import static org.redkale.source.FilterExpress.GREATERTHAN;
|
||||
import static org.redkale.source.FilterExpress.GREATERTHANOREQUALTO;
|
||||
import static org.redkale.source.FilterExpress.NOTEQUAL;
|
||||
|
||||
@@ -20,14 +20,14 @@ import static org.redkale.source.FilterExpress.NOTEQUAL;
|
||||
/**
|
||||
* Created by Lxy at 2017/11/25 12:31.
|
||||
*/
|
||||
@WebServlet({"/"
|
||||
@WebServlet({"/","/project"
|
||||
/* ,"/article","/article/*" */
|
||||
})
|
||||
public class IndexServlet extends BaseServlet {
|
||||
|
||||
@HttpMapping(url = "/", auth = false, comment = "社区首页")
|
||||
public void abc(HttpRequest request, HttpResponse response){
|
||||
Flipper flipper = new Flipper().limit(30).sort("top DESC,createtime DESC");
|
||||
Flipper flipper = new Flipper().limit(15).sort("top DESC,createtime DESC");
|
||||
//置顶贴
|
||||
FilterNode topNode = FilterNode.create("status", NOTEQUAL, -10).and("top", GREATERTHANOREQUALTO, 20);
|
||||
Sheet<ContentInfo> top = contentService.contentQuery(flipper, setPrivate(topNode));
|
||||
@@ -66,4 +66,16 @@ public class IndexServlet extends BaseServlet {
|
||||
|
||||
finish("/article/index.html");
|
||||
}*/
|
||||
|
||||
//====================================项目相关====================================
|
||||
@HttpMapping(url = "/project", auth = false, comment = "项目首页")
|
||||
public void project(HttpRequest request, HttpResponse response){
|
||||
int contentid = 22;
|
||||
ContentInfo content = contentService.contentInfo(sessionid, contentid);
|
||||
Sheet<CommentInfo> comments = commentService.commentQuery(request.getSessionid(false) ,contentid, new Flipper().limit(30));
|
||||
|
||||
Kv kv = Kv.by("bean", content).set("comments", comments);
|
||||
finish("project/index.html", kv);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user