mirror of
https://github.com/m5stack/StackChan.git
synced 2026-04-27 19:12:40 +00:00
server code 4/27
This commit is contained in:
@@ -8,16 +8,22 @@ package post
|
||||
import (
|
||||
"context"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model"
|
||||
"stackChan/internal/model/do"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
"stackChan/api/post/v1"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) CreatePost(ctx context.Context, req *v1.CreatePostReq) (res *v1.CreatePostRes, err error) {
|
||||
device, err := dao.Device.Ctx(ctx).Where("mac", req.Mac).One()
|
||||
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
|
||||
if mac == "" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
|
||||
}
|
||||
device, err := dao.Device.Ctx(ctx).Where("mac", mac).One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -25,7 +31,7 @@ func (c *ControllerV1) CreatePost(ctx context.Context, req *v1.CreatePostReq) (r
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidRequest, "The device does not exist or the Mac address is incorrect")
|
||||
}
|
||||
insertId, err := dao.DevicePost.Ctx(ctx).Data(do.DevicePost{
|
||||
Mac: req.Mac,
|
||||
Mac: mac,
|
||||
ContentText: req.ContentText,
|
||||
ContentImage: req.ContentImage,
|
||||
}).InsertAndGetId()
|
||||
|
||||
@@ -8,15 +8,24 @@ package post
|
||||
import (
|
||||
"context"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model"
|
||||
"stackChan/internal/model/do"
|
||||
|
||||
"stackChan/api/post/v1"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) CreatePostComment(ctx context.Context, req *v1.CreatePostCommentReq) (res *v1.CreatePostCommentRes, err error) {
|
||||
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
|
||||
if mac == "" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
|
||||
}
|
||||
id, err := dao.DevicePostComment.Ctx(ctx).Data(do.DevicePostComment{
|
||||
PostId: req.PostId,
|
||||
Mac: req.Mac,
|
||||
Mac: mac,
|
||||
Content: req.Content,
|
||||
}).InsertAndGetId()
|
||||
if err != nil {
|
||||
|
||||
@@ -12,11 +12,19 @@ import (
|
||||
"stackChan/internal/model"
|
||||
|
||||
"stackChan/api/post/v1"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) DeletePostComment(ctx context.Context, req *v1.DeletePostCommentReq) (res *v1.DeletePostCommentRes, err error) {
|
||||
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
|
||||
if mac == "" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
|
||||
}
|
||||
var postComment model.PostComment
|
||||
err = dao.DevicePostComment.Ctx(ctx).Where("id=?", req.Id).Scan(&postComment)
|
||||
err = dao.DevicePostComment.Ctx(ctx).Where("id=? AND post_id=? AND mac=?", req.CommentId, req.PostId, mac).Scan(&postComment)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -26,13 +34,13 @@ func (c *ControllerV1) DeletePostComment(ctx context.Context, req *v1.DeletePost
|
||||
return nil, errors.New("post not found")
|
||||
}
|
||||
|
||||
if postComment.Mac != req.Mac {
|
||||
if postComment.Mac != mac {
|
||||
return nil, errors.New("no authority to delete")
|
||||
}
|
||||
|
||||
_, err = dao.DevicePostComment.
|
||||
Ctx(ctx).
|
||||
Where("id = ? AND mac = ?", req.Id, req.Mac).
|
||||
Where("id=? AND post_id=?", req.CommentId, req.PostId).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -26,7 +26,7 @@ func (c *ControllerV1) GetPostComment(ctx context.Context, req *v1.GetPostCommen
|
||||
|
||||
var list []*model.PostComment
|
||||
|
||||
db := dao.DevicePostComment.Ctx(ctx).As("dp").Where("mac = ? AND post_id = ?", req.Mac, req.PostId)
|
||||
db := dao.DevicePostComment.Ctx(ctx).As("dp").Where("post_id = ?", req.PostId)
|
||||
|
||||
total, err := db.Count()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user