feat:
This commit is contained in:
parent
4cdd751b16
commit
be25404852
51 changed files with 606 additions and 1194 deletions
|
@ -1,4 +1,13 @@
|
|||
package contests
|
||||
|
||||
import (
|
||||
"context"
|
||||
contestv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/contest/v1"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type ContestHandlers interface {
|
||||
CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error)
|
||||
ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error)
|
||||
DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error)
|
||||
}
|
||||
|
|
|
@ -1 +1,51 @@
|
|||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/contests"
|
||||
contestv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/contest/v1"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type ContestHandlers struct {
|
||||
contestv1.UnimplementedContestServiceServer
|
||||
|
||||
contestUC contests.ContestUseCase
|
||||
}
|
||||
|
||||
func NewContestHandlers(gserver *grpc.Server, contestUC contests.ContestUseCase) {
|
||||
handlers := &ContestHandlers{contestUC: contestUC}
|
||||
|
||||
contestv1.RegisterContestServiceServer(gserver, handlers)
|
||||
}
|
||||
|
||||
func (h *ContestHandlers) CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error) {
|
||||
id, err := h.contestUC.CreateContest(ctx, req.GetTitle())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contestv1.CreateContestResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
func (h *ContestHandlers) ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error) {
|
||||
contest, err := h.contestUC.ReadContestById(ctx, req.GetId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contestv1.ReadContestResponse{Contest: &contestv1.ReadContestResponse_Contest{
|
||||
Id: *contest.Id,
|
||||
Title: *contest.Title,
|
||||
CreatedAt: utils.TimestampP(contest.CreatedAt),
|
||||
UpdatedAt: utils.TimestampP(contest.UpdatedAt),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func (h *ContestHandlers) DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error) {
|
||||
err := h.contestUC.DeleteContest(ctx, req.GetId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
|
|
@ -2,12 +2,11 @@ package contests
|
|||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
)
|
||||
|
||||
type ContestRepository interface {
|
||||
CreateContest(ctx context.Context, contest *models.Contest) (int32, error)
|
||||
CreateContest(ctx context.Context, title string) (int32, error)
|
||||
ReadContestById(ctx context.Context, id int32) (*models.Contest, error)
|
||||
UpdateContest(ctx context.Context, contest *models.Contest) error
|
||||
DeleteContest(ctx context.Context, id int32) error
|
||||
}
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ParticipantStorage struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewParticipantStorage(db *sqlx.DB, logger *zap.Logger) *ParticipantStorage {
|
||||
return &ParticipantStorage{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (storage *ParticipantStorage) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) {
|
||||
query := storage.db.Rebind(`
|
||||
INSERT INTO participants
|
||||
(user_id,contest_id,name)
|
||||
VALUES (?,?,?)
|
||||
RETURNING id
|
||||
`)
|
||||
|
||||
rows, err := storage.db.QueryxContext(
|
||||
ctx,
|
||||
query,
|
||||
participant.UserId,
|
||||
participant.ContestId,
|
||||
participant.Name,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var id int32
|
||||
err = rows.StructScan(&id)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
|
||||
}
|
||||
|
||||
func (storage *ParticipantStorage) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) {
|
||||
var participant models.Participant
|
||||
query := storage.db.Rebind("SELECT * from participants WHERE id=? LIMIT 1")
|
||||
err := storage.db.GetContext(ctx, &participant, query, id)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err)
|
||||
}
|
||||
return &participant, nil
|
||||
}
|
||||
|
||||
func (storage *ParticipantStorage) UpdateParticipant(ctx context.Context, id int32, participant models.Participant) error {
|
||||
query := storage.db.Rebind("UPDATE participants SET name=?")
|
||||
_, err := storage.db.ExecContext(ctx, query, participant.Name)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (storage *ParticipantStorage) DeleteParticipant(ctx context.Context, id int32) error {
|
||||
query := storage.db.Rebind("DELETE FROM participants WHERE id=?")
|
||||
_, err := storage.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -3,7 +3,7 @@ package repository
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
@ -23,37 +23,32 @@ func NewContestRepository(db *sqlx.DB, logger *zap.Logger) *ContestRepository {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *ContestRepository) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
|
||||
query := r.db.Rebind(`
|
||||
INSERT INTO contests
|
||||
(name)
|
||||
VALUES (?)
|
||||
RETURNING id
|
||||
`)
|
||||
const createContestQuery = "INSERT INTO contest (title) VALUES (?) RETURNING id"
|
||||
|
||||
rows, err := r.db.QueryxContext(
|
||||
ctx,
|
||||
query,
|
||||
contest.Name,
|
||||
)
|
||||
func (r *ContestRepository) CreateContest(ctx context.Context, title string) (int32, error) {
|
||||
query := r.db.Rebind(createContestQuery)
|
||||
|
||||
rows, err := r.db.QueryxContext(ctx, query, title)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var id int32
|
||||
err = rows.StructScan(&id)
|
||||
rows.Next()
|
||||
err = rows.Scan(&id)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
|
||||
}
|
||||
|
||||
const readContestByIdQuery = "SELECT * from contest WHERE id=? LIMIT 1"
|
||||
|
||||
func (r *ContestRepository) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
|
||||
var contest models.Contest
|
||||
query := r.db.Rebind("SELECT * from contests WHERE id=? LIMIT 1")
|
||||
query := r.db.Rebind(readContestByIdQuery)
|
||||
err := r.db.GetContext(ctx, &contest, query, id)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err)
|
||||
|
@ -61,18 +56,10 @@ func (r *ContestRepository) ReadContestById(ctx context.Context, id int32) (*mod
|
|||
return &contest, nil
|
||||
}
|
||||
|
||||
func (r *ContestRepository) UpdateContest(ctx context.Context, contest *models.Contest) error {
|
||||
query := r.db.Rebind("UPDATE contests SET name=? WHERE id=?")
|
||||
_, err := r.db.ExecContext(ctx, query, contest.Name, contest.Id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
const deleteContestQuery = "DELETE FROM contest WHERE id=?"
|
||||
|
||||
func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error {
|
||||
query := r.db.Rebind("DELETE FROM contests WHERE id=?")
|
||||
query := r.db.Rebind(deleteContestQuery)
|
||||
_, err := r.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
|
|
58
internal/contests/repository/pg_repository_test.go
Normal file
58
internal/contests/repository/pg_repository_test.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContestRepository_CreateContest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
sqlxDB := sqlx.NewDb(db, "sqlmock")
|
||||
defer sqlxDB.Close()
|
||||
|
||||
contestRepo := NewContestRepository(sqlxDB, zap.NewNop())
|
||||
|
||||
t.Run("valid contest creation", func(t *testing.T) {
|
||||
title := "Contest title"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id"}).AddRow(1)
|
||||
|
||||
mock.ExpectQuery(sqlxDB.Rebind(createContestQuery)).WithArgs(title).WillReturnRows(rows)
|
||||
|
||||
id, err := contestRepo.CreateContest(context.Background(), title)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int32(1), id)
|
||||
})
|
||||
}
|
||||
|
||||
func TestContestRepository_DeleteContest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
sqlxDB := sqlx.NewDb(db, "sqlmock")
|
||||
defer sqlxDB.Close()
|
||||
|
||||
contestRepo := NewContestRepository(sqlxDB, zap.NewNop())
|
||||
|
||||
t.Run("valid contest deletion", func(t *testing.T) {
|
||||
id := int32(1)
|
||||
rows := sqlmock.NewResult(1, 1)
|
||||
|
||||
mock.ExpectExec(sqlxDB.Rebind(deleteContestQuery)).WithArgs(id).WillReturnResult(rows)
|
||||
|
||||
err = contestRepo.DeleteContest(context.Background(), id)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type SolutionStorage struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewSolutionStorage(db *sqlx.DB, logger *zap.Logger) *SolutionStorage {
|
||||
return &SolutionStorage{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: testing graph
|
||||
|
||||
func (storage *SolutionStorage) updateResult(ctx context.Context, participantId int32, taskId int32) error {
|
||||
tx, err := storage.db.Beginx()
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
query := tx.Rebind("UPDATE participant_subtask AS ps SET best_score = (SELECT COALESCE(max(score),0) FROM subtaskruns WHERE subtask_id = ps.subtask_id AND solution_id IN (SELECT id FROM solutions WHERE participant_id=ps.participant_id)) WHERE participant_id = 2 AND subtask_id IN (SELECT id FROM subtasks WHERE task_id = 2)")
|
||||
tx.QueryxContext(ctx, query, participantId, taskId)
|
||||
query = tx.Rebind("UPDATE participant_task SET best_score = (select max(best_score) from participant_subtask WHERE participant_id = ? AND subtask_id IN (SELECT id FROM subtask WHERE task_id = ?)) WHERE participant_id = ? AND task_id = ?")
|
||||
tx.QueryxContext(ctx, query, participantId, taskId, participantId, taskId)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (storage *SolutionStorage) CreateSolution(ctx context.Context, solution *models.Solution) (int32, error) {
|
||||
query := storage.db.Rebind(`
|
||||
INSERT INTO solutions
|
||||
(participant_id,task_id,language_id,solution_hash,result)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
RETURNING id
|
||||
`)
|
||||
|
||||
rows, err := storage.db.QueryxContext(
|
||||
ctx,
|
||||
query,
|
||||
solution.ParticipantId,
|
||||
solution.TaskId,
|
||||
solution.LanguageId,
|
||||
"", //FIXME
|
||||
models.NotTested,
|
||||
)
|
||||
//TODO: add testing tree
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var id int32
|
||||
err = rows.StructScan(&id)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (storage *SolutionStorage) ReadSolutionById(ctx context.Context, id int32) (*models.Solution, error) {
|
||||
var solution models.Solution
|
||||
query := storage.db.Rebind("SELECT * from solutions WHERE id=? LIMIT 1")
|
||||
err := storage.db.GetContext(ctx, &solution, query, id)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err)
|
||||
}
|
||||
return &solution, nil
|
||||
}
|
||||
|
||||
func (storage *SolutionStorage) RejudgeSolution(ctx context.Context, id int32) error {
|
||||
tx, err := storage.db.Beginx()
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
query := tx.Rebind("UPDATE solutions SET result = ? WHERE id = ?")
|
||||
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
|
||||
query = tx.Rebind("UPDATE subtaskruns SET result = ?,score = 0 WHERE solution_id = ?")
|
||||
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
|
||||
query = tx.Rebind("UPDATE testruns SET result = ?, score = 0 WHERE testgrouprun_id IN (SELECT id FROM tesgrouprun WHERE solution_id = ?)")
|
||||
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
|
||||
err = tx.Commit()
|
||||
var solution models.Solution
|
||||
query = storage.db.Rebind("SELECT * from solutions WHERE id=? LIMIT 1")
|
||||
err = storage.db.GetContext(ctx, &solution, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
storage.updateResult(ctx, *solution.ParticipantId, *solution.TaskId) // FIXME
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (storage *SolutionStorage) DeleteSolution(ctx context.Context, id int32) error {
|
||||
query := storage.db.Rebind("DELETE FROM solutions WHERE id=?")
|
||||
_, err := storage.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type TaskStorage struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewTaskStorage(db *sqlx.DB, logger *zap.Logger) *TaskStorage {
|
||||
return &TaskStorage{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (storage *TaskStorage) CreateTask(ctx context.Context, task *models.Task) (int32, error) {
|
||||
query := storage.db.Rebind(`
|
||||
INSERT INTO tasks
|
||||
(contest_id,problem_id,position,position_name)
|
||||
VALUES (?, ?, ?, ?)
|
||||
RETURNING id
|
||||
`)
|
||||
|
||||
rows, err := storage.db.QueryxContext(
|
||||
ctx,
|
||||
query,
|
||||
task.ContestId,
|
||||
task.ProblemId,
|
||||
task.Position,
|
||||
task.PositionName,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var id int32
|
||||
err = rows.StructScan(&id)
|
||||
if err != nil {
|
||||
return 0, handlePgErr(err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
|
||||
}
|
||||
|
||||
func (storage *TaskStorage) ReadTaskById(ctx context.Context, id int32) (*models.Task, error) {
|
||||
var task models.Task
|
||||
query := storage.db.Rebind("SELECT * from tasks WHERE id=? LIMIT 1")
|
||||
err := storage.db.GetContext(ctx, &task, query, id)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err)
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
func (storage *TaskStorage) UpdateTask(ctx context.Context, id int32, task models.Task) error {
|
||||
query := storage.db.Rebind("UPDATE tasks SET position=?,position_name=?")
|
||||
_, err := storage.db.ExecContext(ctx, query, task.Position, task.PositionName)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (storage *TaskStorage) DeleteTask(ctx context.Context, id int32) error {
|
||||
query := storage.db.Rebind("DELETE FROM tasks WHERE id=?")
|
||||
_, err := storage.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -2,12 +2,11 @@ package contests
|
|||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
)
|
||||
|
||||
type ContestUseCase interface {
|
||||
CreateContest(ctx context.Context, contest *models.Contest) (int32, error)
|
||||
CreateContest(ctx context.Context, title string) (int32, error)
|
||||
ReadContestById(ctx context.Context, id int32) (*models.Contest, error)
|
||||
UpdateContest(ctx context.Context, contest *models.Contest) error
|
||||
DeleteContest(ctx context.Context, id int32) error
|
||||
}
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
)
|
||||
|
||||
type ParticipantStorage interface {
|
||||
CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error)
|
||||
ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error)
|
||||
UpdateParticipant(ctx context.Context, participant *models.Participant) error
|
||||
DeleteParticipant(ctx context.Context, id int32) error
|
||||
}
|
||||
|
||||
type ParticipantService struct {
|
||||
participantStorage ParticipantStorage
|
||||
permissionService IPermissionService
|
||||
}
|
||||
|
||||
func NewParticipantService(
|
||||
participantStorage ParticipantStorage,
|
||||
permissionService IPermissionService,
|
||||
) *ParticipantService {
|
||||
return &ParticipantService{
|
||||
participantStorage: participantStorage,
|
||||
permissionService: permissionService,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
||||
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.participantStorage.CreateParticipant(ctx, participant)
|
||||
}
|
||||
|
||||
func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
|
||||
return nil, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.participantStorage.ReadParticipantById(ctx, id)
|
||||
}
|
||||
|
||||
func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.participantStorage.UpdateParticipant(ctx, participant)
|
||||
}
|
||||
|
||||
func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.participantStorage.DeleteParticipant(ctx, id)
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"github.com/open-policy-agent/opa/rego"
|
||||
)
|
||||
|
||||
type PermissionService struct {
|
||||
query *rego.PreparedEvalQuery
|
||||
}
|
||||
|
||||
func NewPermissionService() *PermissionService {
|
||||
query, err := rego.New(
|
||||
rego.Query("allow = data.problem.rbac.allow"),
|
||||
rego.Load([]string{"./opa/all.rego"}, nil),
|
||||
).PrepareForEval(context.TODO())
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &PermissionService{
|
||||
query: &query,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PermissionService) Allowed(ctx context.Context, user *models.User, action string) bool {
|
||||
input := map[string]interface{}{
|
||||
"user": user,
|
||||
"action": action,
|
||||
}
|
||||
|
||||
result, err := s.query.Eval(ctx, rego.EvalInput(input))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return result[0].Bindings["allow"].(bool)
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
)
|
||||
|
||||
type SolutionStorage interface {
|
||||
CreateSolution(ctx context.Context, solution models.Solution) (int32, error)
|
||||
ReadSolutionById(ctx context.Context, id int32) (models.Solution, error)
|
||||
RejudgeSolution(ctx context.Context, id int32) error
|
||||
DeleteSolution(ctx context.Context, id int32) error
|
||||
}
|
||||
|
||||
type SolutionService struct {
|
||||
solutionStorage SolutionStorage
|
||||
permissionService IPermissionService
|
||||
}
|
||||
|
||||
func NewSolutionService(
|
||||
solutionStorage SolutionStorage,
|
||||
permissionService IPermissionService,
|
||||
) *SolutionService {
|
||||
return &SolutionService{
|
||||
solutionStorage: solutionStorage,
|
||||
permissionService: permissionService,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
||||
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.solutionStorage.CreateSolution(ctx, solution)
|
||||
}
|
||||
|
||||
func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
|
||||
return models.Solution{}, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.solutionStorage.ReadSolutionById(ctx, id)
|
||||
}
|
||||
|
||||
func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.solutionStorage.RejudgeSolution(ctx, id)
|
||||
}
|
||||
|
||||
func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.solutionStorage.DeleteSolution(ctx, id)
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
)
|
||||
|
||||
type TaskStorage interface {
|
||||
CreateTask(ctx context.Context, task models.Task) (int32, error)
|
||||
DeleteTask(ctx context.Context, id int32) error
|
||||
}
|
||||
|
||||
type TaskService struct {
|
||||
taskStorage TaskStorage
|
||||
permissionService IPermissionService
|
||||
}
|
||||
|
||||
func NewTaskService(
|
||||
taskStorage TaskStorage,
|
||||
permissionService IPermissionService,
|
||||
) *TaskService {
|
||||
return &TaskService{
|
||||
taskStorage: taskStorage,
|
||||
permissionService: permissionService,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
||||
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.taskStorage.CreateTask(ctx, task)
|
||||
}
|
||||
|
||||
func (service *TaskService) DeleteTask(ctx context.Context, id int32) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.taskStorage.DeleteTask(ctx, id)
|
||||
}
|
|
@ -4,48 +4,28 @@ import (
|
|||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/contests"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
)
|
||||
|
||||
type ContestService struct {
|
||||
contestStorage contests.ContestRepository
|
||||
permissionService IPermissionService
|
||||
type ContestUseCase struct {
|
||||
contestRepo contests.ContestRepository
|
||||
}
|
||||
|
||||
func NewContestService(
|
||||
contestStorage ContestStorage,
|
||||
permissionService IPermissionService,
|
||||
) *ContestService {
|
||||
return &ContestService{
|
||||
contestStorage: contestStorage,
|
||||
permissionService: permissionService,
|
||||
func NewContestUseCase(
|
||||
contestRepo contests.ContestRepository,
|
||||
) *ContestUseCase {
|
||||
return &ContestUseCase{
|
||||
contestRepo: contestRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
||||
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.CreateContest(ctx, contest)
|
||||
func (uc *ContestUseCase) CreateContest(ctx context.Context, title string) (int32, error) {
|
||||
return uc.contestRepo.CreateContest(ctx, title)
|
||||
}
|
||||
|
||||
func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
|
||||
return nil, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.ReadContestById(ctx, id)
|
||||
func (uc *ContestUseCase) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
|
||||
return uc.contestRepo.ReadContestById(ctx, id)
|
||||
}
|
||||
|
||||
func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.UpdateContest(ctx, contest)
|
||||
}
|
||||
|
||||
func (service *ContestService) DeleteContest(ctx context.Context, id int32) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
||||
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.DeleteContest(ctx, id)
|
||||
func (uc *ContestUseCase) DeleteContest(ctx context.Context, id int32) error {
|
||||
return uc.contestRepo.DeleteContest(ctx, id)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue