fix: fix&refactor
This commit is contained in:
parent
5536c086e0
commit
318599cfea
5 changed files with 84 additions and 52 deletions
|
@ -4,3 +4,10 @@ type Pagination struct {
|
||||||
Page int32 `json:"page"`
|
Page int32 `json:"page"`
|
||||||
Total int32 `json:"total"`
|
Total int32 `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Total(count int32, pageSize int32) int32 {
|
||||||
|
if count%pageSize == 0 {
|
||||||
|
return count / pageSize
|
||||||
|
}
|
||||||
|
return count/pageSize + 1
|
||||||
|
}
|
||||||
|
|
|
@ -152,7 +152,7 @@ FROM tasks
|
||||||
INNER JOIN problems ON tasks.problem_id = problems.id
|
INNER JOIN problems ON tasks.problem_id = problems.id
|
||||||
WHERE contest_id = ? ORDER BY position`
|
WHERE contest_id = ? ORDER BY position`
|
||||||
|
|
||||||
func (r *ContestRepository) ReadRichTasks(ctx context.Context, contestId int32) ([]*models.TasksListItem, error) {
|
func (r *ContestRepository) ReadTasks(ctx context.Context, contestId int32) ([]*models.TasksListItem, error) {
|
||||||
const op = "ContestRepository.ReadTasks"
|
const op = "ContestRepository.ReadTasks"
|
||||||
|
|
||||||
var tasks []*models.TasksListItem
|
var tasks []*models.TasksListItem
|
||||||
|
@ -169,24 +169,30 @@ const (
|
||||||
countContestsQuery = "SELECT COUNT(*) FROM contests"
|
countContestsQuery = "SELECT COUNT(*) FROM contests"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *ContestRepository) ListContests(ctx context.Context, page int32, pageSize int32) ([]*models.ContestsListItem, int32, error) {
|
func (r *ContestRepository) ListContests(ctx context.Context, filter models.ContestsFilter) (*models.ContestsList, error) {
|
||||||
const op = "ContestRepository.ReadTasks"
|
const op = "ContestRepository.ReadTasks"
|
||||||
|
|
||||||
var tasks []*models.ContestsListItem
|
var contests []*models.ContestsListItem
|
||||||
query := r.db.Rebind(readContestsListQuery)
|
query := r.db.Rebind(readContestsListQuery)
|
||||||
err := r.db.SelectContext(ctx, &tasks, query, pageSize, (page-1)*pageSize)
|
err := r.db.SelectContext(ctx, &contests, query, filter.PageSize, filter.Offset())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = r.db.Rebind(countContestsQuery)
|
query = r.db.Rebind(countContestsQuery)
|
||||||
var count int32
|
var count int32
|
||||||
err = r.db.GetContext(ctx, &count, query)
|
err = r.db.GetContext(ctx, &count, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tasks, count, nil
|
return &models.ContestsList{
|
||||||
|
Contests: contests,
|
||||||
|
Pagination: models.Pagination{
|
||||||
|
Total: models.Total(count, filter.PageSize),
|
||||||
|
Page: filter.Page,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -194,28 +200,34 @@ const (
|
||||||
countParticipantsQuery = "SELECT COUNT(*) FROM participants WHERE contest_id = ?"
|
countParticipantsQuery = "SELECT COUNT(*) FROM participants WHERE contest_id = ?"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *ContestRepository) ListParticipants(ctx context.Context, contestId int32, page int32, pageSize int32) ([]*models.ParticipantsListItem, int32, error) {
|
func (r *ContestRepository) ListParticipants(ctx context.Context, filter models.ParticipantsFilter) (*models.ParticipantsList, error) {
|
||||||
const op = "ContestRepository.ReadParticipants"
|
const op = "ContestRepository.ReadParticipants"
|
||||||
|
|
||||||
if pageSize > 20 {
|
if filter.PageSize > 20 {
|
||||||
pageSize = 1
|
filter.PageSize = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
var participants []*models.ParticipantsListItem
|
var participants []*models.ParticipantsListItem
|
||||||
query := r.db.Rebind(readParticipantsListQuery)
|
query := r.db.Rebind(readParticipantsListQuery)
|
||||||
err := r.db.SelectContext(ctx, &participants, query, contestId, pageSize, (page-1)*pageSize)
|
err := r.db.SelectContext(ctx, &participants, query, filter.ContestId, filter.PageSize, filter.Offset())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = r.db.Rebind(countParticipantsQuery)
|
query = r.db.Rebind(countParticipantsQuery)
|
||||||
var count int32
|
var count int32
|
||||||
err = r.db.GetContext(ctx, &count, query, contestId)
|
err = r.db.GetContext(ctx, &count, query, filter.ContestId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
return participants, count, nil
|
return &models.ParticipantsList{
|
||||||
|
Participants: participants,
|
||||||
|
Pagination: models.Pagination{
|
||||||
|
Total: models.Total(count, filter.PageSize),
|
||||||
|
Page: filter.Page,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -301,7 +313,7 @@ func (r *ContestRepository) CreateSolution(ctx context.Context, creation *models
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ContestRepository) ListSolutions(ctx context.Context, filters models.SolutionsFilter) ([]*models.SolutionsListItem, int32, error) {
|
func (r *ContestRepository) ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error) {
|
||||||
const op = "ContestRepository.ListSolutions"
|
const op = "ContestRepository.ListSolutions"
|
||||||
|
|
||||||
baseQuery := `
|
baseQuery := `
|
||||||
|
@ -325,34 +337,34 @@ func (r *ContestRepository) ListSolutions(ctx context.Context, filters models.So
|
||||||
var conditions []string
|
var conditions []string
|
||||||
var args []interface{}
|
var args []interface{}
|
||||||
|
|
||||||
if filters.ContestId != nil {
|
if filter.ContestId != nil {
|
||||||
conditions = append(conditions, "contest_id = ?")
|
conditions = append(conditions, "contest_id = ?")
|
||||||
args = append(args, *filters.ContestId)
|
args = append(args, *filter.ContestId)
|
||||||
}
|
}
|
||||||
if filters.ParticipantId != nil {
|
if filter.ParticipantId != nil {
|
||||||
conditions = append(conditions, "participant_id = ?")
|
conditions = append(conditions, "participant_id = ?")
|
||||||
args = append(args, *filters.ParticipantId)
|
args = append(args, *filter.ParticipantId)
|
||||||
}
|
}
|
||||||
if filters.TaskId != nil {
|
if filter.TaskId != nil {
|
||||||
conditions = append(conditions, "task_id = ?")
|
conditions = append(conditions, "task_id = ?")
|
||||||
args = append(args, *filters.TaskId)
|
args = append(args, *filter.TaskId)
|
||||||
}
|
}
|
||||||
if filters.Language != nil {
|
if filter.Language != nil {
|
||||||
conditions = append(conditions, "language = ?")
|
conditions = append(conditions, "language = ?")
|
||||||
args = append(args, *filters.Language)
|
args = append(args, *filter.Language)
|
||||||
}
|
}
|
||||||
if filters.State != nil {
|
if filter.State != nil {
|
||||||
conditions = append(conditions, "state = ?")
|
conditions = append(conditions, "state = ?")
|
||||||
args = append(args, *filters.State)
|
args = append(args, *filter.State)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(conditions) > 0 {
|
if len(conditions) > 0 {
|
||||||
baseQuery += " AND " + strings.Join(conditions, " AND ")
|
baseQuery += " AND " + strings.Join(conditions, " AND ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if filters.Order != nil {
|
if filter.Order != nil {
|
||||||
orderDirection := "ASC"
|
orderDirection := "ASC"
|
||||||
if *filters.Order < 0 {
|
if *filter.Order < 0 {
|
||||||
orderDirection = "DESC"
|
orderDirection = "DESC"
|
||||||
}
|
}
|
||||||
baseQuery += fmt.Sprintf(" ORDER BY s.id %s", orderDirection)
|
baseQuery += fmt.Sprintf(" ORDER BY s.id %s", orderDirection)
|
||||||
|
@ -362,16 +374,16 @@ func (r *ContestRepository) ListSolutions(ctx context.Context, filters models.So
|
||||||
var totalCount int32
|
var totalCount int32
|
||||||
err := r.db.QueryRowxContext(ctx, r.db.Rebind(countQuery), args...).Scan(&totalCount)
|
err := r.db.QueryRowxContext(ctx, r.db.Rebind(countQuery), args...).Scan(&totalCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := (filters.Page - 1) * filters.PageSize
|
offset := (filter.Page - 1) * filter.PageSize
|
||||||
baseQuery += " LIMIT ? OFFSET ?"
|
baseQuery += " LIMIT ? OFFSET ?"
|
||||||
args = append(args, filters.PageSize, offset)
|
args = append(args, filter.PageSize, offset)
|
||||||
|
|
||||||
rows, err := r.db.QueryxContext(ctx, r.db.Rebind(baseQuery), args...)
|
rows, err := r.db.QueryxContext(ctx, r.db.Rebind(baseQuery), args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
@ -380,16 +392,22 @@ func (r *ContestRepository) ListSolutions(ctx context.Context, filters models.So
|
||||||
var solution models.SolutionsListItem
|
var solution models.SolutionsListItem
|
||||||
err = rows.StructScan(&solution)
|
err = rows.StructScan(&solution)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
solutions = append(solutions, &solution)
|
solutions = append(solutions, &solution)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = rows.Err(); err != nil {
|
if err = rows.Err(); err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
return solutions, totalCount, nil
|
return &models.SolutionsList{
|
||||||
|
Solutions: solutions,
|
||||||
|
Pagination: models.Pagination{
|
||||||
|
Total: models.Total(totalCount, filter.PageSize),
|
||||||
|
Page: filter.Page,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -63,6 +63,7 @@ func (r *ProblemRepository) ReadProblemById(ctx context.Context, q tester.Querie
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &problem, nil
|
return &problem, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,18 +88,18 @@ LIMIT ? OFFSET ?`
|
||||||
CountProblemsQuery = "SELECT COUNT(*) FROM problems"
|
CountProblemsQuery = "SELECT COUNT(*) FROM problems"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *ProblemRepository) ListProblems(ctx context.Context, q tester.Querier, page int32, pageSize int32) ([]*models.ProblemsListItem, int32, error) {
|
func (r *ProblemRepository) ListProblems(ctx context.Context, q tester.Querier, filter models.ProblemsFilter) (*models.ProblemsList, error) {
|
||||||
const op = "ContestRepository.ListProblems"
|
const op = "ContestRepository.ListProblems"
|
||||||
|
|
||||||
if pageSize > 20 || pageSize < 1 {
|
if filter.PageSize > 20 || filter.PageSize < 1 {
|
||||||
pageSize = 1
|
filter.PageSize = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
var problems []*models.ProblemsListItem
|
var problems []*models.ProblemsListItem
|
||||||
query := q.Rebind(ListProblemsQuery)
|
query := q.Rebind(ListProblemsQuery)
|
||||||
err := q.SelectContext(ctx, &problems, query, pageSize, (page-1)*pageSize)
|
err := q.SelectContext(ctx, &problems, query, filter.PageSize, filter.Offset())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = q.Rebind(CountProblemsQuery)
|
query = q.Rebind(CountProblemsQuery)
|
||||||
|
@ -106,10 +107,16 @@ func (r *ProblemRepository) ListProblems(ctx context.Context, q tester.Querier,
|
||||||
var count int32
|
var count int32
|
||||||
err = q.GetContext(ctx, &count, query)
|
err = q.GetContext(ctx, &count, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, handlePgErr(err, op)
|
return nil, handlePgErr(err, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
return problems, count, nil
|
return &models.ProblemsList{
|
||||||
|
Problems: problems,
|
||||||
|
Pagination: models.Pagination{
|
||||||
|
Total: models.Total(count, filter.PageSize),
|
||||||
|
Page: filter.Page,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -46,16 +46,16 @@ func (uc *ContestUseCase) DeleteParticipant(ctx context.Context, participantId i
|
||||||
return uc.contestRepo.DeleteParticipant(ctx, participantId)
|
return uc.contestRepo.DeleteParticipant(ctx, participantId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ReadRichTasks(ctx context.Context, contestId int32) ([]*models.TasksListItem, error) {
|
func (uc *ContestUseCase) ReadTasks(ctx context.Context, contestId int32) ([]*models.TasksListItem, error) {
|
||||||
return uc.contestRepo.ReadRichTasks(ctx, contestId)
|
return uc.contestRepo.ReadTasks(ctx, contestId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ListContests(ctx context.Context, page int32, pageSize int32) ([]*models.ContestsListItem, int32, error) {
|
func (uc *ContestUseCase) ListContests(ctx context.Context, filter models.ContestsFilter) (*models.ContestsList, error) {
|
||||||
return uc.contestRepo.ListContests(ctx, page, pageSize)
|
return uc.contestRepo.ListContests(ctx, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ListParticipants(ctx context.Context, contestId int32, page int32, pageSize int32) ([]*models.ParticipantsListItem, int32, error) {
|
func (uc *ContestUseCase) ListParticipants(ctx context.Context, filter models.ParticipantsFilter) (*models.ParticipantsList, error) {
|
||||||
return uc.contestRepo.ListParticipants(ctx, contestId, page, pageSize)
|
return uc.contestRepo.ListParticipants(ctx, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) UpdateContest(ctx context.Context, id int32, contestUpdate models.ContestUpdate) error {
|
func (uc *ContestUseCase) UpdateContest(ctx context.Context, id int32, contestUpdate models.ContestUpdate) error {
|
||||||
|
@ -74,8 +74,8 @@ func (uc *ContestUseCase) CreateSolution(ctx context.Context, creation *models.S
|
||||||
return uc.contestRepo.CreateSolution(ctx, creation)
|
return uc.contestRepo.CreateSolution(ctx, creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ListSolutions(ctx context.Context, filters models.SolutionsFilter) ([]*models.SolutionsListItem, int32, error) {
|
func (uc *ContestUseCase) ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error) {
|
||||||
return uc.contestRepo.ListSolutions(ctx, filters)
|
return uc.contestRepo.ListSolutions(ctx, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ReadTask(ctx context.Context, id int32) (*models.Task, error) {
|
func (uc *ContestUseCase) ReadTask(ctx context.Context, id int32) (*models.Task, error) {
|
||||||
|
|
|
@ -38,8 +38,8 @@ func (u *ProblemUseCase) DeleteProblem(ctx context.Context, id int32) error {
|
||||||
return u.problemRepo.DeleteProblem(ctx, u.problemRepo.DB(), id)
|
return u.problemRepo.DeleteProblem(ctx, u.problemRepo.DB(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ProblemUseCase) ListProblems(ctx context.Context, page int32, pageSize int32) ([]*models.ProblemsListItem, int32, error) {
|
func (u *ProblemUseCase) ListProblems(ctx context.Context, filter models.ProblemsFilter) (*models.ProblemsList, error) {
|
||||||
return u.problemRepo.ListProblems(ctx, u.problemRepo.DB(), page, pageSize)
|
return u.problemRepo.ListProblems(ctx, u.problemRepo.DB(), filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpdate models.ProblemUpdate) error {
|
func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpdate models.ProblemUpdate) error {
|
||||||
|
|
Loading…
Add table
Reference in a new issue