ms-tester/internal/services/participants.go

58 lines
2.1 KiB
Go
Raw Normal View History

2024-08-18 18:03:24 +00:00
package services
import (
"context"
2024-08-23 09:56:52 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/lib"
2024-08-25 16:52:00 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/models"
2024-08-18 18:03:24 +00:00
)
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
2024-08-23 09:56:52 +00:00
permissionService IPermissionService
2024-08-18 18:03:24 +00:00
}
func NewParticipantService(
participantStorage ParticipantStorage,
2024-08-23 09:56:52 +00:00
permissionService IPermissionService,
2024-08-18 18:03:24 +00:00
) *ParticipantService {
return &ParticipantService{
participantStorage: participantStorage,
2024-08-23 09:56:52 +00:00
permissionService: permissionService,
2024-08-18 18:03:24 +00:00
}
}
func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-18 18:03:24 +00:00
return service.participantStorage.CreateParticipant(ctx, participant)
}
func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-18 18:03:24 +00:00
return service.participantStorage.ReadParticipantById(ctx, id)
}
func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-18 18:03:24 +00:00
return service.participantStorage.UpdateParticipant(ctx, participant)
}
func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-18 18:03:24 +00:00
return service.participantStorage.DeleteParticipant(ctx, id)
}