forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@@ -24,28 +23,27 @@ type CreateMessageRequest struct {
|
||||
func (h *Handler) CreateMessage(c fiber.Ctx) error {
|
||||
userID := common.MustUserID(c)
|
||||
|
||||
chatID, err := uuid.Parse(c.Params("chat_id"))
|
||||
chatID, err := common.PathUUID(c, "chat_id")
|
||||
if err != nil {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "invalid chat_id format")
|
||||
return err
|
||||
}
|
||||
|
||||
idempotencyKey := c.Get("Idempotency-Key")
|
||||
if msg := common.ValidateIdempotencyKey(idempotencyKey); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidateIdempotencyKey(idempotencyKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var req CreateMessageRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
if msg := common.ValidateMessageText(req.Text); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidateMessageText(req.Text); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message, err := h.store.CreateMessage(c.Context(), chatID, userID, req.Text, idempotencyKey)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(message)
|
||||
@@ -54,25 +52,24 @@ func (h *Handler) CreateMessage(c fiber.Ctx) error {
|
||||
func (h *Handler) ListMessages(c fiber.Ctx) error {
|
||||
userID := common.MustUserID(c)
|
||||
|
||||
chatID, err := uuid.Parse(c.Params("chat_id"))
|
||||
chatID, err := common.PathUUID(c, "chat_id")
|
||||
if err != nil {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "invalid chat_id format")
|
||||
return err
|
||||
}
|
||||
|
||||
limit, err := common.ParseLimit(c, 50)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
cursor, err := common.ParseCursor(c)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
page, err := h.store.ListMessages(c.Context(), userID, chatID, limit, cursor)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(page)
|
||||
|
||||
Reference in New Issue
Block a user