示例#1
0
        public async Task ShouldReturnSuccessWhenCommandIsValid()
        {
            Guid administratorId       = Guid.NewGuid();
            var  fakeUserRepository    = new Mock <IUserRepository>();
            var  fakeChannelRepository = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new GetChannelByIdQueryResult
            {
                Id              = id,
                Name            = "channelOne",
                Description     = "The first channel",
                AdministratorId = administratorId
            });
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.NewGuid(),
                AdministratorId = administratorId
            };

            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeTrue();
            result.Errors.Should().BeNullOrEmpty();
            handler.Valid.Should().BeTrue();
            fakeChannelRepository.Verify(
                repository => repository.DeleteChannel(It.Is <Guid>(id => id == command.ChannelId)),
                Times.Once());
        }
示例#2
0
        public async Task ShouldReturnErrorWhenAdministratorIdDoesNotMatch()
        {
            Guid adminstratorId         = Guid.NewGuid();
            Guid commandAdministratorId = Guid.NewGuid();
            var  fakeUserRepository     = new Mock <IUserRepository>();
            var  fakeChannelRepository  = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new GetChannelByIdQueryResult
            {
                Id              = id,
                Name            = "channelOne",
                Description     = "The first channel",
                AdministratorId = adminstratorId
            });
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.NewGuid(),
                AdministratorId = commandAdministratorId
            };

            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
        public async Task <ActionResult> DeleteChannel([FromRoute] Guid id,
                                                       [FromHeader] Guid authorization,
                                                       [FromServices] IHandler <DeleteChannelCommand> handler)
        {
            var command = new DeleteChannelCommand
            {
                ChannelId       = id,
                AdministratorId = authorization
            };
            ICommandResult result = await handler.HandleAsync(command);

            if (!result.Success)
            {
                var channelHandler = handler as ChannelHandler;
                if (channelHandler.Notifications.Any(notification => notification.Message == "Channel not found"))
                {
                    return(NotFound(new ErrorViewModel(result)));
                }
                if (channelHandler.Notifications
                    .Any(notification => notification.Message == "AdministratorId does't match channel administrator id"))
                {
                    return(Unauthorized());
                }
                return(BadRequest(new ErrorViewModel(result)));
            }
            return(NoContent());
        }
示例#4
0
        public async Task WhenDeletingChannel_ItShouldIssueDeleteChannelCommand()
        {
            var command = new DeleteChannelCommand(Requester, ChannelId);

            this.requesterContext.Setup(_ => _.GetRequesterAsync()).ReturnsAsync(Requester);
            this.deleteChannel.Setup(v => v.HandleAsync(command)).Returns(Task.FromResult(0)).Verifiable();

            var result = await this.target.DeleteChannelAsync(ChannelId.Value.EncodeGuid());

            Assert.IsInstanceOfType(result, typeof(OkResult));
            this.updateChannel.Verify();
        }
        public void ShouldReturnSuccessWhenChannelIdAndAdministratorIdIsValid()
        {
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.NewGuid(),
                AdministratorId = Guid.NewGuid()
            };

            command.Validate();

            command.Valid.Should().BeTrue();
        }
        public void ShouldReturnErrorWhenChannelIdAndAdministratorIdIsEmpty()
        {
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.Empty,
                AdministratorId = Guid.Empty
            };

            command.Validate();

            command.Invalid.Should().BeTrue();
        }
示例#7
0
        public async Task ShouldReturnErrorWhenChannelIdAndAdministratorIdIsEmpty()
        {
            var fakeUserRepository    = new Mock <IUserRepository>();
            var fakeChannelRepository = new Mock <IChannelRepository>();
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.Empty,
                AdministratorId = Guid.Empty
            };

            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
示例#8
0
        public async Task ShouldReturnErrorWhenChannelNotFound()
        {
            var fakeUserRepository    = new Mock <IUserRepository>();
            var fakeChannelRepository = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .Returns(Task.FromResult <GetChannelByIdQueryResult>(null));
            var command = new DeleteChannelCommand
            {
                ChannelId       = Guid.NewGuid(),
                AdministratorId = Guid.NewGuid()
            };

            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
        public async Task <ICommandResult> HandleAsync(DeleteChannelCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Could not delete this channel", Errors));
            }
            GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.ChannelId);

            if (channel is null)
            {
                AddNotification(nameof(command.ChannelId), "Channel not found");
                return(new CommandResult(false, "Could not delete this channel", Errors));
            }
            if (channel.AdministratorId != command.AdministratorId)
            {
                AddNotification(nameof(command.AdministratorId), "AdministratorId does't match channel administrator id");
                return(new CommandResult(false, "User does't have permission to delete this channel", Errors));
            }
            await _channelRepository.DeleteChannel(channel.Id);

            return(new CommandResult(true, "Channel successfully deleted", null, null));
        }
示例#10
0
        public PodcastViewModel(IScreen hostScreen,
                                IPlayerModel player,
                                IEventAggregator eventAggregator,
                                IDialogService dialogService,
                                IFeedParser feedParser,
                                IChannelStore channelStore,
                                IPlaylistStore playlistStore)
            : base(eventAggregator)
        {
            HostScreen    = hostScreen;
            Player        = player;
            DialogService = dialogService;
            FeedParser    = feedParser;

            this.channelStore  = channelStore;
            this.playlistStore = playlistStore;

            GetChannelsCommand = ReactiveCommand.CreateFromTask(async _ => await GetChannels());
            GetChannelsCommand.IsExecuting.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => OnChannelsBusy(GetChannelsCommand.GetHashCode(), _));

            PlayItemCommand = ReactiveCommand.CreateFromTask <IPlayable>(async _ => await OnPlayEpisode(_ as Episode));

            // open url
            OpenUrlDialogCommand = ReactiveCommand.CreateFromTask(async _ => await OpenUrlDialog());
            OpenUrlDialogCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => LogAndNotify(NotificationType.Error, Messages.ERR_CHANNEL_LOAD));

            // channels busy indicator
            this.WhenAnyObservable(_ => _.ChannelsBusyList.CountChanged).DistinctUntilChanged()
            .Subscribe(_ => IsChannelsBusy = _ > 0);

            // episodes busy indicator
            this.WhenAnyObservable(_ => _.EpisodesBusyList.CountChanged).DistinctUntilChanged()
            .Subscribe(_ => IsEpisodesBusy = _ > 0);

            OpenUrlCommand = ReactiveCommand.CreateFromTask <string>(async _ => await OnOpenUrl(_));
            OpenUrlCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => LogAndNotify(NotificationType.Error, Messages.ERR_CHANNEL_LOAD));

            // load channel
            LoadChannelFromUrlCommand = ReactiveCommand.CreateFromTask <string>(LoadChannelFromUrlAsync);
            OpenUrlCommand.IsExecuting.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ =>
            {
                OnChannelsBusy(OpenUrlCommand.GetHashCode(), _);
                OnEpisodesBusy(OpenUrlCommand.GetHashCode(), _);
            });

            LoadChannelFromUrlCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => LogAndNotify(NotificationType.Error, Messages.ERR_CHANNEL_LOAD));

            // delete channel
            ConfirmDeleteChannelCommand = ReactiveCommand.CreateFromTask <Channel>(async _ => await ConfirmDelete(_));
            DeleteChannelCommand        = ReactiveCommand.CreateFromTask <Channel>(async _ => await DeleteChannel(_));
            DeleteChannelCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => LogAndNotify(NotificationType.Error, Messages.ERR_CHANNEL_DELETE));
            DeleteChannelCommand.IsExecuting.Subscribe(_ =>
            {
                OnChannelsBusy(DeleteChannelCommand.GetHashCode(), _);
                OnEpisodesBusy(DeleteChannelCommand.GetHashCode(), _);
            });

            var existsSelectedChannel = this.WhenAny(vm => vm.SelectedChannel, _ => SelectedChannel != null);

            this.WhenAnyValue(vm => vm.SelectedChannel).Select(_ => _ == null ? 0 : SelectedChannel.Id)
            .ToProperty(this, _ => _.SelectedChannelId, out selectedChannelId);
            RemoveFilterCommand = ReactiveCommand.Create <Unit>(_ => SelectedChannel = null, existsSelectedChannel);

            MarkAllPlayedCommand = ReactiveCommand.CreateFromTask <Channel>(async _ => await MarkChannelPlayed(_));

            CopyUrlCommand = ReactiveCommand.Create <Channel>(_ => Clipboard.SetText(_.Link));

            var episodeSet = this.WhenAnyValue(_ => _.SelectedEpisode);

            // detail
            this.WhenAnyValue(vm => vm.SelectedChannel).NotNull().Cast <IDetailEntity>()
            .Merge(episodeSet.Cast <IDetailEntity>())
            .ToProperty(this, _ => _.CurrentDetailEntity, out currentDetailEntity);

            this.WhenAnyValue(vm => vm.Player.LocateCurrent).NotNull()
            .Subscribe(c => c.Subscribe(_ => LocatePageForCurrent()));

            // episode list is loading
            this.WhenAnyValue(_ => _.EpisodeList.IsLoading).SubscribeOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => OnEpisodesBusy(EpisodeList.GetHashCode(), _));

            ScrollIntoView = ReactiveCommand.Create <int, int>(_ => _);

            // playlist
            AddToPlaylist = ReactiveCommand.CreateFromTask <Playlist>(async _ => await AddEpisodeToPlaylist(_));

            // sort order
            this.WhenAnyValue(_ => _.SelectedEpisodeSortOrder)
            .Subscribe(_ => EpisodeOrderBy.OnNext(GetEpisodeSortOrder(_)));
            ToggleSortDirectionCommand =
                ReactiveCommand.Create(
                    () => EpisodeOrderBy.OnNext(GetEpisodeSortOrder(SelectedEpisodeSortOrder, false)));

            // on channels changed
            this.WhenAnyValue(_ => _.Channels).NotNull().Subscribe(_ => OnChannelsChanged());

            // update channels
            var canUpdate = new BehaviorSubject <bool>(false);

            UpdateChannelsCommand = ReactiveCommand.CreateFromTask(async _ => await UpdateChannelsAsync(),
                                                                   canUpdate.DistinctUntilChanged());

            UpdateChannelsCommand.IsExecuting.CombineLatest(this.WhenAnyValue(_ => _.Channels.IsEmpty),
                                                            (exec, empty) => !exec && !empty && IsUpdateEnabled).DistinctUntilChanged()
            .Subscribe(_ => canUpdate.OnNext(_));

            UpdateChannelsCommand.IsExecuting.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ => IsUpdating = _);
            UpdateChannelsCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => LogAndNotify(NotificationType.Error, Messages.ERR_CHANNEL_UPDATE));

            // init episodes
            EpisodeList = InitEpisodeList();
            EpisodeList.Changed.Subscribe(_ => InitActivePlaylist());

            // detail
            ToggleShowDetailCommand = ReactiveCommand.Create <bool, bool>(_ => _);
            DeactivateCommand       = ReactiveCommand.Create(() => { });
        }