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 }