From f654ad53b14650da67d26328ec0d93d92d193ddb Mon Sep 17 00:00:00 2001 From: lareii Date: Thu, 12 Sep 2024 01:55:05 +0300 Subject: [PATCH] feat(server): improve post model and post handlers --- server/handlers/posts/create_post.go | 7 ++-- server/handlers/posts/get_post.go | 56 ++++++++++++++++++++-------- server/handlers/posts/get_posts.go | 42 ++++++++++----------- server/handlers/posts/update_post.go | 11 +++++- server/models/post.go | 2 +- 5 files changed, 74 insertions(+), 44 deletions(-) diff --git a/server/handlers/posts/create_post.go b/server/handlers/posts/create_post.go index 82b2914..8bd865d 100644 --- a/server/handlers/posts/create_post.go +++ b/server/handlers/posts/create_post.go @@ -1,13 +1,13 @@ package posts import ( - "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" + "github.com/lareii/copl.uk/server/utils" ) type NewPostBody struct { - Content string `json:"content" validate:"required"` + Content string `json:"content" validate:"required,min=1,max=1000"` } func CreatePost(c *fiber.Ctx) error { @@ -25,8 +25,7 @@ func CreatePost(c *fiber.Ctx) error { }) } - var validate = validator.New() - if err := validate.Struct(&body); err != nil { + if err := utils.Validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/posts/get_post.go b/server/handlers/posts/get_post.go index d24bc31..0af3c8a 100644 --- a/server/handlers/posts/get_post.go +++ b/server/handlers/posts/get_post.go @@ -6,6 +6,30 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +type PostResponse struct { + Message string `json:"message"` + Post PostDetails `json:"post"` +} + +type PostDetails struct { + ID primitive.ObjectID `json:"id"` + CreatedAt primitive.Timestamp `json:"created_at"` + UpdatedAt primitive.Timestamp `json:"updated_at"` + Author AuthorDetails `json:"author"` + Content string `json:"content"` + Likes []primitive.ObjectID `json:"likes"` + Comments uint `json:"comments"` +} + +type AuthorDetails struct { + ID primitive.ObjectID `json:"id"` + CreatedAt primitive.Timestamp `json:"created_at"` + DisplayName string `json:"display_name"` + Username string `json:"username"` + About string `json:"about,omitempty"` + Points int `json:"points"` +} + func GetPost(c *fiber.Ctx) error { id := c.Params("id") postID, err := primitive.ObjectIDFromHex(id) @@ -29,23 +53,23 @@ func GetPost(c *fiber.Ctx) error { }) } - return c.Status(fiber.StatusOK).JSON(fiber.Map{ - "message": "Post found.", - "post": fiber.Map{ - "id": post.ID, - "created_at": post.CreatedAt, - "updated_at": post.UpdatedAt, - "author": fiber.Map{ - "id": author.ID, - "created_at": author.CreatedAt, - "display_name": author.DisplayName, - "username": author.Username, - "about": author.About, - "points": author.Points, + return c.Status(fiber.StatusOK).JSON(PostResponse{ + Message: "Post found.", + Post: PostDetails{ + ID: post.ID, + CreatedAt: post.CreatedAt, + UpdatedAt: post.UpdatedAt, + Author: AuthorDetails{ + ID: author.ID, + CreatedAt: author.CreatedAt, + DisplayName: author.DisplayName, + Username: author.Username, + About: author.About, + Points: author.Points, }, - "content": post.Content, - "likes": post.Likes, - "comments": post.Comments, + Content: post.Content, + Likes: post.Likes, + Comments: post.Comments, }, }) } diff --git a/server/handlers/posts/get_posts.go b/server/handlers/posts/get_posts.go index 3ec5716..bfc20ce 100644 --- a/server/handlers/posts/get_posts.go +++ b/server/handlers/posts/get_posts.go @@ -6,6 +6,11 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +type PostsResponse struct { + Message string `json:"message"` + Posts []PostDetails `json:"posts"` +} + func GetPosts(c *fiber.Ctx) error { limit := c.QueryInt("limit", 10) offset := c.QueryInt("offset", 0) @@ -22,7 +27,7 @@ func GetPosts(c *fiber.Ctx) error { authorIDs = append(authorIDs, post.Author) } - var authors []models.User + authorMap := make(map[primitive.ObjectID]models.User) for _, authorID := range authorIDs { author, err := models.GetUserByID(authorID) if err != nil { @@ -30,33 +35,28 @@ func GetPosts(c *fiber.Ctx) error { "message": "Error fetching post authors.", }) } - authors = append(authors, author) - } - - authorMap := make(map[primitive.ObjectID]models.User) - for _, author := range authors { authorMap[author.ID] = author } - var responsePosts []fiber.Map + var responsePosts []PostDetails for _, post := range posts { author := authorMap[post.Author] - responsePosts = append(responsePosts, fiber.Map{ - "id": post.ID, - "created_at": post.CreatedAt, - "updated_at": post.UpdatedAt, - "author": fiber.Map{ - "id": author.ID, - "created_at": author.CreatedAt, - "display_name": author.DisplayName, - "username": author.Username, - "about": author.About, - "points": author.Points, + responsePosts = append(responsePosts, PostDetails{ + ID: post.ID, + CreatedAt: post.CreatedAt, + UpdatedAt: post.UpdatedAt, + Author: AuthorDetails{ + ID: author.ID, + CreatedAt: author.CreatedAt, + DisplayName: author.DisplayName, + Username: author.Username, + About: author.About, + Points: author.Points, }, - "content": post.Content, - "likes": post.Likes, - "comments": post.Comments, + Content: post.Content, + Likes: post.Likes, + Comments: post.Comments, }) } diff --git a/server/handlers/posts/update_post.go b/server/handlers/posts/update_post.go index 106ca8a..4e8308e 100644 --- a/server/handlers/posts/update_post.go +++ b/server/handlers/posts/update_post.go @@ -5,13 +5,14 @@ import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" + "github.com/lareii/copl.uk/server/utils" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) type UpdatePostBody struct { - Content string `json:"content"` - Like *bool `json:"like"` + Content string `json:"content" validate:"omitempty,max=1000"` + Like *bool `json:"like" validate:"omitempty"` } func UpdatePost(c *fiber.Ctx) error { @@ -29,6 +30,12 @@ func UpdatePost(c *fiber.Ctx) error { }) } + if err := utils.Validate.Struct(&body); err != nil { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Missing or invalid fields.", + }) + } + id := c.Params("id") postID, err := primitive.ObjectIDFromHex(id) if err != nil { diff --git a/server/models/post.go b/server/models/post.go index 1c8246c..1dfe55a 100644 --- a/server/models/post.go +++ b/server/models/post.go @@ -17,7 +17,7 @@ type Post struct { UpdatedAt primitive.Timestamp `bson:"updated_at" json:"updated_at"` Author primitive.ObjectID `bson:"author" json:"author"` Content string `bson:"content" json:"content"` - Likes []primitive.ObjectID `bson:"likes" json:"likes,omitempty"` + Likes []primitive.ObjectID `bson:"likes" json:"likes"` Comments uint `bson:"comments" json:"comments"` }