package repository import ( "context" "errors" "fmt" "github.com/dusiburg/fancy-api/internal/model" "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" ) var ( ErrCollectionNotFound = errors.New("collection not found") ) const historyCollectionName = "history" type CollectionRepository struct { pool *pgxpool.Pool } func NewCollectionRepository(pool *pgxpool.Pool) *CollectionRepository { return &CollectionRepository{pool: pool} } func (r *CollectionRepository) GetCollections(ctx context.Context, userID uuid.UUID) ([]model.Collection, error) { var collections []model.Collection rows, err := r.pool.Query(ctx, `SELECT id, name, lang, user_id, words, type, created_at, updated_at FROM collections WHERE user_id = $1 ORDER BY CASE WHEN type = 'user' THEN 0 ELSE 1 END, updated_at DESC`, userID, ) if err != nil { return nil, fmt.Errorf("getting collections: %w", mapPgError(err)) } defer rows.Close() for rows.Next() { var collection model.Collection err := rows.Scan( &collection.Id, &collection.Name, &collection.Lang, &collection.UserID, &collection.Words, &collection.Type, &collection.CreatedAt, &collection.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("scanning collection: %w", err) } collections = append(collections, collection) } if err = rows.Err(); err != nil { return nil, fmt.Errorf("rows iteration error: %w", err) } return collections, nil } func (r *CollectionRepository) GetCollectionByID(ctx context.Context, collectionID uuid.UUID) (*model.Collection, error) { var collection model.Collection err := r.pool.QueryRow(ctx, `SELECT id, name, lang, user_id, words, type, created_at, updated_at FROM collections WHERE id = $1`, collectionID, ).Scan( &collection.Id, &collection.Name, &collection.Lang, &collection.UserID, &collection.Words, &collection.Type, &collection.CreatedAt, &collection.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("getting collection: %w", mapPgError(err)) } return &collection, nil } func (r *CollectionRepository) GetCollectionsByWord(ctx context.Context, userID uuid.UUID, lang model.Language, word string) ([]model.Collection, error) { collections := make([]model.Collection, 0) rows, err := r.pool.Query(ctx, `SELECT id, name, lang, user_id, words, type, created_at, updated_at FROM collections WHERE user_id = $1 AND lang = $2 AND $3 = ANY(words)`, userID, lang, word, ) if err != nil { return nil, fmt.Errorf("getting collections: %w", mapPgError(err)) } defer rows.Close() for rows.Next() { var collection model.Collection err := rows.Scan( &collection.Id, &collection.Name, &collection.Lang, &collection.UserID, &collection.Words, &collection.Type, &collection.CreatedAt, &collection.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("scanning collection: %w", err) } collections = append(collections, collection) } if err = rows.Err(); err != nil { return nil, fmt.Errorf("rows iteration error: %w", err) } return collections, nil } func (r *CollectionRepository) CreateCollection(ctx context.Context, collection *model.Collection) (*model.Collection, error) { var created model.Collection err := r.pool.QueryRow(ctx, `INSERT INTO collections (name, lang, user_id, words, type) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, lang, user_id, words, type, created_at, updated_at`, collection.Name, collection.Lang, collection.UserID, collection.Words, collection.Type, ).Scan( &created.Id, &created.Name, &created.Lang, &created.UserID, &created.Words, &created.Type, &created.CreatedAt, &created.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("create collection: %w", mapPgError(err)) } return &created, nil } func (r *CollectionRepository) GetOrCreateAutoCollection(ctx context.Context, userID uuid.UUID, lang model.Language) (*model.Collection, error) { var c model.Collection err := r.pool.QueryRow(ctx, `SELECT id, name, lang, user_id, words, type, created_at, updated_at FROM collections WHERE user_id = $1 AND lang = $2 AND type = $3`, userID, lang, model.CollectionTypeAuto, ).Scan( &c.Id, &c.Name, &c.Lang, &c.UserID, &c.Words, &c.Type, &c.CreatedAt, &c.UpdatedAt, ) if err == nil { return &c, nil } if !errors.Is(mapPgError(err), ErrCollectionNotFound) { return nil, fmt.Errorf("getting history collection: %w", err) } err = r.pool.QueryRow(ctx, `INSERT INTO collections (name, lang, user_id, words, type) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, lang, user_id, words, type, created_at, updated_at`, historyCollectionName, lang, userID, []string{}, model.CollectionTypeAuto, ).Scan( &c.Id, &c.Name, &c.Lang, &c.UserID, &c.Words, &c.Type, &c.CreatedAt, &c.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("creating history collection: %w", mapPgError(err)) } return &c, nil } func (r *CollectionRepository) UpdateCollection(ctx context.Context, collection *model.Collection) (*model.Collection, error) { var created model.Collection err := r.pool.QueryRow(ctx, `UPDATE collections SET name = $1, lang = $2, words = $3, type = $4, updated_at = now() WHERE id = $5 RETURNING id, name, lang, user_id, words, type, created_at, updated_at`, collection.Name, collection.Lang, collection.Words, collection.Type, collection.Id, ).Scan( &created.Id, &created.Name, &created.Lang, &created.UserID, &created.Words, &created.Type, &created.CreatedAt, &created.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("update collection: %w", mapPgError(err)) } return &created, nil } func (r *CollectionRepository) DeleteCollection(ctx context.Context, collectionID uuid.UUID) error { _, err := r.pool.Exec(ctx, "DELETE FROM collections WHERE id = $1", collectionID) if err != nil { return fmt.Errorf("delete collection: %w", mapPgError(err)) } return nil } func mapPgError(err error) error { if errors.Is(err, pgx.ErrNoRows) { return ErrCollectionNotFound } return err }