init(app) moved app code from Lab1

This commit is contained in:
imadev
2026-10-21 16:42:14 +03:00
parent 65003b6119
commit 1db3953226
22 changed files with 1297 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
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")
)
+84
View File
@@ -0,0 +1,84 @@
package messages
import (
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
"github.com/gofiber/fiber/v3"
"github.com/google/uuid"
)
func 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)
}
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
})
if err != nil {
return common.SendAppError(c, err)
}
return c.Status(fiber.StatusCreated).JSON(message)
}
func 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)
}
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 && 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
})
if err != nil {
return common.SendAppError(c, err)
}
return c.JSON(page)
}
+42
View File
@@ -0,0 +1,42 @@
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
View File
@@ -0,0 +1,8 @@
package messages
import "github.com/gofiber/fiber/v3"
func RegisterRoutes(router fiber.Router) {
router.Post("/chats/:chat_id/messages", createMessage)
router.Get("/chats/:chat_id/messages", listMessages)
}