forked from mtusi2026/mtusi2026lab2
init(app) moved app code from Lab1
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package chats
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func createChat(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
var req CreateChatRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if msg := common.ValidateChatTitle(req.Title); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
|
||||
if msg := common.ValidateMemberIDs(req.MemberIDs, userID); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
|
||||
var chat Chat
|
||||
|
||||
err := storage.GlobalStore.Update(func(tx storage.Tx) error {
|
||||
for _, memberID := range req.MemberIDs {
|
||||
if _, ok := tx.GetUser(memberID); !ok {
|
||||
return ErrMemberNotFound
|
||||
}
|
||||
}
|
||||
|
||||
chat = NewChat(req)
|
||||
tx.CreateChat(chat)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(chat)
|
||||
}
|
||||
|
||||
func listChats(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
limit, err := common.ParseLimit(c, 50)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
cursor, err := common.ParseCursor(c)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if cursor != nil && userID != cursor.ContextID {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "bad cursor value")
|
||||
}
|
||||
|
||||
var page *common.PaginatedResponse[storage.Entity]
|
||||
|
||||
err = storage.GlobalStore.View(func(tx storage.ReadOnlyTx) error {
|
||||
found, fetchErr := tx.GetUserChats(userID, limit, cursor)
|
||||
page = found
|
||||
return fetchErr
|
||||
})
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(page)
|
||||
}
|
||||
|
||||
func getChat(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
chatID, err := uuid.Parse(c.Params("chat_id"))
|
||||
if err != nil {
|
||||
return common.SendAppError(c, ErrInvalidChatID)
|
||||
}
|
||||
|
||||
var chat storage.ChatEntity
|
||||
|
||||
err = storage.GlobalStore.View(func(tx storage.ReadOnlyTx) error {
|
||||
if !tx.IsMember(userID, chatID) {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
found, ok := tx.GetChat(chatID)
|
||||
if !ok {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
chat = found
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(chat)
|
||||
}
|
||||
Reference in New Issue
Block a user