feat(app) lab2 prepared for mr

This commit is contained in:
imadev
2026-10-23 18:55:20 +03:00
parent 1db3953226
commit f6e74247fd
57 changed files with 2367 additions and 660 deletions
-8
View File
@@ -1,8 +0,0 @@
package messages
import "gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
var (
ErrChatNotFound = common.NotFound("chat not found")
ErrInvalidChatID = common.BadRequest("invalid chat_id format")
)
+22 -34
View File
@@ -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)
}
-42
View File
@@ -1,42 +0,0 @@
package messages
import (
"time"
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
"github.com/google/uuid"
)
type Message struct {
ID uuid.UUID `json:"id"`
ChatID uuid.UUID `json:"chat_id"`
SenderID uuid.UUID `json:"sender_id"`
Text string `json:"text"`
CreatedAt common.RFCFormTime `json:"created_at"`
}
type CreateMessageRequest struct {
Text string `json:"text" validate:"required"`
}
func NewMessage(chatID, senderID uuid.UUID, text string) Message {
return Message{
ID: uuid.New(),
ChatID: chatID,
SenderID: senderID,
Text: text,
CreatedAt: common.NowUTC(),
}
}
func (m Message) GetID() uuid.UUID {
return m.ID
}
func (m Message) GetCreatedAt() time.Time {
return time.Time(m.CreatedAt)
}
func (m Message) GetChatID() uuid.UUID {
return m.ChatID
}
+8 -4
View File
@@ -1,8 +1,12 @@
package messages
import "github.com/gofiber/fiber/v3"
import (
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
"github.com/gofiber/fiber/v3"
)
func RegisterRoutes(router fiber.Router) {
router.Post("/chats/:chat_id/messages", createMessage)
router.Get("/chats/:chat_id/messages", listMessages)
func RegisterRoutes(router fiber.Router, st *store.Store) {
handler := NewHandler(st)
router.Post("/chats/:chat_id/messages", handler.CreateMessage)
router.Get("/chats/:chat_id/messages", handler.ListMessages)
}