refactor:
This commit is contained in:
parent
81e75e5a9c
commit
d62ae666d5
57 changed files with 656 additions and 310 deletions
4
internal/contests/delivery.go
Normal file
4
internal/contests/delivery.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package contests
|
||||
|
||||
type ContestHandlers interface {
|
||||
}
|
1
internal/contests/delivery/grpc/handlers.go
Normal file
1
internal/contests/delivery/grpc/handlers.go
Normal file
|
@ -0,0 +1 @@
|
|||
package grpc
|
13
internal/contests/pg_repository.go
Normal file
13
internal/contests/pg_repository.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package contests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
)
|
||||
|
||||
type ContestRepository interface {
|
||||
CreateContest(ctx context.Context, contest *models.Contest) (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
|
||||
}
|
79
internal/contests/repository/participants.go
Normal file
79
internal/contests/repository/participants.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
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
|
||||
}
|
97
internal/contests/repository/pg_repository.go
Normal file
97
internal/contests/repository/pg_repository.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ContestRepository struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewContestRepository(db *sqlx.DB, logger *zap.Logger) *ContestRepository {
|
||||
return &ContestRepository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ContestRepository) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
|
||||
query := r.db.Rebind(`
|
||||
INSERT INTO contests
|
||||
(name)
|
||||
VALUES (?)
|
||||
RETURNING id
|
||||
`)
|
||||
|
||||
rows, err := r.db.QueryxContext(
|
||||
ctx,
|
||||
query,
|
||||
contest.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 (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")
|
||||
err := r.db.GetContext(ctx, &contest, query, id)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error {
|
||||
query := r.db.Rebind("DELETE FROM contests WHERE id=?")
|
||||
_, err := r.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return handlePgErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handlePgErr(err error) error {
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) {
|
||||
return utils.StorageError(err, utils.ErrUnknown, "unexpected error from postgres")
|
||||
}
|
||||
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
|
||||
// TODO: probably should specify which constraint
|
||||
return utils.StorageError(err, utils.ErrConflict, pgErr.Message)
|
||||
}
|
||||
if pgerrcode.IsNoData(pgErr.Code) {
|
||||
return utils.StorageError(err, utils.ErrNotFound, pgErr.Message)
|
||||
}
|
||||
return utils.StorageError(err, utils.ErrUnimplemented, "unimplemented error")
|
||||
}
|
114
internal/contests/repository/solution.go
Normal file
114
internal/contests/repository/solution.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
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
|
||||
}
|
80
internal/contests/repository/task.go
Normal file
80
internal/contests/repository/task.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
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
|
||||
}
|
13
internal/contests/usecase.go
Normal file
13
internal/contests/usecase.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package contests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
)
|
||||
|
||||
type ContestUseCase interface {
|
||||
CreateContest(ctx context.Context, contest *models.Contest) (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
|
||||
}
|
44
internal/contests/usecase/all.rego
Normal file
44
internal/contests/usecase/all.rego
Normal file
|
@ -0,0 +1,44 @@
|
|||
package problem.rbac
|
||||
|
||||
import rego.v1
|
||||
|
||||
spectator := 0
|
||||
participant := 1
|
||||
moderator := 2
|
||||
admin := 3
|
||||
|
||||
permissions := {
|
||||
"read": is_spectator,
|
||||
"participate": is_participant,
|
||||
"update": is_moderator,
|
||||
"create": is_moderator,
|
||||
"delete": is_moderator,
|
||||
}
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if is_admin
|
||||
|
||||
allow if {
|
||||
permissions[input.action]
|
||||
}
|
||||
|
||||
default is_admin := false
|
||||
is_admin if {
|
||||
input.user.role == admin
|
||||
}
|
||||
|
||||
default is_moderator := false
|
||||
is_moderator if {
|
||||
input.user.role >= moderator
|
||||
}
|
||||
|
||||
default is_participant := false
|
||||
is_participant if {
|
||||
input.user.role >= participant
|
||||
}
|
||||
|
||||
default is_spectator := true
|
||||
is_spectator if {
|
||||
input.user.role >= spectator
|
||||
}
|
57
internal/contests/usecase/participants.go
Normal file
57
internal/contests/usecase/participants.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/lib"
|
||||
)
|
||||
|
||||
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, lib.ServiceError(nil, lib.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, lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.participantStorage.DeleteParticipant(ctx, id)
|
||||
}
|
39
internal/contests/usecase/permission.go
Normal file
39
internal/contests/usecase/permission.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package services
|
||||
|
||||
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)
|
||||
}
|
57
internal/contests/usecase/solution.go
Normal file
57
internal/contests/usecase/solution.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/lib"
|
||||
)
|
||||
|
||||
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, lib.ServiceError(nil, lib.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{}, lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.solutionStorage.DeleteSolution(ctx, id)
|
||||
}
|
41
internal/contests/usecase/task.go
Normal file
41
internal/contests/usecase/task.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
}
|
50
internal/contests/usecase/usecase.go
Normal file
50
internal/contests/usecase/usecase.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.sch9.ru/new_gate/models"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/contests"
|
||||
)
|
||||
|
||||
type ContestService struct {
|
||||
contestStorage contests.ContestRepository
|
||||
permissionService IPermissionService
|
||||
}
|
||||
|
||||
func NewContestService(
|
||||
contestStorage ContestStorage,
|
||||
permissionService IPermissionService,
|
||||
) *ContestService {
|
||||
return &ContestService{
|
||||
contestStorage: contestStorage,
|
||||
permissionService: permissionService,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
||||
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.CreateContest(ctx, contest)
|
||||
}
|
||||
|
||||
func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
|
||||
return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.ReadContestById(ctx, id)
|
||||
}
|
||||
|
||||
func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error {
|
||||
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
|
||||
return lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
||||
}
|
||||
return service.contestStorage.DeleteContest(ctx, id)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue