2024-10-09 18:55:16 +00:00
|
|
|
package usecase
|
2024-08-16 16:50:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-05 16:05:29 +00:00
|
|
|
"git.sch9.ru/new_gate/models"
|
2024-10-09 18:55:16 +00:00
|
|
|
"git.sch9.ru/new_gate/ms-tester/internal/contests"
|
2024-10-09 19:04:37 +00:00
|
|
|
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
2024-08-16 16:50:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContestService struct {
|
2024-10-09 18:55:16 +00:00
|
|
|
contestStorage contests.ContestRepository
|
2024-08-23 09:56:52 +00:00
|
|
|
permissionService IPermissionService
|
2024-08-16 16:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContestService(
|
|
|
|
contestStorage ContestStorage,
|
2024-08-23 09:56:52 +00:00
|
|
|
permissionService IPermissionService,
|
2024-08-16 16:50:47 +00:00
|
|
|
) *ContestService {
|
|
|
|
return &ContestService{
|
2024-08-23 09:56:52 +00:00
|
|
|
contestStorage: contestStorage,
|
|
|
|
permissionService: permissionService,
|
2024-08-16 16:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
2024-10-09 19:04:37 +00:00
|
|
|
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-16 16:50:47 +00:00
|
|
|
return service.contestStorage.CreateContest(ctx, contest)
|
|
|
|
}
|
|
|
|
|
2024-08-16 17:04:49 +00:00
|
|
|
func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
|
2024-10-09 19:04:37 +00:00
|
|
|
return nil, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-16 17:04:49 +00:00
|
|
|
return service.contestStorage.ReadContestById(ctx, id)
|
2024-08-16 16:50:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 17:04:49 +00:00
|
|
|
func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
|
2024-10-09 19:04:37 +00:00
|
|
|
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-16 16:50:47 +00:00
|
|
|
return service.contestStorage.UpdateContest(ctx, contest)
|
|
|
|
}
|
|
|
|
|
2024-08-16 17:04:49 +00:00
|
|
|
func (service *ContestService) DeleteContest(ctx context.Context, id int32) error {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
2024-10-09 19:04:37 +00:00
|
|
|
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-16 17:04:49 +00:00
|
|
|
return service.contestStorage.DeleteContest(ctx, id)
|
2024-08-16 16:50:47 +00:00
|
|
|
}
|