Files
Ivan Rezchikov 628dd6384b
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
version 1
2026-09-18 00:45:51 +03:00

49 lines
1.4 KiB
Go

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
}