merch-api/internal/user/repository.go
2026-03-20 16:09:12 +03:00

49 lines
1.2 KiB
Go

package user
import (
"context"
"database/sql"
"github.com/jackc/pgx/v5/pgxpool"
)
type Repository interface {
getUserId(ctx context.Context, userUuid string) (int64, error)
addUser(ctx context.Context, u *userModel) error
deleteUser(ctx context.Context, userUuid string, deletedAt sql.NullTime) error
}
type repo struct {
db *pgxpool.Pool
}
func newRepository(db *pgxpool.Pool) Repository {
return &repo{
db: db,
}
}
func (r *repo) getUserId(ctx context.Context, userUuid string) (int64, error) {
q := `SELECT id FROM users WHERE uuid = $1 AND deleted_at IS NULL LIMIT 1`
row := r.db.QueryRow(ctx, q, userUuid)
var id int64
if err := row.Scan(&id); err != nil {
return 0, err
}
return id, nil
}
func (r *repo) addUser(ctx context.Context, u *userModel) error {
q := `INSERT INTO users (created_at, updated_at, deleted_at, uuid) VALUES ($1, $2, $3, $4)`
_, err := r.db.Exec(ctx, q, u.CreatedAt, u.UpdatedAt, u.DeletedAt, u.Uuid)
return err
}
func (r *repo) deleteUser(ctx context.Context, userUuid string, deletedAt sql.NullTime) error {
q := `UPDATE users SET deleted_at = $1 WHERE uuid = $2`
_, err := r.db.Exec(ctx, q, deletedAt, userUuid)
return err
}