Skip to content

Commit

Permalink
Merge pull request #16 from mertcandav/master
Browse files Browse the repository at this point in the history
server: refactor server/utils
  • Loading branch information
lareii authored Sep 22, 2024
2 parents 2353538 + 29bfd41 commit 347da13
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions server/handlers/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.",
})
Expand Down
4 changes: 2 additions & 2 deletions server/handlers/auth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.",
})
Expand Down
4 changes: 2 additions & 2 deletions server/handlers/comments/create_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.",
})
Expand Down
9 changes: 5 additions & 4 deletions server/handlers/comments/update_comment.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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.",
})
Expand Down Expand Up @@ -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{
Expand All @@ -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}})
Expand Down
4 changes: 2 additions & 2 deletions server/handlers/me/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.",
})
Expand Down
4 changes: 2 additions & 2 deletions server/handlers/me/update_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.",
})
Expand Down
4 changes: 2 additions & 2 deletions server/handlers/posts/create_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.",
})
Expand Down
9 changes: 5 additions & 4 deletions server/handlers/posts/update_post.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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.",
})
Expand Down Expand Up @@ -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{
Expand All @@ -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}})
Expand Down
5 changes: 3 additions & 2 deletions server/handlers/users/follow_user.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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}}
Expand Down
13 changes: 0 additions & 13 deletions server/utils/slices.go

This file was deleted.

12 changes: 8 additions & 4 deletions server/utils/validator.go → server/validate/validator.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package utils
package validate

import (
"regexp"

"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._]+$")
return regex.MatchString(fl.Field().String())
}

func init() {
Validate = validator.New()
Validate.RegisterValidation("username_valid", usernameValidator)
validate = validator.New()
validate.RegisterValidation("username_valid", usernameValidator)
}

0 comments on commit 347da13

Please sign in to comment.