From 29bfd419aa7635da48f29cf2ef6f42886a2fc1c4 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Sun, 22 Sep 2024 10:52:56 +0300 Subject: [PATCH] refactor server/utils --- server/handlers/auth/login.go | 4 ++-- server/handlers/auth/register.go | 4 ++-- server/handlers/comments/create_comment.go | 4 ++-- server/handlers/comments/update_comment.go | 9 +++++---- server/handlers/me/update.go | 4 ++-- server/handlers/me/update_notifications.go | 4 ++-- server/handlers/posts/create_post.go | 4 ++-- server/handlers/posts/update_post.go | 9 +++++---- server/handlers/users/follow_user.go | 5 +++-- server/utils/slices.go | 13 ------------- server/{utils => validate}/validator.go | 12 ++++++++---- 11 files changed, 33 insertions(+), 39 deletions(-) delete mode 100644 server/utils/slices.go rename server/{utils => validate}/validator.go (53%) diff --git a/server/handlers/auth/login.go b/server/handlers/auth/login.go index 171ce11..0088936 100644 --- a/server/handlers/auth/login.go +++ b/server/handlers/auth/login.go @@ -7,7 +7,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/golang-jwt/jwt" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson/primitive" "golang.org/x/crypto/bcrypt" ) @@ -25,7 +25,7 @@ func Login(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/auth/register.go b/server/handlers/auth/register.go index 6e83b65..63224b7 100644 --- a/server/handlers/auth/register.go +++ b/server/handlers/auth/register.go @@ -3,7 +3,7 @@ package auth import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "golang.org/x/crypto/bcrypt" ) @@ -22,7 +22,7 @@ func Register(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/comments/create_comment.go b/server/handlers/comments/create_comment.go index 07d2fd2..586c570 100644 --- a/server/handlers/comments/create_comment.go +++ b/server/handlers/comments/create_comment.go @@ -3,7 +3,7 @@ package comments import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -26,7 +26,7 @@ func CreateComment(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/comments/update_comment.go b/server/handlers/comments/update_comment.go index 04d4b6b..400e5da 100644 --- a/server/handlers/comments/update_comment.go +++ b/server/handlers/comments/update_comment.go @@ -1,11 +1,12 @@ package comments import ( + "slices" "time" "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -30,7 +31,7 @@ func UpdateComment(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) @@ -76,7 +77,7 @@ func UpdateComment(c *fiber.Ctx) error { } if body.Like != nil { - if *body.Like && !utils.Contains(comment.Likes, user.ID) { + if *body.Like && !slices.Contains(comment.Likes, user.ID) { update["$addToSet"] = bson.M{"likes": user.ID} if comment.Author != user.ID { notification := models.Notification{ @@ -89,7 +90,7 @@ func UpdateComment(c *fiber.Ctx) error { models.UpdateUser(comment.Author, bson.M{"$inc": bson.M{"points": 1}}) } - } else if !*body.Like && utils.Contains(comment.Likes, user.ID) { + } else if !*body.Like && slices.Contains(comment.Likes, user.ID) { update["$pull"] = bson.M{"likes": user.ID} if comment.Author != user.ID { models.UpdateUser(comment.Author, bson.M{"$inc": bson.M{"points": -1}}) diff --git a/server/handlers/me/update.go b/server/handlers/me/update.go index fde2139..6d9c812 100644 --- a/server/handlers/me/update.go +++ b/server/handlers/me/update.go @@ -3,7 +3,7 @@ package me import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson" "golang.org/x/crypto/bcrypt" ) @@ -31,7 +31,7 @@ func UpdateUser(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/me/update_notifications.go b/server/handlers/me/update_notifications.go index 25976d5..3d2f05b 100644 --- a/server/handlers/me/update_notifications.go +++ b/server/handlers/me/update_notifications.go @@ -3,7 +3,7 @@ package me import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -26,7 +26,7 @@ func UpdateNotification(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/posts/create_post.go b/server/handlers/posts/create_post.go index faa07de..a2d76ed 100644 --- a/server/handlers/posts/create_post.go +++ b/server/handlers/posts/create_post.go @@ -3,7 +3,7 @@ package posts import ( "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" ) type NewPostBody struct { @@ -25,7 +25,7 @@ func CreatePost(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) diff --git a/server/handlers/posts/update_post.go b/server/handlers/posts/update_post.go index 2d7475a..d664b7e 100644 --- a/server/handlers/posts/update_post.go +++ b/server/handlers/posts/update_post.go @@ -1,11 +1,12 @@ package posts import ( + "slices" "time" "github.com/gofiber/fiber/v2" "github.com/lareii/copl.uk/server/models" - "github.com/lareii/copl.uk/server/utils" + "github.com/lareii/copl.uk/server/validate" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -30,7 +31,7 @@ func UpdatePost(c *fiber.Ctx) error { }) } - if err := utils.Validate.Struct(&body); err != nil { + if err := validate.Struct(&body); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "message": "Missing or invalid fields.", }) @@ -69,7 +70,7 @@ func UpdatePost(c *fiber.Ctx) error { } if body.Like != nil { - if *body.Like && !utils.Contains(post.Likes, user.ID) { + if *body.Like && !slices.Contains(post.Likes, user.ID) { update["$addToSet"] = bson.M{"likes": user.ID} if post.Author != user.ID { notification := models.Notification{ @@ -82,7 +83,7 @@ func UpdatePost(c *fiber.Ctx) error { models.UpdateUser(post.Author, bson.M{"$inc": bson.M{"points": 1}}) } - } else if !*body.Like && utils.Contains(post.Likes, user.ID) { + } else if !*body.Like && slices.Contains(post.Likes, user.ID) { update["$pull"] = bson.M{"likes": user.ID} if post.Author != user.ID { models.UpdateUser(post.Author, bson.M{"$inc": bson.M{"points": -1}}) diff --git a/server/handlers/users/follow_user.go b/server/handlers/users/follow_user.go index 6b4996b..c183080 100644 --- a/server/handlers/users/follow_user.go +++ b/server/handlers/users/follow_user.go @@ -1,9 +1,10 @@ package users import ( + "slices" + "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" ) @@ -35,7 +36,7 @@ func FollowUser(c *fiber.Ctx) error { var updateTarget, updateUser bson.M - isFollowing := utils.Contains(user.Following, targetUser.ID) + isFollowing := slices.Contains(user.Following, targetUser.ID) if isFollowing { updateTarget = bson.M{"$pull": bson.M{"followers": user.ID}} updateUser = bson.M{"$pull": bson.M{"following": targetUser.ID}} diff --git a/server/utils/slices.go b/server/utils/slices.go deleted file mode 100644 index 07d60b5..0000000 --- a/server/utils/slices.go +++ /dev/null @@ -1,13 +0,0 @@ -package utils - -import "go.mongodb.org/mongo-driver/bson/primitive" - -func Contains(slice []primitive.ObjectID, id primitive.ObjectID) bool { - for _, item := range slice { - if item == id { - return true - } - } - - return false -} diff --git a/server/utils/validator.go b/server/validate/validator.go similarity index 53% rename from server/utils/validator.go rename to server/validate/validator.go index 2686ca5..03c0d64 100644 --- a/server/utils/validator.go +++ b/server/validate/validator.go @@ -1,4 +1,4 @@ -package utils +package validate import ( "regexp" @@ -6,7 +6,11 @@ import ( "github.com/go-playground/validator/v10" ) -var Validate *validator.Validate +var validate *validator.Validate + +func Struct(s interface{}) error { + return validate.Struct(s) +} func usernameValidator(fl validator.FieldLevel) bool { regex := regexp.MustCompile("^[a-zA-Z0-9._]+$") @@ -14,6 +18,6 @@ func usernameValidator(fl validator.FieldLevel) bool { } func init() { - Validate = validator.New() - Validate.RegisterValidation("username_valid", usernameValidator) + validate = validator.New() + validate.RegisterValidation("username_valid", usernameValidator) }