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
-9
View File
@@ -1,9 +0,0 @@
package chats
import "gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
var (
ErrChatNotFound = common.NotFound("chat not found")
ErrInvalidChatID = common.BadRequest("invalid chat_id format")
ErrMemberNotFound = common.BadRequest("member_id refers to non-existent user")
)
+24 -44
View File
@@ -1,13 +1,26 @@
package chats
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 createChat(c fiber.Ctx) error {
type Handler struct {
store *store.Store
}
func NewHandler(st *store.Store) *Handler {
return &Handler{store: st}
}
type CreateChatRequest struct {
Title string `json:"title" validate:"required"`
MemberIDs []uuid.UUID `json:"member_ids" validate:"required"`
}
func (h *Handler) CreateChat(c fiber.Ctx) error {
userID := c.Locals("userID").(uuid.UUID)
var req CreateChatRequest
@@ -18,24 +31,11 @@ func createChat(c fiber.Ctx) error {
if msg := common.ValidateChatTitle(req.Title); msg != "" {
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
}
if msg := common.ValidateMemberIDs(req.MemberIDs, userID); msg != "" {
return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg)
}
var chat Chat
err := storage.GlobalStore.Update(func(tx storage.Tx) error {
for _, memberID := range req.MemberIDs {
if _, ok := tx.GetUser(memberID); !ok {
return ErrMemberNotFound
}
}
chat = NewChat(req)
tx.CreateChat(chat)
return nil
})
chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs)
if err != nil {
return common.SendAppError(c, err)
}
@@ -43,7 +43,7 @@ func createChat(c fiber.Ctx) error {
return c.Status(fiber.StatusCreated).JSON(chat)
}
func listChats(c fiber.Ctx) error {
func (h *Handler) ListChats(c fiber.Ctx) error {
userID := c.Locals("userID").(uuid.UUID)
limit, err := common.ParseLimit(c, 50)
@@ -55,19 +55,12 @@ func listChats(c fiber.Ctx) error {
if err != nil {
return common.SendAppError(c, err)
}
if cursor != nil && userID != cursor.ContextID {
if cursor != nil && cursor.ContextID != userID {
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 {
found, fetchErr := tx.GetUserChats(userID, limit, cursor)
page = found
return fetchErr
})
page, err := h.store.ListChats(c.Context(), userID, limit, cursor)
if err != nil {
return common.SendAppError(c, err)
}
@@ -75,29 +68,16 @@ func listChats(c fiber.Ctx) error {
return c.JSON(page)
}
func getChat(c fiber.Ctx) error {
func (h *Handler) GetChat(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 chat storage.ChatEntity
err = storage.GlobalStore.View(func(tx storage.ReadOnlyTx) error {
if !tx.IsMember(userID, chatID) {
return ErrChatNotFound
}
found, ok := tx.GetChat(chatID)
if !ok {
return ErrChatNotFound
}
chat = found
return nil
})
chat, err := h.store.GetChatForUser(c.Context(), userID, chatID)
if err != nil {
return common.SendAppError(c, err)
}
-41
View File
@@ -1,41 +0,0 @@
package chats
import (
"time"
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
"github.com/google/uuid"
)
type Chat struct {
ID uuid.UUID `json:"id"`
Title string `json:"title"`
MemberIDs []uuid.UUID `json:"member_ids"`
CreatedAt common.RFCFormTime `json:"created_at"`
}
type CreateChatRequest struct {
Title string `json:"title" validate:"required"`
MemberIDs []uuid.UUID `json:"member_ids" validate:"required"`
}
func NewChat(req CreateChatRequest) Chat {
return Chat{
ID: uuid.New(),
Title: req.Title,
MemberIDs: req.MemberIDs,
CreatedAt: common.NowUTC(),
}
}
func (c Chat) GetID() uuid.UUID {
return c.ID
}
func (c Chat) GetCreatedAt() time.Time {
return time.Time(c.CreatedAt)
}
func (c Chat) GetMemberIDs() []uuid.UUID {
return c.MemberIDs
}
+9 -5
View File
@@ -1,9 +1,13 @@
package chats
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", createChat)
router.Get("/chats", listChats)
router.Get("/chats/:chat_id", getChat)
func RegisterRoutes(router fiber.Router, st *store.Store) {
handler := NewHandler(st)
router.Post("/chats", handler.CreateChat)
router.Get("/chats", handler.ListChats)
router.Get("/chats/:chat_id", handler.GetChat)
}