added create group route
This commit is contained in:
53
database/groups.go
Normal file
53
database/groups.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"relay-server/helpers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateGroup(db *sql.DB, groupName string, userID uuid.UUID) (uuid.UUID, error) {
|
||||||
|
createConversationQuery := `
|
||||||
|
INSERT INTO Conversations (conversation_type, name)
|
||||||
|
VALUES ('group', $1)
|
||||||
|
RETURNING conversation_id AS group_id;
|
||||||
|
`
|
||||||
|
|
||||||
|
insertGroupAdminQuery := `
|
||||||
|
INSERT INTO GroupAdmins (conversation_id, user_id, granted_by, is_owner)
|
||||||
|
VALUES ($1, $2, $3, true)
|
||||||
|
RETURNING granted_at;
|
||||||
|
`
|
||||||
|
|
||||||
|
addMemberToGroupQuery := `
|
||||||
|
INSERT INTO Memberships (conversation_id, user_id)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
RETURNING user_id;
|
||||||
|
`
|
||||||
|
|
||||||
|
var groupID uuid.UUID
|
||||||
|
err := db.QueryRow(createConversationQuery, groupName).Scan(&groupID)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.Nil, helpers.NewError(helpers.ErrInternal, "Failed to create group", fmt.Errorf("failed to create group: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
var grantedAt string
|
||||||
|
err = db.QueryRow(insertGroupAdminQuery, groupID, userID, userID).Scan(&grantedAt)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.Nil, helpers.NewError(helpers.ErrInternal, "Failed to create group", fmt.Errorf("failed to insert group admin: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
var memberID uuid.UUID
|
||||||
|
err = db.QueryRow(addMemberToGroupQuery, groupID, userID).Scan(&memberID)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.Nil, helpers.NewError(helpers.ErrInternal, "Failed to create group", fmt.Errorf("failed to add member to group: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = InsertContactByID(db, userID, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return groupID, helpers.NewError(helpers.ErrInternal, "Failed to create group contact", fmt.Errorf("failed to insert group contact: %w", err))
|
||||||
|
}
|
||||||
|
return groupID, nil
|
||||||
|
}
|
||||||
33
handlers/groups.go
Normal file
33
handlers/groups.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"relay-server/database"
|
||||||
|
"relay-server/helpers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateGroup(c *fiber.Ctx) error {
|
||||||
|
type createGroupRequest struct {
|
||||||
|
GroupName string `json:"groupName"`
|
||||||
|
}
|
||||||
|
|
||||||
|
userID := c.Locals("userID").(uuid.UUID)
|
||||||
|
|
||||||
|
var req createGroupRequest
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return helpers.NewError(helpers.ErrInvalidInput, "Invalid request body", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.GroupName == "" {
|
||||||
|
return helpers.NewError(helpers.ErrInvalidInput, "Group name is empty", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
groupID, err := database.CreateGroup(database.DB, req.GroupName, userID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//TODO zrobic io.to(groupID).emit("added to group", {groupID, username: req.GroupName})
|
||||||
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{"groupID": groupID})
|
||||||
|
|
||||||
|
}
|
||||||
@@ -36,3 +36,7 @@ type Message struct {
|
|||||||
Sender string `json:"sender"`
|
Sender string `json:"sender"`
|
||||||
AttachmentUrl *string `json:"attachment_url"`
|
AttachmentUrl *string `json:"attachment_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateGroupResponse struct {
|
||||||
|
GroupID uuid.UUID `json:"group_id"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,4 +30,8 @@ func SetupRoutes(app *fiber.App) {
|
|||||||
// Messages group
|
// Messages group
|
||||||
messages := chat.Group("/messages", middleware.Protected(), logger.New())
|
messages := chat.Group("/messages", middleware.Protected(), logger.New())
|
||||||
messages.Get("/:conversationID", handlers.GetMessages)
|
messages.Get("/:conversationID", handlers.GetMessages)
|
||||||
|
|
||||||
|
// Groups group
|
||||||
|
groups := chat.Group("/groups", middleware.Protected(), logger.New())
|
||||||
|
groups.Post("/create", handlers.CreateGroup)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user