ms-tester/internal/services/participants.go
2024-08-23 14:56:52 +05:00

58 lines
2.1 KiB
Go

package services
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models"
)
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)
}