forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -26,19 +26,19 @@ func (h *Handler) CreateChat(c fiber.Ctx) error {
|
||||
|
||||
var req CreateChatRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if msg := common.ValidateChatTitle(req.Title); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidateChatTitle(req.Title); err != nil {
|
||||
return err
|
||||
}
|
||||
if msg := common.ValidateMemberIDs(req.MemberIDs, userID); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
if err := common.ValidateMemberIDs(req.MemberIDs, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(chat)
|
||||
@@ -49,21 +49,20 @@ func (h *Handler) ListChats(c fiber.Ctx) error {
|
||||
|
||||
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
|
||||
}
|
||||
if cursor != nil && cursor.ContextID != userID {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "bad cursor value")
|
||||
return common.BadRequest("bad cursor value")
|
||||
}
|
||||
|
||||
page, err := h.store.ListChats(c.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(page)
|
||||
@@ -72,15 +71,14 @@ func (h *Handler) ListChats(c fiber.Ctx) error {
|
||||
func (h *Handler) GetChat(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
|
||||
}
|
||||
|
||||
chat, err := h.store.GetChatForUser(c.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(chat)
|
||||
|
||||
Reference in New Issue
Block a user