public async Task <AffectedViewersDto> Handle(DeletePlaylistCommand request, CancellationToken cancellationToken) { try { using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var room = await _roomRepository.GetAsync(request.RoomId, cancellationToken) ?? throw new ArgumentException("Room could not be found"); var playlist = await _playlistRepository.GetAsync(room.ActivePlaylistId.Value, cancellationToken) ?? throw new ArgumentException("Playlist could not be found"); var viewer = await _viewerRepository.GetAsync(request.MemberId, cancellationToken) ?? throw new ArgumentException("Viewer could not be found"); if (!playlist.Owner.Equals(viewer)) { throw new InformativeException("Playlist does not belong to you"); } await _playlistRepository.DeleteAsync(playlist.Id, cancellationToken); var remainingPlaylists = await _playlistRepository.GetAllByViewerAsync(request.MemberId, cancellationToken); if (remainingPlaylists == null || remainingPlaylists.Count == 0) { var newPlaylistId = _identifierProvider.GenerateGuid(); var newPlaylist = new Playlist(newPlaylistId, "Temporary", viewer); await _playlistRepository.AddAsync(newPlaylist, cancellationToken); room.UpdateSelectedPlaylist(newPlaylist); } else { room.UpdateSelectedPlaylist(remainingPlaylists.First()); } await _roomRepository.UpdateAsync(room, cancellationToken); transaction.Complete(); var affectedViewers = room.Viewers.ToList(); affectedViewers.Add(room.Host); return(_mapper.Map <AffectedViewersDto>(affectedViewers)); } catch (InformativeException exception) { _logger.LogError(exception, $"Could not delete playlist {request.RoomId.ToString()} by member " + $"{request.MemberId.ToString()}"); throw; } catch (Exception exception) { _logger.LogError(exception, $"Could not delete playlist {request.RoomId.ToString()} by member " + $"{request.MemberId.ToString()}"); throw new InformativeException("Could not delete playlist. Please retry"); } }
public async Task <RoomDto> Handle(CreateRoomCommand request, CancellationToken cancellationToken) { try { using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var host = await _viewerRepository.GetAsync(request.HostId, cancellationToken) ?? throw new ArgumentException($"Host {request.HostId.ToString()} could not be found"); if (request.PlaylistId == default) { Guid generatedPlaylistId = _identifierProvider.GenerateGuid(); var playlist = new Playlist(generatedPlaylistId, "Temporary", host) { IsTemporary = true }; await _playlistRepository.AddAsync(playlist, cancellationToken); request.PlaylistId = generatedPlaylistId; } Guid generatedRoomId = _identifierProvider.GenerateGuid(); string invitationToken = _identifierProvider.GenerateToken(generatedRoomId); var room = new Room( generatedRoomId, request.Name, host, request.Type, new Invitation(invitationToken), request.PlaylistId, request.Description) { MaxViewersQuantity = request.MaxViewersQuantity }; await _roomRepository.AddAsync(room, cancellationToken); transaction.Complete(); return(_mapper.Map <RoomDto>(room)); } catch (InformativeException exception) { _logger.LogError(exception, $"Could not create room with name {request.Name} by user with ID " + $"{request.HostId.ToString()}"); throw; } catch (Exception exception) { _logger.LogError(exception, $"Could not create room with name {request.Name} by user with ID " + $"{request.HostId.ToString()}"); throw new InformativeException("Could not create room. Please retry"); } }
public async Task <Unit> Handle(SavePlaylistCommand request, CancellationToken cancellationToken) { try { using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var room = await _roomRepository.GetAsync(request.RoomId, cancellationToken) ?? throw new ArgumentException("Room could not be found"); var viewer = await _viewerRepository.GetAsync(request.ViewerId, cancellationToken) ?? throw new ArgumentException("Viewer could not be found"); if (!room.Viewers.Contains(viewer)) { throw new InvalidOperationException("Viewer does not belong to this room"); } var activePlaylist = await _playlistRepository.GetAsync(room.ActivePlaylistId.Value, cancellationToken) ?? throw new ArgumentException("Playlist could not be found"); if (activePlaylist.Owner == viewer) { if (!activePlaylist.IsTemporary) { throw new InvalidOperationException("Playlist is added already"); } activePlaylist.IsTemporary = false; } else { Guid newPlaylistId = _identifierProvider.GenerateGuid(); activePlaylist = CreatePlaylistCopy(newPlaylistId, viewer, activePlaylist); } await _playlistRepository.AddAsync(activePlaylist, cancellationToken); transaction.Complete(); return(Unit.Value); } catch (InformativeException exception) { _logger.LogError(exception, $"Could not add playlist {request.RoomId.ToString()} " + $"to host of room {request.RoomId.ToString()}"); throw; } catch (Exception exception) { _logger.LogError(exception, $"Could not add playlist {request.RoomId.ToString()} " + $"to host of room {request.RoomId.ToString()}"); throw new InformativeException("Could not add playlist. Please retry"); } }
public async Task RegisterAsync(Guid id, string name) { var authenticator = await _authenRepository.GetAsync(id); if (authenticator == null) { throw new Exception($"Authenticator with tag: '{id}' does not exists'"); } var playlist = new Playlist(name, id); await _playlistRepository.AddAsync(playlist); }
public async Task <PlaylistViewModel> AddPlaylistAsync(PlaylistViewModel newPlaylistViewModel, CancellationToken ct = default(CancellationToken)) { var playlist = new Playlist { Name = newPlaylistViewModel.Name }; playlist = await _playlistRepository.AddAsync(playlist, ct); newPlaylistViewModel.PlaylistId = playlist.PlaylistId; return(newPlaylistViewModel); }