-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCreateRoomCommandHandler.cs
More file actions
53 lines (39 loc) · 1.54 KB
/
CreateRoomCommandHandler.cs
File metadata and controls
53 lines (39 loc) · 1.54 KB
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
using DomeGym.Application.Common.Interfaces;
using DomeGym.Domain.RoomAggregate;
using ErrorOr;
using MediatR;
namespace DomeGym.Application.Rooms.Commands.CreateRoom;
public class CreateRoomCommandHandler : IRequestHandler<CreateRoomCommand, ErrorOr<Room>>
{
private readonly ISubscriptionsRepository _subscriptionsRepository;
private readonly IGymsRepository _gymsRepository;
public CreateRoomCommandHandler(ISubscriptionsRepository subscriptionsRepository, IGymsRepository gymsRepository)
{
_subscriptionsRepository = subscriptionsRepository;
_gymsRepository = gymsRepository;
}
public async Task<ErrorOr<Room>> Handle(CreateRoomCommand command, CancellationToken cancellationToken)
{
var gym = await _gymsRepository.GetByIdAsync(command.GymId);
if (gym is null)
{
return Error.NotFound(description: "Gym not found");
}
var subscription = await _subscriptionsRepository.GetByIdAsync(gym.SubscriptionId);
if (subscription is null)
{
return Error.Unexpected(description: "Subscription not found");
}
var room = new Room(
name: command.RoomName,
maxDailySessions: subscription.GetMaxDailySessions(),
gymId: gym.Id);
var addRoomResult = gym.AddRoom(room);
if (addRoomResult.IsError)
{
return addRoomResult.Errors;
}
await _gymsRepository.UpdateAsync(gym);
return room;
}
}