38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"datarush/pkg/api/user"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
type UserRepository struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewUserRepository(db *sqlx.DB) *UserRepository {
|
|
return &UserRepository{db: db}
|
|
}
|
|
|
|
func (r *UserRepository) GetProfile(ctx context.Context, userID string) (*user.User, error) {
|
|
var u user.User
|
|
err := r.db.GetContext(ctx, &u, "SELECT id, username, email, full_name, avatar_url FROM users WHERE id = $1", userID)
|
|
return &u, err
|
|
}
|
|
|
|
func (r *UserRepository) RegisterForCompetition(ctx context.Context, userID, competitionID string) error {
|
|
_, err := r.db.ExecContext(ctx, "INSERT INTO user_competitions (user_id, competition_id) VALUES ($1, $2)", userID, competitionID)
|
|
return err
|
|
}
|
|
|
|
func (r *UserRepository) UnregisterFromCompetition(ctx context.Context, userID, competitionID string) error {
|
|
_, err := r.db.ExecContext(ctx, "DELETE FROM user_competitions WHERE user_id = $1 AND competition_id = $2", userID, competitionID)
|
|
return err
|
|
}
|
|
|
|
func (r *UserRepository) ListUserCompetitions(ctx context.Context, userID string) ([]string, error) {
|
|
var competitionIDs []string
|
|
err := r.db.SelectContext(ctx, &competitionIDs, "SELECT competition_id FROM user_competitions WHERE user_id = $1", userID)
|
|
return competitionIDs, err
|
|
} |