package storage import ( "context" "errors" "github.com/google/uuid" "github.com/jackc/pgerrcode" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgxpool" "gitea.privcloud.tech/mtusi2026/internal/dto" "gitea.privcloud.tech/mtusi2026/internal/pkg/apperr" "gitea.privcloud.tech/mtusi2026/internal/pkg/postgres" ) type UsersRepo struct { pool *pgxpool.Pool } func NewUsersRepo(pool *pgxpool.Pool) *UsersRepo { return &UsersRepo{pool: pool} } func (r *UsersRepo) Create(ctx context.Context, req dto.CreateStoredUser) error { return r.create(ctx, r.pool, req) } func (r *UsersRepo) CreateTx(ctx context.Context, tx pgx.Tx, req dto.CreateStoredUser) error { return r.create(ctx, tx, req) } func (r *UsersRepo) GetByID(ctx context.Context, id uuid.UUID) (*dto.User, error) { return r.getByID(ctx, r.pool, id) } func (r *UsersRepo) GetByIDTx(ctx context.Context, tx pgx.Tx, id uuid.UUID) (*dto.User, error) { return r.getByID(ctx, tx, id) } func (r *UsersRepo) GetByUsername(ctx context.Context, username string) (*dto.User, error) { const query = `SELECT id, username, name, created_at FROM users WHERE username = $1` return scanUser(r.pool.QueryRow(ctx, query, username)) } func (r *UsersRepo) GetCredentialsByUsername(ctx context.Context, username string) (*dto.UserCredentials, error) { const query = `SELECT id, username, name, created_at, password_hash FROM users WHERE username = $1` var credentials dto.UserCredentials err := r.pool.QueryRow(ctx, query, username).Scan( &credentials.User.ID, &credentials.User.Username, &credentials.User.DisplayName, &credentials.User.CreatedAt, &credentials.PasswordHash, ) if err != nil { return nil, apperr.WrapPostgres(err, "failed to get user credentials") } return &credentials, nil } func (r *UsersRepo) Delete(ctx context.Context, id uuid.UUID) error { const query = `DELETE FROM users WHERE id = $1` ct, err := r.pool.Exec(ctx, query, id) if err != nil { return apperr.WrapPostgres(err, "failed to delete user") } if ct.RowsAffected() == 0 { return apperr.New(apperr.CodeNotFound, "user not found") } return nil } func (r *UsersRepo) create(ctx context.Context, conn postgres.Conn, req dto.CreateStoredUser) error { const query = `INSERT INTO users (id, username, name, password_hash, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)` _, err := conn.Exec(ctx, query, req.ID, req.Username, req.DisplayName, req.PasswordHash, req.CreatedAt, req.UpdatedAt) if err != nil { if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == pgerrcode.UniqueViolation && pgErr.ConstraintName == "users_username_unique" { return apperr.Wrap(err, apperr.CodeConflict, "username already exists") } return apperr.WrapPostgres(err, "failed to create user") } return nil } func (r *UsersRepo) getByID(ctx context.Context, conn postgres.Conn, id uuid.UUID) (*dto.User, error) { const query = `SELECT id, username, name, created_at FROM users WHERE id = $1` return scanUser(conn.QueryRow(ctx, query, id)) } func scanUser(row pgx.Row) (*dto.User, error) { var user dto.User if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt); err != nil { return nil, apperr.WrapPostgres(err, "failed to get user") } return &user, nil }