forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
@@ -1,40 +1,42 @@
|
||||
package messages
|
||||
|
||||
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 createMessage(c fiber.Ctx) error {
|
||||
type Handler struct {
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func NewHandler(st *store.Store) *Handler {
|
||||
return &Handler{store: st}
|
||||
}
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text" validate:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) CreateMessage(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 req CreateMessageRequest
|
||||
if err := common.BindJSON(c, &req); err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if msg := common.ValidateMessageText(req.Text); msg != "" {
|
||||
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
|
||||
}
|
||||
|
||||
var message Message
|
||||
|
||||
err = storage.GlobalStore.Update(func(tx storage.Tx) error {
|
||||
if !tx.IsMember(userID, chatID) {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
message = NewMessage(chatID, userID, req.Text)
|
||||
tx.CreateMessage(message)
|
||||
return nil
|
||||
})
|
||||
message, err := h.store.CreateMessage(c.Context(), chatID, userID, req.Text)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
@@ -42,12 +44,13 @@ func createMessage(c fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusCreated).JSON(message)
|
||||
}
|
||||
|
||||
func listMessages(c fiber.Ctx) error {
|
||||
func (h *Handler) ListMessages(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")
|
||||
}
|
||||
|
||||
limit, err := common.ParseLimit(c, 50)
|
||||
@@ -60,22 +63,7 @@ func listMessages(c fiber.Ctx) error {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
if cursor != nil && chatID != 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 {
|
||||
if !tx.IsMember(userID, chatID) {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
found, fetchErr := tx.GetChatMessages(chatID, limit, cursor)
|
||||
page = found
|
||||
return fetchErr
|
||||
})
|
||||
page, err := h.store.ListMessages(c.Context(), userID, chatID, limit, cursor)
|
||||
if err != nil {
|
||||
return common.SendAppError(c, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user