diff --git a/internal/dazBlog/controller/v1/post/create.go b/internal/dazBlog/controller/v1/post/create.go index a2031ac..685f08d 100644 --- a/internal/dazBlog/controller/v1/post/create.go +++ b/internal/dazBlog/controller/v1/post/create.go @@ -32,6 +32,7 @@ func (ctrl *PostController) Create(c *gin.Context) { } resp, err := ctrl.b.Posts().Create(c, c.GetString(known.XUsernameKey), &r) + log.C(c).Infow("user create post", "username", c.GetString(known.XUsernameKey), "title", r.Title) if err != nil { core.WriteResponse(c, err, nil) return diff --git a/internal/dazBlog/controller/v1/post/delete.go b/internal/dazBlog/controller/v1/post/delete.go index e10e07c..f52fdfc 100644 --- a/internal/dazBlog/controller/v1/post/delete.go +++ b/internal/dazBlog/controller/v1/post/delete.go @@ -23,5 +23,5 @@ func (ctrl *PostController) Delete(c *gin.Context) { return } - core.WriteResponse(c, nil, nil) + core.WriteResponse(c, nil, map[string]string{"delete post_id": postID, "status": "ok"}) } diff --git a/internal/dazBlog/controller/v1/post/delete_collection.go b/internal/dazBlog/controller/v1/post/delete_collection.go index c4f4ce8..ea86f63 100644 --- a/internal/dazBlog/controller/v1/post/delete_collection.go +++ b/internal/dazBlog/controller/v1/post/delete_collection.go @@ -23,5 +23,5 @@ func (ctrl *PostController) DeleteCollection(c *gin.Context) { return } - core.WriteResponse(c, nil, nil) + core.WriteResponse(c, nil, map[string][]string{"delete post_ids": postIDs, "status": {"ok"}}) } diff --git a/internal/dazBlog/controller/v1/post/update.go b/internal/dazBlog/controller/v1/post/update.go index 8001b79..bef6aad 100644 --- a/internal/dazBlog/controller/v1/post/update.go +++ b/internal/dazBlog/controller/v1/post/update.go @@ -39,5 +39,5 @@ func (ctrl *PostController) Update(c *gin.Context) { return } - core.WriteResponse(c, nil, nil) + core.WriteResponse(c, nil, map[string]string{"update post_id": postID}) } diff --git a/internal/dazBlog/controller/v1/user/list.go b/internal/dazBlog/controller/v1/user/list.go index 9433b61..05badf6 100644 --- a/internal/dazBlog/controller/v1/user/list.go +++ b/internal/dazBlog/controller/v1/user/list.go @@ -6,71 +6,71 @@ package user import ( - "context" - "time" + "context" + "time" - "github.com/gin-gonic/gin" - "google.golang.org/protobuf/types/known/timestamppb" + "github.com/gin-gonic/gin" + "google.golang.org/protobuf/types/known/timestamppb" - "github.com/Daz-3ux/dBlog/internal/pkg/core" - "github.com/Daz-3ux/dBlog/internal/pkg/log" - v1 "github.com/Daz-3ux/dBlog/pkg/api/dazBlog/v1" - pb "github.com/Daz-3ux/dBlog/pkg/proto/dazBlog/v1" + "github.com/Daz-3ux/dBlog/internal/pkg/core" + "github.com/Daz-3ux/dBlog/internal/pkg/log" + v1 "github.com/Daz-3ux/dBlog/pkg/api/dazBlog/v1" + pb "github.com/Daz-3ux/dBlog/pkg/proto/dazBlog/v1" ) // List return users list, only root user can call this function func (ctrl *UserController) List(c *gin.Context) { - log.C(c).Infow("List user function called") + log.C(c).Infow("List user function called") - var r v1.ListUserRequest - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, err, nil) - log.C(c).Errorw("List user bind called", "error", err) + var r v1.ListUserRequest + if err := c.ShouldBindJSON(&r); err != nil { + core.WriteResponse(c, err, nil) + log.C(c).Errorw("List user bind called", "error", err) - return - } + return + } - resp, err := ctrl.b.Users().List(c, r.Offset, r.Limit) - if err != nil { - core.WriteResponse(c, err, nil) - log.C(c).Errorw("List user write called", "error", err) + resp, err := ctrl.b.Users().List(c, r.Offset, r.Limit) + if err != nil { + core.WriteResponse(c, err, nil) + log.C(c).Errorw("List user write called", "error", err) - return - } + return + } - core.WriteResponse(c, nil, resp) + core.WriteResponse(c, nil, resp) } // ListUsers return users list for gRPC func (ctrl *UserController) ListUsers(ctx context.Context, r *pb.ListUsersRequest) (*pb.ListUsersResponse, error) { - log.C(ctx).Infow("ListUsers function called") + log.C(ctx).Infow("ListUsers function called") - resp, err := ctrl.b.Users().List(ctx, int(r.Offset), int(r.Limit)) - if err != nil { - return nil, err - } + resp, err := ctrl.b.Users().List(ctx, int(r.Offset), int(r.Limit)) + if err != nil { + return nil, err + } - users := make([]*pb.UserInfo, 0, len(resp.Users)) - for _, u := range resp.Users { - createdAt, _ := time.Parse("2006-01-02 15:04:05", u.CreatedAt) - updatedAt, _ := time.Parse("2006-01-02 15:04:05", u.UpdatedAt) - users = append(users, &pb.UserInfo{ - Username: u.Username, - Nickname: u.Nickname, - Email: u.Email, - Gender: u.Gender, - Phone: u.Phone, - Qq: u.QQ, - Postcount: u.PostCount, - CreatedAt: timestamppb.New(createdAt), - UpdatedAt: timestamppb.New(updatedAt), - }) - } + users := make([]*pb.UserInfo, 0, len(resp.Users)) + for _, u := range resp.Users { + createdAt, _ := time.Parse("2006-01-02 15:04:05", u.CreatedAt) + updatedAt, _ := time.Parse("2006-01-02 15:04:05", u.UpdatedAt) + users = append(users, &pb.UserInfo{ + Username: u.Username, + Nickname: u.Nickname, + Email: u.Email, + Gender: u.Gender, + Phone: u.Phone, + Qq: u.QQ, + Postcount: u.PostCount, + CreatedAt: timestamppb.New(createdAt), + UpdatedAt: timestamppb.New(updatedAt), + }) + } - ret := &pb.ListUsersResponse{ - TotalCount: resp.TotalCount, - Users: users, - } + ret := &pb.ListUsersResponse{ + TotalCount: resp.TotalCount, + Users: users, + } - return ret, nil + return ret, nil } diff --git a/internal/dazBlog/router.go b/internal/dazBlog/router.go index 66364e3..d9087db 100644 --- a/internal/dazBlog/router.go +++ b/internal/dazBlog/router.go @@ -67,7 +67,7 @@ func installRouters(g *gin.Engine) error { userv1.DELETE(":name", uc.Delete) } - postv1 := v1.Group("/posts") + postv1 := v1.Group("/posts", mw.Authn()) { // create post postv1.POST("", pc.Create)