Example #1
0
        public async Task <PlaylistDto> Handle(GetPlaylistQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var room = await _roomRepository.GetAsync(request.RoomId, cancellationToken)
                           ?? throw new InformativeException("Room could not be found");

                if (!room.IsPlaylistSelected)
                {
                    throw new InformativeException("There is no playlist in the room");
                }

                var playlist = await _playlistRepository.GetAsync(room.ActivePlaylistId.Value, cancellationToken);

                var availablePlaylists = await _playlistRepository.GetAllByViewerAsync(
                    room.Host.Profile.Id, cancellationToken);

                var dto = _mapper.Map <PlaylistDto>(playlist);
                dto.AvailablePlaylists = _mapper.Map <IList <SelectablePlaylistDto> >(availablePlaylists);
                return(dto);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Could not get playlist {request.RoomId}");
                throw new InformativeException("Could not get playlist. Please retry");
            }
        }
        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 <IList <SelectablePlaylistDto> > Handle(GetAvailablePlaylistsQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var host = await _viewerRepository.GetAsync(request.HostId, cancellationToken)
                           ?? throw new ArgumentException("Room could not be found");

                var availablePlaylists =
                    await _playlistRepository.GetAllByViewerAsync(host.Profile.Id, cancellationToken);

                return(_mapper.Map <IList <SelectablePlaylistDto> >(availablePlaylists));
            }
            catch (InformativeException exception)
            {
                _logger.LogError(exception, $"Could not get available playlists for room {request.HostId.ToString()}");
                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Could not get available playlists for room {request.HostId.ToString()}");
                throw new InformativeException("Could not get available playlists. Please retry");
            }
        }