forked from mtusi2026/mtusi2026lab3
chore(app) transferred codebase from Lab2
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package chats
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func NewHandler(st *store.Store) *Handler {
|
||||
return &Handler{store: st}
|
||||
}
|
||||
|
||||
type CreateChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
}
|
||||
|
||||
func (h *Handler) CreateChat(c fiber.Ctx) error {
|
||||
userID := common.MustUserID(c)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(chat)
|
||||
}
|
||||
|
||||
func (h *Handler) ListChats(c fiber.Ctx) error {
|
||||
userID := common.MustUserID(c)
|
||||
|
||||
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 && cursor.ContextID != userID {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "bad cursor value")
|
||||
}
|
||||
|
||||
page, err := h.store.ListChats(c.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(page)
|
||||
}
|
||||
|
||||
func (h *Handler) GetChat(c fiber.Ctx) error {
|
||||
userID := common.MustUserID(c)
|
||||
|
||||
chatID, err := uuid.Parse(c.Params("chat_id"))
|
||||
if err != nil {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "invalid chat_id format")
|
||||
}
|
||||
|
||||
chat, err := h.store.GetChatForUser(c.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(chat)
|
||||
}
|
||||
Reference in New Issue
Block a user