58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
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)
|
|
}
|