Permalink
Browse files

Implemented "Flag as Spam" button.

  • Loading branch information...
william-gross committed Oct 5, 2018
1 parent 8a85934 commit c8d5cabafc0b8dadf843cb109a933927e091e573
@@ -41,5 +41,7 @@ public class Article
public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public bool IsSpam { get; set; }
}
}
@@ -58,5 +58,12 @@ public async Task Delete(string slug)
{
await _mediator.Send(new Delete.Command(slug));
}

[HttpPatch("{slug}")]
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
public async Task MarkSpam(string slug)
{
await _mediator.Send(new MarkSpam.Command(slug));
}
}
}
@@ -86,7 +86,8 @@ public async Task<ArticleEnvelope> Handle(Command message, CancellationToken can
UpdatedAt = DateTime.UtcNow,
Description = message.Article.Description,
Title = message.Article.Title,
Slug = message.Article.Title.GenerateSlug()
Slug = message.Article.Title.GenerateSlug(),
IsSpam = false
};
await _context.Articles.AddAsync(article, cancellationToken);

@@ -0,0 +1,57 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Conduit.Infrastructure;
using Conduit.Infrastructure.Errors;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Conduit.Features.Articles
{
public class MarkSpam
{
public class Command : IRequest
{
public Command(string slug)
{
Slug = slug;
}

public string Slug { get; set; }
}

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
RuleFor(x => x.Slug).NotNull().NotEmpty();
}
}

public class QueryHandler : IRequestHandler<Command>
{
private readonly ConduitContext _context;

public QueryHandler(ConduitContext context)
{
_context = context;
}

public async Task<Unit> Handle(Command message, CancellationToken cancellationToken)
{
var article = await _context.Articles
.FirstOrDefaultAsync(x => x.Slug == message.Slug, cancellationToken);

if (article == null)
{
throw new RestException(HttpStatusCode.NotFound, new { Article = Constants.NOT_FOUND });
}

article.IsSpam = true;
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
}
}

0 comments on commit c8d5cab

Please sign in to comment.