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"); } }
[HttpDelete("{id}")] //удаление по id public async Task <IActionResult> Delte(Guid id) { try { return(Ok(await _playRepo.DeleteAsync(id))); } catch (Exception ex) { return(StatusCode(500, ex)); } }
public async Task <AffectedViewersDto> Handle(ChangeActivePlaylistCommand 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 newPlaylist = await _playlistRepository.GetAsync(request.PlaylistId, cancellationToken) ?? throw new ArgumentException("New playlist could not be found"); room.UpdateSelectedPlaylist(newPlaylist); if (room.IsPlaylistSelected) { var previousPlaylist = await _playlistRepository.GetAsync(room.ActivePlaylistId.Value, cancellationToken) ?? throw new InvalidOperationException( "Previous active playlist could not be found"); if (previousPlaylist.IsTemporary) { await _playlistRepository.DeleteAsync(previousPlaylist.Id, cancellationToken); } } 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 change active playlist in room {request.RoomId.ToString()}"); throw; } catch (Exception exception) { _logger.LogError(exception, $"Could not change active playlist in room {request.RoomId.ToString()}"); throw new InformativeException("Could not change active playlist in room. Please retry"); } }
public async Task <Unit> Handle(CloseRoomCommand 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"); if (room.Host.Profile.Id != request.MemberId) { throw new InformativeException("Member does not have permission"); } if (room.IsPlaylistSelected) { var activePlaylist = await _playlistRepository.GetAsync(room.ActivePlaylistId.Value, cancellationToken) ?? throw new ArgumentException("Active playlist could not be found"); if (activePlaylist.IsTemporary) { await _playlistRepository.DeleteAsync(activePlaylist.Id, cancellationToken); } } await _roomRepository.DeleteAsync(room.Id, cancellationToken); transaction.Complete(); return(Unit.Value); } catch (InformativeException exception) { _logger.LogError(exception, $"Could not close room {request.RoomId.ToString()}"); throw; } catch (Exception exception) { _logger.LogError(exception, $"Could not close room {request.RoomId.ToString()}"); throw new InformativeException("Could not close room. Please retry"); } }
public async Task <bool> DeletePlaylistAsync(int id, CancellationToken ct = default(CancellationToken)) { return(await _playlistRepository.DeleteAsync(id, ct)); }