forked from mtusi2026/mtusi2026lab2
feat(chat): store chats in Postgres
This commit is contained in:
+1
-1
@@ -45,7 +45,7 @@ func Run() error {
|
||||
|
||||
authService := service.NewAuthService(queries, cfg.Server.SessionTTL)
|
||||
userService := service.NewUserService(queries)
|
||||
chatService := service.NewChatService(memory)
|
||||
chatService := service.NewChatService(pool, queries)
|
||||
messageService := service.NewMessageService(memory)
|
||||
|
||||
router := NewRouter(
|
||||
|
||||
@@ -27,8 +27,9 @@ type ChatPageResponse struct {
|
||||
}
|
||||
|
||||
func (r CreateChatRequest) Validate() bool {
|
||||
titleLen := utf8.RuneCountInString(r.Title)
|
||||
if strings.TrimSpace(r.Title) == "" || titleLen > 100 {
|
||||
if strings.TrimSpace(r.Title) == "" ||
|
||||
strings.ContainsRune(r.Title, 0) ||
|
||||
utf8.RuneCountInString(r.Title) > 100 {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Create(userID, req.Title, req.MemberIDs)
|
||||
chat, err := h.service.Create(r.Context(), userID, req.Title, req.MemberIDs)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrNotChatMember):
|
||||
httpx.WriteError(
|
||||
@@ -94,7 +94,7 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Get(userID, chatID)
|
||||
chat, err := h.service.Get(r.Context(), userID, chatID)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrChatNotFound):
|
||||
httpx.WriteError(
|
||||
@@ -122,7 +122,7 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
chats, nextCursor, err := h.service.List(userID, limit, cursor)
|
||||
chats, nextCursor, err := h.service.List(r.Context(), userID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
|
||||
+109
-26
@@ -1,14 +1,19 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/store"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,15 +23,35 @@ var (
|
||||
ErrUnknownMember = errors.New("member_ids contains unknown user")
|
||||
)
|
||||
|
||||
type ChatService struct {
|
||||
store *store.Memory
|
||||
type chatQueries interface {
|
||||
CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error)
|
||||
AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error
|
||||
GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error)
|
||||
ListChats(ctx context.Context, arg db.ListChatsParams) ([]db.ListChatsRow, error)
|
||||
}
|
||||
|
||||
func NewChatService(store *store.Memory) *ChatService {
|
||||
return &ChatService{store: store}
|
||||
type txBeginner interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
}
|
||||
|
||||
type ChatService struct {
|
||||
pool txBeginner
|
||||
q chatQueries
|
||||
withTx func(pgx.Tx) chatQueries
|
||||
}
|
||||
|
||||
func NewChatService(pool *pgxpool.Pool, q *db.Queries) *ChatService {
|
||||
return &ChatService{
|
||||
pool: pool,
|
||||
q: q,
|
||||
withTx: func(tx pgx.Tx) chatQueries {
|
||||
return q.WithTx(tx)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
title string,
|
||||
memberIDs []uuid.UUID,
|
||||
@@ -35,35 +60,81 @@ func (s *ChatService) Create(
|
||||
return model.Chat{}, ErrNotChatMember
|
||||
}
|
||||
|
||||
chat, err := s.store.CreateChat(title, memberIDs)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrDuplicateMember):
|
||||
return model.Chat{}, ErrDuplicateMember
|
||||
case errors.Is(err, store.ErrUnknownMember):
|
||||
return model.Chat{}, ErrUnknownMember
|
||||
case err != nil:
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
q := s.withTx(tx)
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
row, err := q.CreateChat(ctx, db.CreateChatParams{
|
||||
ID: id,
|
||||
Title: title,
|
||||
})
|
||||
|
||||
err = q.AddChatMembers(ctx, db.AddChatMembersParams{
|
||||
ChatID: row.ID,
|
||||
UserIds: memberIDs,
|
||||
})
|
||||
switch postgres.ErrCode(err) {
|
||||
case postgres.CodeUniqueViolation:
|
||||
return model.Chat{}, ErrDuplicateMember
|
||||
case postgres.CodeForeignKeyViolation:
|
||||
return model.Chat{}, ErrUnknownMember
|
||||
}
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
ids := slices.Clone(memberIDs)
|
||||
slices.SortFunc(ids, func(a, b uuid.UUID) int {
|
||||
return bytes.Compare(a[:], b[:])
|
||||
})
|
||||
|
||||
return model.Chat{
|
||||
ID: row.ID,
|
||||
Title: row.Title,
|
||||
CreatedAt: row.CreatedAt,
|
||||
MemberIDs: ids,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
chatID uuid.UUID,
|
||||
) (model.Chat, error) {
|
||||
chat, err := s.store.GetChat(chatID, userID)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
row, err := s.q.GetChat(ctx, db.GetChatParams{
|
||||
ID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.Chat{}, ErrChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
return model.Chat{
|
||||
ID: row.ID,
|
||||
Title: row.Title,
|
||||
CreatedAt: row.CreatedAt,
|
||||
MemberIDs: row.MemberIds,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
limit int,
|
||||
cursor *string,
|
||||
@@ -81,21 +152,33 @@ func (s *ChatService) List(
|
||||
afterID = &decoded.ID
|
||||
}
|
||||
|
||||
chats, hasMore, err := s.store.ListChats(
|
||||
userID,
|
||||
afterCreatedAt,
|
||||
afterID,
|
||||
limit,
|
||||
)
|
||||
if errors.Is(err, store.ErrInvalidCursor) {
|
||||
return nil, nil, ErrInvalidCursor
|
||||
}
|
||||
rows, err := s.q.ListChats(ctx, db.ListChatsParams{
|
||||
UserID: userID,
|
||||
AfterCreatedAt: afterCreatedAt,
|
||||
AfterID: afterID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
hasMore := len(rows) > limit
|
||||
if hasMore {
|
||||
rows = rows[:limit]
|
||||
}
|
||||
|
||||
chats := make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
})
|
||||
}
|
||||
|
||||
var next *string
|
||||
if hasMore && len(chats) > 0 {
|
||||
if hasMore {
|
||||
last := chats[len(chats)-1]
|
||||
|
||||
c := encodeCursor(Cursor{
|
||||
|
||||
Reference in New Issue
Block a user