private async Task EnsureCurrentUserCanChangeRequestStatusAsync(LobbyJoiningRequest lobbyJoiningRequest) { await using var mnpContext = _contextFactory.Create(); var currentUserId = await _userService.GetCurrentUserIdAsync(); switch (lobbyJoiningRequest.RequestInitiator) { case RequestInitiator.User: { var isCurrentUserLobbyPlayer = await mnpContext.LobbyJoiningRequests.AnyAsync(r => r.Lobby.Id == lobbyJoiningRequest.LobbyId && r.Lobby.LobbyPlayers.Select(p => p.PlayerId).Contains(currentUserId)); if (!isCurrentUserLobbyPlayer) { throw new NoAccessException($"User {currentUserId} can't change lobby request status"); } break; } case RequestInitiator.Lobby: { if (lobbyJoiningRequest.UserId != currentUserId) { throw new NoAccessException($"User {currentUserId} can't change lobby request status"); } break; } } }
public async Task AddJoiningRequestAsync(LobbyJoiningRequest lobbyJoiningRequest) { await EnsureRequestInitializedByCurrentUserAsync(lobbyJoiningRequest); await _mnpContext.LobbyJoiningRequests.AddAsync(lobbyJoiningRequest); await _mnpContext.SaveChangesAsync(); }
private async Task EnsureRequestInitializedByCurrentUserAsync(LobbyJoiningRequest lobbyJoiningRequest) { var currentUserId = await _userService.GetCurrentUserIdAsync(); if (lobbyJoiningRequest.UserId != currentUserId) { throw new NoAccessException($"User {currentUserId} can't add joining request with other user id."); } }
public async Task AddJoiningRequestAsync(LobbyJoiningRequest lobbyJoiningRequest) { await using var mnpContext = _contextFactory.Create(); lobbyJoiningRequest.InitialDate = DateTime.Now; lobbyJoiningRequest.RequestStatus = RequestStatus.Initial; if (lobbyJoiningRequest.RequestInitiator == RequestInitiator.User) { lobbyJoiningRequest.UserId = await _userService.GetCurrentUserIdAsync(); } await mnpContext.LobbyJoiningRequests.AddAsync(lobbyJoiningRequest); await mnpContext.SaveChangesAsync(); }