forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
@@ -1,13 +1,26 @@
|
||||
package chats
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func createChat(c fiber.Ctx) error {
|
||||
type Handler struct {
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func NewHandler(st *store.Store) *Handler {
|
||||
return &Handler{store: st}
|
||||
}
|
||||
|
||||
type CreateChatRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids" validate:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) CreateChat(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
var req CreateChatRequest
|
||||
@@ -18,24 +31,11 @@ func createChat(c fiber.Ctx) error {
|
||||
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
|
||||
})
|
||||
chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func createChat(c fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusCreated).JSON(chat)
|
||||
}
|
||||
|
||||
func listChats(c fiber.Ctx) error {
|
||||
func (h *Handler) ListChats(c fiber.Ctx) error {
|
||||
userID := c.Locals("userID").(uuid.UUID)
|
||||
|
||||
limit, err := common.ParseLimit(c, 50)
|
||||
@@ -55,19 +55,12 @@ func listChats(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if cursor != nil && userID != cursor.ContextID {
|
||||
if cursor != nil && cursor.ContextID != userID {
|
||||
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
|
||||
})
|
||||
page, err := h.store.ListChats(c.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
@@ -75,29 +68,16 @@ func listChats(c fiber.Ctx) error {
|
||||
return c.JSON(page)
|
||||
}
|
||||
|
||||
func getChat(c fiber.Ctx) error {
|
||||
func (h *Handler) 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)
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "invalid chat_id format")
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
chat, err := h.store.GetChatForUser(c.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user