Skip to content

Commit

Permalink
fix: solve bugs for post function
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Oct 17, 2023
1 parent 8d5f512 commit fae5bb9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions internal/dazBlog/controller/v1/post/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/dazBlog/controller/v1/post/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}
2 changes: 1 addition & 1 deletion internal/dazBlog/controller/v1/post/delete_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}})
}
2 changes: 1 addition & 1 deletion internal/dazBlog/controller/v1/post/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
96 changes: 48 additions & 48 deletions internal/dazBlog/controller/v1/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/dazBlog/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fae5bb9

Please sign in to comment.