added add member to group route
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"log"
|
||||
"relay-server/database"
|
||||
"relay-server/helpers"
|
||||
)
|
||||
@@ -31,3 +32,37 @@ func CreateGroup(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{"groupID": groupID})
|
||||
|
||||
}
|
||||
|
||||
func AddMemberToGroup(c *fiber.Ctx) error {
|
||||
type addMemberToGroupRequest struct {
|
||||
GroupID uuid.UUID `json:"group_id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
}
|
||||
var req addMemberToGroupRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid request body", err)
|
||||
}
|
||||
if req.GroupID == uuid.Nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Group ID is empty", nil)
|
||||
}
|
||||
if req.UserID == uuid.Nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "User ID is empty", nil)
|
||||
}
|
||||
|
||||
isAdmin, err := database.IsAdmin(database.DB, req.UserID, req.GroupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !isAdmin {
|
||||
return helpers.NewError(helpers.ErrUnauthorized, "You are not a group administrator", nil)
|
||||
}
|
||||
|
||||
_, err = database.AddMemberToGroup(database.DB, req.GroupID, req.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//TODO zrobic io.to(req.GroupID).emit("added to group", {groupID: req.GroupID, userID: req.UserID}), i wyslij do tego latest message bo na razie mi sie nie chce
|
||||
|
||||
log.Println("Successfully added member to group")
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Successfully added member to group"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user