-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCreateSessionCommandHandler.cs
More file actions
70 lines (54 loc) · 3.19 KB
/
CreateSessionCommandHandler.cs
File metadata and controls
70 lines (54 loc) · 3.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using ByteSync.Common.Business.Sessions.Cloud.Connections;
using ByteSync.Common.Helpers;
using ByteSync.ServerCommon.Business.Sessions;
using ByteSync.ServerCommon.Interfaces.Repositories;
using ByteSync.ServerCommon.Interfaces.Services;
using ByteSync.ServerCommon.Interfaces.Services.Clients;
using MediatR;
using Microsoft.Extensions.Logging;
namespace ByteSync.ServerCommon.Commands.CloudSessions;
public class CreateSessionCommandHandler : IRequestHandler<CreateSessionRequest, CloudSessionResult>
{
private readonly ICloudSessionsRepository _cloudSessionsRepository;
private readonly IClientsGroupsService _clientsGroupsService;
private readonly ICloudSessionsService _cloudSessionsService;
private readonly IRedisInfrastructureService _redisInfrastructureService;
private readonly ILogger<CreateSessionCommandHandler> _logger;
public CreateSessionCommandHandler(ICloudSessionsRepository cloudSessionsRepository, IClientsGroupsService clientsGroupsService,
IClientsRepository clientsRepository, ICloudSessionsService cloudSessionsService, IRedisInfrastructureService redisInfrastructureService,
ILogger<CreateSessionCommandHandler> logger)
{
_cloudSessionsRepository = cloudSessionsRepository;
_clientsGroupsService = clientsGroupsService;
_cloudSessionsService = cloudSessionsService;
_redisInfrastructureService = redisInfrastructureService;
_logger = logger;
}
public async Task<CloudSessionResult> Handle(CreateSessionRequest request, CancellationToken cancellationToken)
{
var createCloudSessionParameters = request.CreateCloudSessionParameters;
var client = request.Client;
var transaction = _redisInfrastructureService.OpenTransaction();
CloudSessionData cloudSessionData;
SessionMemberData creatorData;
cloudSessionData = new CloudSessionData(createCloudSessionParameters.LobbyId, createCloudSessionParameters.SessionSettings, client);
creatorData = new SessionMemberData(client, createCloudSessionParameters.CreatorPublicKeyInfo,
createCloudSessionParameters.CreatorProfileClientId, cloudSessionData,
createCloudSessionParameters.CreatorPrivateData);
cloudSessionData.SessionMembers.Add(creatorData);
cloudSessionData = await _cloudSessionsRepository.AddCloudSession(cloudSessionData, GenerateRandomSessionId, transaction);
await _clientsGroupsService.AddSessionSubscription(client, cloudSessionData.SessionId, transaction);
await transaction.ExecuteAsync();
await _clientsGroupsService.AddToSessionGroup(client, cloudSessionData.SessionId);
_logger.LogInformation("Cloud Session {SessionId} created", cloudSessionData.SessionId);
var cloudSessionResult = await _cloudSessionsService.BuildCloudSessionResult(cloudSessionData, creatorData);
return cloudSessionResult;
}
private string GenerateRandomSessionId()
{
string sessionId = RandomUtils.GetRandomNumber(3) +
RandomUtils.GetRandomLetters(3, false) +
RandomUtils.GetRandomNumber(3);
return sessionId;
}
}