Skip to content

Commit 25e041b

Browse files
committed
add posts
1 parent 5d8c714 commit 25e041b

24 files changed

+2067
-0
lines changed

posts/apis/models/post.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package models
2+
3+
import (
4+
jimApiModels "github.com/juggleim/jugglechat-server/apis/models"
5+
)
6+
7+
type Posts struct {
8+
Items []*Post `json:"items"`
9+
IsFinished bool `json:"is_finished"`
10+
}
11+
12+
type Post struct {
13+
PostId string `json:"post_id"`
14+
Content *PostContent `json:"content"`
15+
UserInfo *jimApiModels.UserObj `json:"user_info"`
16+
CreatedTime int64 `json:"created_time"`
17+
UpdatedTime int64 `json:"updated_time"`
18+
Reactions map[string][]*PostReaction `json:"reactions"`
19+
TopComments []*PostComment `json:"top_comments"`
20+
}
21+
22+
type PostIds struct {
23+
PostIds []string `json:"post_ids"`
24+
}
25+
26+
type PostCommentIds struct {
27+
CommentIds []string `json:"comment_ids"`
28+
}
29+
30+
// type Reaction struct {
31+
// Value string `json:"value"`
32+
// UserInfo *jimApiModels.UserObj `json:"user_info"`
33+
// }
34+
35+
type PostContent struct {
36+
Text string `json:"text"`
37+
Images []*PostContentImage `json:"images"`
38+
Video *PostContentVideo `json:"video"`
39+
}
40+
41+
type PostContentImage struct {
42+
Url string `json:"url"`
43+
TumbnailUrl string `json:"thumbnail_url"`
44+
Height int `json:"height"`
45+
Width int `json:"width"`
46+
}
47+
48+
type PostContentVideo struct {
49+
Url string `json:"url"`
50+
SnapshotUrl string `json:"snapshot_url"`
51+
Duration int `json:"duration"`
52+
Height int `json:"height"`
53+
Width int `json:"width"`
54+
}
55+
56+
type PostComment struct {
57+
CommentId string `json:"comment_id"`
58+
PostId string `json:"post_id"`
59+
ParentCommentId string `json:"parent_comment_id"`
60+
Text string `json:"text"`
61+
ParentUserId string `json:"parent_user_id,omitempty"`
62+
ParentUserInfo *jimApiModels.UserObj `json:"parent_user_info"`
63+
UserInfo *jimApiModels.UserObj `json:"user_info"`
64+
CreatedTime int64 `json:"created_time"`
65+
UpdatedTime int64 `json:"updated_time"`
66+
}
67+
68+
type PostComments struct {
69+
Items []*PostComment `json:"items"`
70+
IsFinished bool `json:"is_finished"`
71+
}

posts/apis/models/reaction.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package models
2+
3+
import (
4+
jimApiModels "github.com/juggleim/jugglechat-server/apis/models"
5+
)
6+
7+
type PostReaction struct {
8+
PostId string `json:"post_id,omitempty"`
9+
Key string `json:"key"`
10+
Value string `json:"value"`
11+
Timestamp int64 `json:"timestamp"`
12+
UserInfo *jimApiModels.UserObj `json:"user_info,omitempty"`
13+
}
14+
15+
type PostReactions struct {
16+
Reactions []*PostReaction `json:"reactions"`
17+
}

posts/apis/post.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package apis
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/juggleim/jugglechat-server/commons/ctxs"
6+
"github.com/juggleim/jugglechat-server/commons/errs"
7+
"github.com/juggleim/jugglechat-server/commons/responses"
8+
utils "github.com/juggleim/jugglechat-server/commons/tools"
9+
apimodels "github.com/juggleim/jugglechat-server/posts/apis/models"
10+
"github.com/juggleim/jugglechat-server/posts/services"
11+
)
12+
13+
func PostAdd(ctx *gin.Context) {
14+
req := apimodels.Post{}
15+
if err := ctx.BindJSON(&req); err != nil {
16+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
17+
return
18+
}
19+
code, resp := services.PostAdd(ctxs.ToCtx(ctx), &req)
20+
if code != errs.IMErrorCode_SUCCESS {
21+
responses.ErrorHttpResp(ctx, code)
22+
return
23+
}
24+
responses.SuccessHttpResp(ctx, resp)
25+
}
26+
27+
func PostUpdate(ctx *gin.Context) {
28+
req := apimodels.Post{}
29+
if err := ctx.BindJSON(&req); err != nil {
30+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
31+
return
32+
}
33+
code := services.PostUpdate(ctxs.ToCtx(ctx), &req)
34+
if code != errs.IMErrorCode_SUCCESS {
35+
responses.ErrorHttpResp(ctx, code)
36+
return
37+
}
38+
responses.SuccessHttpResp(ctx, nil)
39+
}
40+
41+
func PostDel(ctx *gin.Context) {
42+
req := apimodels.PostIds{}
43+
if err := ctx.BindJSON(&req); err != nil {
44+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
45+
return
46+
}
47+
code := services.PostDel(ctxs.ToCtx(ctx), &req)
48+
if code != errs.IMErrorCode_SUCCESS {
49+
responses.ErrorHttpResp(ctx, code)
50+
return
51+
}
52+
responses.SuccessHttpResp(ctx, nil)
53+
}
54+
55+
func QryPosts(ctx *gin.Context) {
56+
var limit int64 = 20
57+
limitStr := ctx.Query("limit")
58+
var err error
59+
if limitStr != "" {
60+
limit, err = utils.String2Int64(limitStr)
61+
if err != nil {
62+
limit = 20
63+
}
64+
}
65+
var start int64
66+
startTimeStr := ctx.Query("start")
67+
start, err = utils.String2Int64(startTimeStr)
68+
if err != nil {
69+
start = 0
70+
}
71+
var isPositive bool = false
72+
orderStr := ctx.Query("order")
73+
order, err := utils.String2Int64(orderStr)
74+
if err == nil {
75+
if order == 1 {
76+
isPositive = true
77+
}
78+
}
79+
code, resp := services.QryPosts(ctxs.ToCtx(ctx), start, limit, isPositive)
80+
if code != errs.IMErrorCode_SUCCESS {
81+
responses.ErrorHttpResp(ctx, code)
82+
return
83+
}
84+
responses.SuccessHttpResp(ctx, resp)
85+
}
86+
87+
func PostInfo(ctx *gin.Context) {
88+
postId := ctx.Query("post_id")
89+
if postId == "" {
90+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
91+
return
92+
}
93+
code, resp := services.QryPostInfo(ctxs.ToCtx(ctx), postId)
94+
if code != errs.IMErrorCode_SUCCESS {
95+
responses.ErrorHttpResp(ctx, code)
96+
return
97+
}
98+
responses.SuccessHttpResp(ctx, resp)
99+
}

posts/apis/postcomment.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package apis
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/juggleim/jugglechat-server/commons/ctxs"
6+
"github.com/juggleim/jugglechat-server/commons/errs"
7+
"github.com/juggleim/jugglechat-server/commons/responses"
8+
utils "github.com/juggleim/jugglechat-server/commons/tools"
9+
apimodels "github.com/juggleim/jugglechat-server/posts/apis/models"
10+
"github.com/juggleim/jugglechat-server/posts/services"
11+
)
12+
13+
func PostCommentAdd(ctx *gin.Context) {
14+
req := apimodels.PostComment{}
15+
if err := ctx.BindJSON(&req); err != nil || req.PostId == "" {
16+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
17+
return
18+
}
19+
code, resp := services.PostCommentAdd(ctxs.ToCtx(ctx), &req)
20+
if code != errs.IMErrorCode_SUCCESS {
21+
responses.ErrorHttpResp(ctx, code)
22+
return
23+
}
24+
responses.SuccessHttpResp(ctx, resp)
25+
}
26+
27+
func PostCommentUpdate(ctx *gin.Context) {
28+
req := apimodels.PostComment{}
29+
if err := ctx.BindJSON(&req); err != nil {
30+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
31+
return
32+
}
33+
code := services.PostCommentUpdate(ctxs.ToCtx(ctx), &req)
34+
if code != errs.IMErrorCode_SUCCESS {
35+
responses.ErrorHttpResp(ctx, code)
36+
return
37+
}
38+
responses.SuccessHttpResp(ctx, nil)
39+
}
40+
41+
func PostCommentDel(ctx *gin.Context) {
42+
req := apimodels.PostCommentIds{}
43+
if err := ctx.BindJSON(&req); err != nil {
44+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
45+
return
46+
}
47+
code := services.PostCommentDel(ctxs.ToCtx(ctx), &req)
48+
if code != errs.IMErrorCode_SUCCESS {
49+
responses.ErrorHttpResp(ctx, code)
50+
return
51+
}
52+
responses.SuccessHttpResp(ctx, nil)
53+
}
54+
55+
func QryPostComments(ctx *gin.Context) {
56+
postId := ctx.Query("post_id")
57+
if postId == "" {
58+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
59+
return
60+
}
61+
var limit int64 = 20
62+
limitStr := ctx.Query("limit")
63+
var err error
64+
if limitStr != "" {
65+
limit, err = utils.String2Int64(limitStr)
66+
if err != nil {
67+
limit = 20
68+
}
69+
}
70+
var start int64
71+
startTimeStr := ctx.Query("start")
72+
start, err = utils.String2Int64(startTimeStr)
73+
if err != nil {
74+
start = 0
75+
}
76+
var isPositive bool = false
77+
orderStr := ctx.Query("order")
78+
order, err := utils.String2Int64(orderStr)
79+
if err == nil {
80+
if order == 1 {
81+
isPositive = true
82+
}
83+
}
84+
code, resp := services.QryPostComments(ctxs.ToCtx(ctx), postId, start, limit, isPositive)
85+
if code != errs.IMErrorCode_SUCCESS {
86+
responses.ErrorHttpResp(ctx, code)
87+
return
88+
}
89+
responses.SuccessHttpResp(ctx, resp)
90+
}

posts/apis/reaction.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package apis
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/juggleim/jugglechat-server/commons/ctxs"
6+
"github.com/juggleim/jugglechat-server/commons/errs"
7+
"github.com/juggleim/jugglechat-server/commons/responses"
8+
"github.com/juggleim/jugglechat-server/posts/apis/models"
9+
"github.com/juggleim/jugglechat-server/posts/services"
10+
)
11+
12+
func AddPostReaction(ctx *gin.Context) {
13+
req := models.PostReaction{}
14+
if err := ctx.BindJSON(&req); err != nil {
15+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
16+
return
17+
}
18+
code := services.AddPostReaction(ctxs.ToCtx(ctx), &req)
19+
if code != errs.IMErrorCode_SUCCESS {
20+
responses.ErrorHttpResp(ctx, code)
21+
return
22+
}
23+
responses.SuccessHttpResp(ctx, nil)
24+
}
25+
26+
func DelPostReaction(ctx *gin.Context) {
27+
req := models.PostReaction{}
28+
if err := ctx.BindJSON(&req); err != nil {
29+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
30+
return
31+
}
32+
code := services.DelPostReaction(ctxs.ToCtx(ctx), &req)
33+
if code != errs.IMErrorCode_SUCCESS {
34+
responses.ErrorHttpResp(ctx, code)
35+
return
36+
}
37+
responses.SuccessHttpResp(ctx, nil)
38+
}
39+
40+
func QryPostReactions(ctx *gin.Context) {
41+
postId := ctx.Query("post_id")
42+
if postId == "" {
43+
responses.ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
44+
return
45+
}
46+
code, resp := services.QryPostReactions(ctxs.ToCtx(ctx), postId)
47+
if code != errs.IMErrorCode_SUCCESS {
48+
responses.ErrorHttpResp(ctx, code)
49+
return
50+
}
51+
responses.SuccessHttpResp(ctx, resp)
52+
}

0 commit comments

Comments
 (0)