public async Task <Result> Handle(BanSubredditCommand command, CancellationToken cancellationToken)
    {
        if (command == null)
        {
            throw new NullReferenceException(nameof(command));
        }

        var guild = await _context.Guilds.FirstOrDefaultAsync(guild => guild.Id == command.GuildId, cancellationToken);

        if (guild == null)
        {
            throw new NullReferenceException(nameof(guild));
        }

        var subreddit = await _context.Subreddits.FirstOrDefaultAsync(subreddit => subreddit.Id == command.SubredditId, cancellationToken);

        if (subreddit == null)
        {
            throw new NullReferenceException(nameof(subreddit));
        }

        var bannedSubreddit = _mapper.Map <BannedSubreddit>(command);
        await _context.BannedSubreddits.AddAsync(bannedSubreddit, cancellationToken);

        await _context.SaveChangesAsync(cancellationToken);

        return(new Result()
        {
            Messages = new List <string> {
                "Subreddit banned"
            }, Succeeded = true
        });
    }
示例#2
0
 public async Task <IActionResult> BanSubreddit(BanSubredditCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }