rewrite competition service: remove http related things
This commit is contained in:
@@ -2,25 +2,25 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"datarush/internal/competition/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
pb "datarush/pkg/api/competition"
|
||||
)
|
||||
|
||||
type ListCompetitionsOptions struct {
|
||||
Page int
|
||||
PageSize int
|
||||
State *domain.CompetitionState
|
||||
State *pb.CompetitionState
|
||||
IsParticipating *bool
|
||||
SearchQuery *string
|
||||
}
|
||||
|
||||
type CompetitionRepository interface {
|
||||
Create(ctx context.Context, competition *domain.Competition) error
|
||||
Get(ctx context.Context, id uuid.UUID) (*domain.Competition, error)
|
||||
Update(ctx context.Context, competition *domain.Competition) error
|
||||
Create(ctx context.Context, competition *pb.Competition) (*pb.Competition, error)
|
||||
Get(ctx context.Context, id uuid.UUID) (*pb.Competition, error)
|
||||
Update(ctx context.Context, competition *pb.Competition) (*pb.Competition, error)
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
List(ctx context.Context, opts ListCompetitionsOptions) ([]domain.Competition, int, error)
|
||||
ChangeState(ctx context.Context, id uuid.UUID, state domain.CompetitionState) (*domain.Competition, error)
|
||||
}
|
||||
List(ctx context.Context, opts ListCompetitionsOptions) ([]*pb.Competition, int, error)
|
||||
ChangeState(ctx context.Context, id uuid.UUID, state pb.CompetitionState) (*pb.Competition, error)
|
||||
}
|
||||
@@ -2,152 +2,91 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"datarush/internal/competition/domain"
|
||||
"datarush/internal/competition/repository"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
"datarush/internal/competition/repository"
|
||||
pb "datarush/pkg/api/competition"
|
||||
|
||||
const (
|
||||
competitionCachePrefix = "competition:"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type CompetitionRepository struct {
|
||||
db *sql.DB
|
||||
redisClient *redis.Client
|
||||
cacheEnabled bool
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewCompetitionRepository(db *sql.DB, redisClient *redis.Client, cacheEnabled bool) repository.CompetitionRepository {
|
||||
func NewCompetitionRepository(db *sqlx.DB) repository.CompetitionRepository {
|
||||
return &CompetitionRepository{
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
cacheEnabled: cacheEnabled,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) cacheKey(id string) string {
|
||||
return competitionCachePrefix + id
|
||||
}
|
||||
func (r *CompetitionRepository) Create(ctx context.Context, c *pb.Competition) (*pb.Competition, error) {
|
||||
query := `INSERT INTO competitions (state, title, description, image_url, start_time, end_time, type, participation_type)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, created_at, updated_at`
|
||||
|
||||
func (r *CompetitionRepository) Create(ctx context.Context, competition *domain.Competition) error {
|
||||
query := `INSERT INTO competitions (id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
competition.ID,
|
||||
competition.State,
|
||||
competition.Title,
|
||||
competition.Description,
|
||||
competition.ImageURL,
|
||||
competition.StartTime,
|
||||
competition.EndTime,
|
||||
competition.Type,
|
||||
competition.ParticipationType,
|
||||
competition.CreatedAt,
|
||||
competition.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
data, err := json.Marshal(competition)
|
||||
if err == nil {
|
||||
r.redisClient.Set(ctx, r.cacheKey(competition.ID.String()), data, 10*time.Minute).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Get(ctx context.Context, id uuid.UUID) (*domain.Competition, error) {
|
||||
if r.cacheEnabled {
|
||||
val, err := r.redisClient.Get(ctx, r.cacheKey(id.String())).Result()
|
||||
if err == nil {
|
||||
var competition domain.Competition
|
||||
if json.Unmarshal([]byte(val), &competition) == nil {
|
||||
return &competition, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query := `SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions WHERE id = $1`
|
||||
row := r.db.QueryRowContext(ctx, query, id)
|
||||
|
||||
var competition domain.Competition
|
||||
err := row.Scan(
|
||||
&competition.ID,
|
||||
&competition.State,
|
||||
&competition.Title,
|
||||
&competition.Description,
|
||||
&competition.ImageURL,
|
||||
&competition.StartTime,
|
||||
&competition.EndTime,
|
||||
&competition.Type,
|
||||
&competition.ParticipationType,
|
||||
&competition.CreatedAt,
|
||||
&competition.UpdatedAt,
|
||||
)
|
||||
var createdCompetition pb.Competition
|
||||
err := r.db.QueryRowxContext(ctx, query,
|
||||
c.State,
|
||||
c.Title,
|
||||
c.Description,
|
||||
c.ImageUrl,
|
||||
c.StartTime.AsTime(),
|
||||
c.EndTime.AsTime(),
|
||||
c.Type,
|
||||
c.ParticipationType,
|
||||
).StructScan(&createdCompetition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Id = createdCompetition.Id
|
||||
c.CreatedAt = createdCompetition.CreatedAt
|
||||
c.UpdatedAt = createdCompetition.UpdatedAt
|
||||
|
||||
if r.cacheEnabled {
|
||||
data, err := json.Marshal(&competition)
|
||||
if err == nil {
|
||||
r.redisClient.Set(ctx, r.cacheKey(id.String()), data, 10*time.Minute).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return &competition, nil
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Update(ctx context.Context, competition *domain.Competition) error {
|
||||
func (r *CompetitionRepository) Get(ctx context.Context, id uuid.UUID) (*pb.Competition, error) {
|
||||
query := `SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions WHERE id = $1`
|
||||
var competition pb.Competition
|
||||
err := r.db.GetContext(ctx, &competition, query, id)
|
||||
return &competition, err
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Update(ctx context.Context, c *pb.Competition) (*pb.Competition, error) {
|
||||
query := `UPDATE competitions SET
|
||||
state = $2, title = $3, description = $4, image_url = $5, start_time = $6, end_time = $7, type = $8, participation_type = $9, updated_at = $10
|
||||
WHERE id = $1`
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
competition.ID,
|
||||
competition.State,
|
||||
competition.Title,
|
||||
competition.Description,
|
||||
competition.ImageURL,
|
||||
competition.StartTime,
|
||||
competition.EndTime,
|
||||
competition.Type,
|
||||
competition.ParticipationType,
|
||||
competition.UpdatedAt,
|
||||
)
|
||||
state = $2, title = $3, description = $4, image_url = $5, start_time = $6, end_time = $7, type = $8, participation_type = $9, updated_at = now()
|
||||
WHERE id = $1 RETURNING updated_at`
|
||||
|
||||
var updatedCompetition pb.Competition
|
||||
err := r.db.QueryRowxContext(ctx, query,
|
||||
c.Id,
|
||||
c.State,
|
||||
c.Title,
|
||||
c.Description,
|
||||
c.ImageUrl,
|
||||
c.StartTime.AsTime(),
|
||||
c.EndTime.AsTime(),
|
||||
c.Type,
|
||||
c.ParticipationType,
|
||||
).StructScan(&updatedCompetition)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
c.UpdatedAt = updatedCompetition.UpdatedAt
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(competition.ID.String())).Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
query := `DELETE FROM competitions WHERE id = $1`
|
||||
_, err := r.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(id.String())).Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]domain.Competition, int, error) {
|
||||
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]*pb.Competition, int, error) {
|
||||
var args []interface{}
|
||||
var whereClauses []string
|
||||
argId := 1
|
||||
@@ -164,15 +103,15 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
}
|
||||
|
||||
if opts.IsParticipating != nil {
|
||||
userID, ok := ctx.Value("user_id").(uuid.UUID)
|
||||
userID, ok := ctx.Value("user_id").(string)
|
||||
if !ok {
|
||||
return nil, 0, fmt.Errorf("user not authenticated or user_id not in context")
|
||||
}
|
||||
|
||||
if *opts.IsParticipating {
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT competition_id FROM competition_participants WHERE user_id = $%d)", argId))
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT competition_id FROM user_competitions WHERE user_id = $%d)", argId))
|
||||
} else {
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (SELECT competition_id FROM competition_participants WHERE user_id = $%d)", argId))
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (SELECT competition_id FROM user_competitions WHERE user_id = $%d)", argId))
|
||||
}
|
||||
args = append(args, userID)
|
||||
argId++
|
||||
@@ -185,54 +124,30 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
|
||||
countQuery := "SELECT COUNT(*) FROM competitions " + where
|
||||
var total int
|
||||
if err := r.db.QueryRowContext(ctx, countQuery, args...).Scan(&total); err != nil {
|
||||
if err := r.db.GetContext(ctx, &total, countQuery, args...); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d`, where, argId, argId+1)
|
||||
args = append(args, opts.PageSize, opts.Page*opts.PageSize)
|
||||
args = append(args, opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
var competitions []*pb.Competition
|
||||
err := r.db.SelectContext(ctx, &competitions, query, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var competitions []domain.Competition
|
||||
for rows.Next() {
|
||||
var c domain.Competition
|
||||
err := rows.Scan(
|
||||
&c.ID,
|
||||
&c.State,
|
||||
&c.Title,
|
||||
&c.Description,
|
||||
&c.ImageURL,
|
||||
&c.StartTime,
|
||||
&c.EndTime,
|
||||
&c.Type,
|
||||
&c.ParticipationType,
|
||||
&c.CreatedAt,
|
||||
&c.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
competitions = append(competitions, c)
|
||||
}
|
||||
|
||||
return competitions, total, nil
|
||||
}
|
||||
func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, state domain.CompetitionState) (*domain.Competition, error) {
|
||||
query := `UPDATE competitions SET state = $1, updated_at = $2 WHERE id = $3`
|
||||
|
||||
func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, state pb.CompetitionState) (*pb.Competition, error) {
|
||||
query := `UPDATE competitions SET state = $1, updated_at = $2 WHERE id = $3 RETURNING updated_at`
|
||||
now := time.Now()
|
||||
_, err := r.db.ExecContext(ctx, query, state, now, id)
|
||||
var competition pb.Competition
|
||||
err := r.db.QueryRowxContext(ctx, query, state, now, id).StructScan(&competition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(id.String())).Err()
|
||||
}
|
||||
|
||||
return r.Get(ctx, id)
|
||||
}
|
||||
Reference in New Issue
Block a user