Skip to content

Commit

Permalink
feat(server): improve post model and post handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 11, 2024
1 parent f2df935 commit f654ad5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 44 deletions.
7 changes: 3 additions & 4 deletions server/handlers/posts/create_post.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.",
})
Expand Down
56 changes: 40 additions & 16 deletions server/handlers/posts/get_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
},
})
}
42 changes: 21 additions & 21 deletions server/handlers/posts/get_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,41 +27,36 @@ 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 {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"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,
})
}

Expand Down
11 changes: 9 additions & 2 deletions server/handlers/posts/update_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/models/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit f654ad5

Please sign in to comment.