/
EventManager.cs
111 lines (101 loc) · 3.65 KB
/
EventManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Threading.Tasks;
using EventHub.Countries;
using EventHub.Events.Registrations;
using EventHub.Organizations;
using EventHub.Organizations.Plans;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
namespace EventHub.Events
{
public class EventManager : DomainService
{
private readonly EventUrlCodeGenerator _eventUrlCodeGenerator;
private readonly IRepository<EventRegistration, Guid> _eventRegistrationRepository;
private readonly IRepository<Country, Guid> _countriesRepository;
private readonly PlanFeatureManager _planFeatureManager;
public EventManager(
EventUrlCodeGenerator eventUrlCodeGenerator,
IRepository<EventRegistration, Guid> eventRegistrationRepository,
IRepository<Country, Guid> countriesRepository,
PlanFeatureManager planFeatureManager)
{
_eventUrlCodeGenerator = eventUrlCodeGenerator;
_eventRegistrationRepository = eventRegistrationRepository;
_countriesRepository = countriesRepository;
_planFeatureManager = planFeatureManager;
}
public async Task<Event> CreateAsync(
Organization organization,
string title,
DateTime startTime,
DateTime endTime,
string description)
{
if (!await _planFeatureManager.CanCreateNewEventAsync(organization.Id))
{
throw new BusinessException(EventHubErrorCodes.CannotCreateNewEvent);
}
return new Event(
GuidGenerator.Create(),
organization.Id,
await _eventUrlCodeGenerator.GenerateAsync(),
title,
startTime,
endTime,
description
);
}
public async Task AddTrackAsync(Event @event, string name)
{
if (!await _planFeatureManager.CanAddNewTrackAsync(@event))
{
throw new BusinessException(EventHubErrorCodes.CannotAddNewTrack);
}
@event.AddTrack(
GuidGenerator.Create(),
name
);
}
public async Task SetCapacityAsync(
Event @event,
int? capacity)
{
if (capacity.HasValue)
{
var registeredUserCount = await _eventRegistrationRepository.CountAsync(x => x.EventId == @event.Id);
if (capacity.Value < registeredUserCount)
{
throw new BusinessException(EventHubErrorCodes.CapacityCanNotBeLowerThanRegisteredUserCount);
}
}
@event.Capacity = capacity;
}
public async Task SetLocationAsync(
Event @event,
bool isOnline,
string onlineLink,
Guid? countryId,
string city)
{
@event.IsOnline = isOnline;
if (@event.IsOnline)
{
@event.OnlineLink = onlineLink;
@event.CountryId = null;
@event.CountryName = null;
@event.City = null;
}
else
{
Check.NotNull(countryId, nameof(countryId));
Check.NotNull(city, nameof(city));
@event.OnlineLink = null;
@event.CountryId = countryId;
@event.CountryName = (await _countriesRepository.GetAsync(@event.CountryId!.Value)).Name;
@event.City = city;
}
}
}
}