public async Task <Result> Handle(RemovePlaylistSubredditCommand command, CancellationToken cancellationToken)
    {
        var playlist = await _context.Playlists.Include(x => x.Subreddits).FirstOrDefaultAsync(playlist => playlist.Id == command.PlaylistId);

        if (playlist == null)
        {
            return((await Result.FailAsync("Playlist not found")) as Result);
        }

        var subreddit = playlist.Subreddits.FirstOrDefault(sub => sub.Id == command.SubredditId);

        if (subreddit == null)
        {
            return((await Result.FailAsync("Subreddit is not in the playlist")) as Result);
        }

        playlist.Subreddits.Remove(subreddit);
        _context.Playlists.Update(playlist);
        await _context.SaveChangesAsync(cancellationToken);

        return((await Result.SuccessAsync("Subreddit removed from the playlist")) as Result);
    }
Exemplo n.º 2
0
 public async Task <IActionResult> RemoveSubreddit(RemovePlaylistSubredditCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }