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)
|
authService := service.NewAuthService(queries, cfg.Server.SessionTTL)
|
||||||
userService := service.NewUserService(queries)
|
userService := service.NewUserService(queries)
|
||||||
chatService := service.NewChatService(memory)
|
chatService := service.NewChatService(pool, queries)
|
||||||
messageService := service.NewMessageService(memory)
|
messageService := service.NewMessageService(memory)
|
||||||
|
|
||||||
router := NewRouter(
|
router := NewRouter(
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ type ChatPageResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r CreateChatRequest) Validate() bool {
|
func (r CreateChatRequest) Validate() bool {
|
||||||
titleLen := utf8.RuneCountInString(r.Title)
|
if strings.TrimSpace(r.Title) == "" ||
|
||||||
if strings.TrimSpace(r.Title) == "" || titleLen > 100 {
|
strings.ContainsRune(r.Title, 0) ||
|
||||||
|
utf8.RuneCountInString(r.Title) > 100 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chat, err := h.service.Create(userID, req.Title, req.MemberIDs)
|
chat, err := h.service.Create(r.Context(), userID, req.Title, req.MemberIDs)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrNotChatMember):
|
case errors.Is(err, service.ErrNotChatMember):
|
||||||
httpx.WriteError(
|
httpx.WriteError(
|
||||||
@@ -94,7 +94,7 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chat, err := h.service.Get(userID, chatID)
|
chat, err := h.service.Get(r.Context(), userID, chatID)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrChatNotFound):
|
case errors.Is(err, service.ErrChatNotFound):
|
||||||
httpx.WriteError(
|
httpx.WriteError(
|
||||||
@@ -122,7 +122,7 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chats, nextCursor, err := h.service.List(userID, limit, cursor)
|
chats, nextCursor, err := h.service.List(r.Context(), userID, limit, cursor)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrInvalidCursor):
|
case errors.Is(err, service.ErrInvalidCursor):
|
||||||
httpx.WriteError(
|
httpx.WriteError(
|
||||||
|
|||||||
+109
-26
@@ -1,14 +1,19 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"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/model"
|
||||||
"mtusi2026-messenger/internal/store"
|
"mtusi2026-messenger/internal/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -18,15 +23,35 @@ var (
|
|||||||
ErrUnknownMember = errors.New("member_ids contains unknown user")
|
ErrUnknownMember = errors.New("member_ids contains unknown user")
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChatService struct {
|
type chatQueries interface {
|
||||||
store *store.Memory
|
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 {
|
type txBeginner interface {
|
||||||
return &ChatService{store: store}
|
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(
|
func (s *ChatService) Create(
|
||||||
|
ctx context.Context,
|
||||||
userID uuid.UUID,
|
userID uuid.UUID,
|
||||||
title string,
|
title string,
|
||||||
memberIDs []uuid.UUID,
|
memberIDs []uuid.UUID,
|
||||||
@@ -35,35 +60,81 @@ func (s *ChatService) Create(
|
|||||||
return model.Chat{}, ErrNotChatMember
|
return model.Chat{}, ErrNotChatMember
|
||||||
}
|
}
|
||||||
|
|
||||||
chat, err := s.store.CreateChat(title, memberIDs)
|
tx, err := s.pool.Begin(ctx)
|
||||||
switch {
|
if err != nil {
|
||||||
case errors.Is(err, store.ErrDuplicateMember):
|
return model.Chat{}, err
|
||||||
return model.Chat{}, ErrDuplicateMember
|
}
|
||||||
case errors.Is(err, store.ErrUnknownMember):
|
defer tx.Rollback(ctx)
|
||||||
return model.Chat{}, ErrUnknownMember
|
|
||||||
case err != nil:
|
q := s.withTx(tx)
|
||||||
|
|
||||||
|
id, err := uuid.NewV7()
|
||||||
|
if err != nil {
|
||||||
return model.Chat{}, err
|
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(
|
func (s *ChatService) Get(
|
||||||
|
ctx context.Context,
|
||||||
userID uuid.UUID,
|
userID uuid.UUID,
|
||||||
chatID uuid.UUID,
|
chatID uuid.UUID,
|
||||||
) (model.Chat, error) {
|
) (model.Chat, error) {
|
||||||
chat, err := s.store.GetChat(chatID, userID)
|
row, err := s.q.GetChat(ctx, db.GetChatParams{
|
||||||
if errors.Is(err, store.ErrNotFound) {
|
ID: chatID,
|
||||||
|
UserID: userID,
|
||||||
|
})
|
||||||
|
if postgres.IsNotFound(err) {
|
||||||
return model.Chat{}, ErrChatNotFound
|
return model.Chat{}, ErrChatNotFound
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Chat{}, err
|
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(
|
func (s *ChatService) List(
|
||||||
|
ctx context.Context,
|
||||||
userID uuid.UUID,
|
userID uuid.UUID,
|
||||||
limit int,
|
limit int,
|
||||||
cursor *string,
|
cursor *string,
|
||||||
@@ -81,21 +152,33 @@ func (s *ChatService) List(
|
|||||||
afterID = &decoded.ID
|
afterID = &decoded.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
chats, hasMore, err := s.store.ListChats(
|
rows, err := s.q.ListChats(ctx, db.ListChatsParams{
|
||||||
userID,
|
UserID: userID,
|
||||||
afterCreatedAt,
|
AfterCreatedAt: afterCreatedAt,
|
||||||
afterID,
|
AfterID: afterID,
|
||||||
limit,
|
Lim: int32(limit + 1),
|
||||||
)
|
})
|
||||||
if errors.Is(err, store.ErrInvalidCursor) {
|
|
||||||
return nil, nil, ErrInvalidCursor
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
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
|
var next *string
|
||||||
if hasMore && len(chats) > 0 {
|
if hasMore {
|
||||||
last := chats[len(chats)-1]
|
last := chats[len(chats)-1]
|
||||||
|
|
||||||
c := encodeCursor(Cursor{
|
c := encodeCursor(Cursor{
|
||||||
|
|||||||
Reference in New Issue
Block a user