version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s

This commit is contained in:
Ivan Rezchikov
2026-09-18 00:45:51 +03:00
commit 628dd6384b
149 changed files with 11478 additions and 0 deletions
@@ -0,0 +1,206 @@
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
}
+48
View File
@@ -0,0 +1,48 @@
package repository
import (
"context"
"github.com/dusiburg/fancy-api/internal/model"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
)
type UserRepository struct {
pool *pgxpool.Pool
}
func NewUserRepository(pool *pgxpool.Pool) *UserRepository {
return &UserRepository{pool: pool}
}
func (r *UserRepository) UpsertByKotosoftID(ctx context.Context, kotosoftID, email, firstName, secondName string) (*model.User, error) {
var u model.User
err := r.pool.QueryRow(ctx, `
INSERT INTO users (kotosoft_id, email, first_name, second_name)
VALUES ($1, $2, $3, $4)
ON CONFLICT (kotosoft_id) DO UPDATE
SET email = $2, first_name = $3, second_name = $4, "updated_at" = now()
RETURNING id, kotosoft_id, email, first_name, second_name, "updated_at", "created_at"
`, kotosoftID, email, firstName, secondName).Scan(
&u.Id, &u.KotosoftID, &u.Email, &u.FirstName, &u.SecondName, &u.UpdatedAt, &u.CreatedAt,
)
if err != nil {
return nil, err
}
return &u, nil
}
func (r *UserRepository) GetByID(ctx context.Context, id uuid.UUID) (*model.User, error) {
var u model.User
err := r.pool.QueryRow(ctx, `
SELECT id, kotosoft_id, email, first_name, second_name, "updated_at", "created_at"
FROM users WHERE id = $1
`, id).Scan(
&u.Id, &u.KotosoftID, &u.Email, &u.FirstName, &u.SecondName, &u.UpdatedAt, &u.CreatedAt,
)
if err != nil {
return nil, err
}
return &u, nil
}