164 lines
3.7 KiB
Go
164 lines
3.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"highload/internal/database/sqlc"
|
|
"highload/internal/model"
|
|
"highload/internal/repository"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type ChatRepository struct {
|
|
queries *sqlc.Queries
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewChatRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *ChatRepository {
|
|
return &ChatRepository{
|
|
queries: queries,
|
|
pool: pool,
|
|
}
|
|
}
|
|
|
|
func (r *ChatRepository) CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error) {
|
|
txCtx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
|
|
tx, err := r.pool.Begin(txCtx)
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
defer func() {
|
|
rollbackCtx, rollbackCancel := context.WithTimeout(
|
|
context.WithoutCancel(ctx),
|
|
time.Second,
|
|
)
|
|
defer rollbackCancel()
|
|
|
|
_ = tx.Rollback(rollbackCtx)
|
|
}()
|
|
txQueries := r.queries.WithTx(tx)
|
|
|
|
sqlcChat, err := txQueries.CreateChat(txCtx, title)
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
chat := &model.Chat{
|
|
ID: sqlcChat.ID.String(),
|
|
Title: sqlcChat.Title,
|
|
CreatedAt: sqlcChat.CreatedAt.Time,
|
|
}
|
|
|
|
for _, id := range memberIDs {
|
|
userID, err := getUUIDFromString(id)
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
err = txQueries.CreateChatMember(txCtx, sqlc.CreateChatMemberParams{ChatID: sqlcChat.ID, UserID: *userID})
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
}
|
|
|
|
chat.MemberIDs = slices.Clone(memberIDs)
|
|
|
|
err = tx.Commit(txCtx)
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
return chat, nil
|
|
|
|
}
|
|
|
|
func (r *ChatRepository) GetChatByID(ctx context.Context, id string) (*model.Chat, error) {
|
|
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
uuidID, err := getUUIDFromString(id)
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
sqlcChat, err := r.queries.GetChatByID(dbCtx, *uuidID)
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
chat := mapChatSqlcToModel(&sqlcChat)
|
|
|
|
return chat, nil
|
|
|
|
}
|
|
|
|
func (r *ChatRepository) GetChatsByUserID(ctx context.Context, userID string, limit int, after *repository.ChatPosition) ([]*model.Chat, error) {
|
|
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
uuidUserID, err := getUUIDFromString(userID)
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
afterCreatedAt := pgtype.Timestamptz{Valid: false}
|
|
afterID := pgtype.UUID{Valid: false}
|
|
|
|
if after != nil {
|
|
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
|
|
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
|
|
afterID = *uuidFromString
|
|
} else {
|
|
return nil, convertDBError(err)
|
|
}
|
|
}
|
|
|
|
chats, err := r.queries.GetChatsByUserID(dbCtx, sqlc.GetChatsByUserIDParams{UserID: *uuidUserID, AfterCreatedAt: afterCreatedAt, AfterID: afterID, LimitCount: int32(limit)})
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
resultChats := make([]*model.Chat, len(chats))
|
|
for index, sqlcChat := range chats {
|
|
memberIDs := make([]string, len(sqlcChat.MemberIds))
|
|
|
|
for i, memberID := range sqlcChat.MemberIds {
|
|
memberIDs[i] = memberID.String()
|
|
}
|
|
|
|
resultChats[index] = &model.Chat{
|
|
ID: sqlcChat.ID.String(),
|
|
MemberIDs: memberIDs,
|
|
CreatedAt: sqlcChat.CreatedAt.Time,
|
|
Title: sqlcChat.Title,
|
|
}
|
|
}
|
|
|
|
return resultChats, nil
|
|
}
|
|
|
|
func mapChatSqlcToModel(sqlcChat *sqlc.GetChatByIDRow) *model.Chat {
|
|
|
|
memberIDs := make([]string, len(sqlcChat.MemberIds))
|
|
|
|
for i, memberID := range sqlcChat.MemberIds {
|
|
memberIDs[i] = memberID.String()
|
|
}
|
|
|
|
return &model.Chat{
|
|
ID: sqlcChat.ID.String(),
|
|
MemberIDs: memberIDs,
|
|
CreatedAt: sqlcChat.CreatedAt.Time,
|
|
Title: sqlcChat.Title,
|
|
}
|
|
|
|
}
|