Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using ErrorOr;
using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands;
public record PublishPostCommand(Guid AuthorId, string Title, string Content) : IRequest<ErrorOr<Post>>;
public class PublishPostCommandHandler(IApplicationDbContext dbContext) : IRequestHandler<PublishPostCommand, ErrorOr<Post>>
{
public async Task<ErrorOr<Post>> Handle(PublishPostCommand request, CancellationToken cancellationToken)
{
var post = new Post {AuthorId = request.AuthorId, Title = request.Title, Content = request.Content};
await dbContext.Posts.AddAsync(post, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
// TODO fire event PostPublished
return post;
}
}
public class PublishPostCommandValidator : AbstractValidator<PublishPostCommand>
{
public PublishPostCommandValidator()
{
RuleFor(p => p.AuthorId)
.NotEmpty();
RuleFor(p => p.Title)
.NotEmpty()
.Length(3, 64);
RuleFor(p => p.Content)
.NotEmpty()
.Length(3, 512);
}
}