Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • xbily/pv293-socialnetwork-social
1 result
Show changes
Commits on Source (2)
namespace SocialNetwork.Social.Application.Common.Models;
public class Result
{
internal Result(bool succeeded, IEnumerable<string> errors)
{
Succeeded = succeeded;
Errors = errors.ToArray();
}
public bool Succeeded { get; init; }
public string[] Errors { get; init; }
public static Result Success()
{
return new Result(true, Array.Empty<string>());
}
public static Result Failure(IEnumerable<string> errors)
{
return new Result(false, errors);
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using MediatR; using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces; using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed; using SocialNetwork.Social.Domain.Entities.Feed;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands; namespace SocialNetwork.Social.Application.Posts.Commands;
...@@ -21,6 +22,7 @@ public class CommentPostCommandHandler(IApplicationDbContext dbContext) : IReque ...@@ -21,6 +22,7 @@ public class CommentPostCommandHandler(IApplicationDbContext dbContext) : IReque
post.Comments.Add(comment); post.Comments.Add(comment);
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
post.AddDomainEvent(new PostCommentedEvent(post, comment));
dbContext.Posts.Update(post); dbContext.Posts.Update(post);
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using MediatR; using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces; using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed; using SocialNetwork.Social.Domain.Entities.Feed;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands; namespace SocialNetwork.Social.Application.Posts.Commands;
...@@ -13,10 +14,11 @@ public class PublishPostCommandHandler(IApplicationDbContext dbContext) : IReque ...@@ -13,10 +14,11 @@ public class PublishPostCommandHandler(IApplicationDbContext dbContext) : IReque
{ {
var post = new Post {AuthorId = request.AuthorId, Title = request.Title, Content = request.Content}; var post = new Post {AuthorId = request.AuthorId, Title = request.Title, Content = request.Content};
post.AddDomainEvent(new PostPublishedEvent(post));
await dbContext.Posts.AddAsync(post, cancellationToken); await dbContext.Posts.AddAsync(post, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
// TODO fire event PostPublished await dbContext.SaveChangesAsync(cancellationToken);
return post; return post;
} }
......
using ErrorOr; using ErrorOr;
using MediatR; using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces; using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands; namespace SocialNetwork.Social.Application.Posts.Commands;
...@@ -16,7 +17,7 @@ public class LikePostCommandHandler(IApplicationDbContext dbContext) : IRequestH ...@@ -16,7 +17,7 @@ public class LikePostCommandHandler(IApplicationDbContext dbContext) : IRequestH
post.LikesCount++; // I would create a whole table for likers and postId to add who liked the post post.LikesCount++; // I would create a whole table for likers and postId to add who liked the post
// TODO fire post liked event post.AddDomainEvent(new PostLikedEvent(post.Id, request.LikerId));
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
return Result.Success; return Result.Success;
......
...@@ -3,13 +3,13 @@ using MediatR; ...@@ -3,13 +3,13 @@ using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces; using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed; using SocialNetwork.Social.Domain.Entities.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands; namespace SocialNetwork.Social.Application.Posts.Queries;
public record GetPostByIdCommand(Guid PostId) : IRequest<ErrorOr<Post>>; public record GetPostByIdQuery(Guid PostId) : IRequest<ErrorOr<Post>>;
public class GetPostByIdCommandHandler(IApplicationDbContext dbContext) : IRequestHandler<GetPostByIdCommand, ErrorOr<Post>> public class GetPostByIdQueryHandler(IApplicationDbContext dbContext) : IRequestHandler<GetPostByIdQuery, ErrorOr<Post>>
{ {
public async Task<ErrorOr<Post>> Handle(GetPostByIdCommand request, CancellationToken cancellationToken) public async Task<ErrorOr<Post>> Handle(GetPostByIdQuery request, CancellationToken cancellationToken)
{ {
var postIdToFind = request.PostId; var postIdToFind = request.PostId;
var post = await dbContext var post = await dbContext
......
...@@ -2,14 +2,15 @@ ...@@ -2,14 +2,15 @@
using SocialNetwork.Social.Application.Common.Interfaces; using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed; using SocialNetwork.Social.Domain.Entities.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands; namespace SocialNetwork.Social.Application.Posts.Queries;
public class GetPostsCommand : IRequest<List<Post>>; // Could include a filter and pagination here public class GetPostsQuery : IRequest<List<Post>>; // Could include a filter and pagination here
public class GetPostsCommandHandler(IApplicationDbContext dbContext) : IRequestHandler<GetPostsCommand, List<Post>> public class GetPostsQueryHandler(IApplicationDbContext dbContext) : IRequestHandler<GetPostsQuery, List<Post>>
{ {
public Task<List<Post>> Handle(GetPostsCommand request, CancellationToken cancellationToken) => public Task<List<Post>> Handle(GetPostsQuery request, CancellationToken cancellationToken) =>
dbContext dbContext
.Posts .Posts
.AsNoTracking()
.ToListAsync(cancellationToken: cancellationToken); .ToListAsync(cancellationToken: cancellationToken);
} }
namespace SocialNetwork.Social.Domain.ValueObjects; namespace SocialNetwork.Social.Domain.ValueObjects;
public class SocialProfile public class SocialProfile : ValueObject
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public required string Name { get; set; } public required string Name { get; set; }
public required string Email { get; set; } public required string Email { get; set; }
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Id;
yield return Name;
yield return Email;
}
} }
using ErrorOr; using ErrorOr;
using MediatR; using MediatR;
using SocialNetwork.Social.Application.Posts.Commands; using SocialNetwork.Social.Application.Posts.Commands;
using SocialNetwork.Social.Application.Posts.Queries;
namespace WebApi.Endpoints; namespace WebApi.Endpoints;
...@@ -20,11 +21,11 @@ public class PostEndpoints ...@@ -20,11 +21,11 @@ public class PostEndpoints
} }
private async Task<IResult> GetAllPosts(ISender sender) => private async Task<IResult> GetAllPosts(ISender sender) =>
Results.Ok(await sender.Send(new GetPostsCommand())); Results.Ok(await sender.Send(new GetPostsQuery()));
private async Task<IResult> GetPostById(Guid postId, ISender sender) private async Task<IResult> GetPostById(Guid postId, ISender sender)
{ {
var errorOrPost = await sender.Send(new GetPostByIdCommand(postId)); var errorOrPost = await sender.Send(new GetPostByIdQuery(postId));
return errorOrPost.MatchFirst( return errorOrPost.MatchFirst(
Results.Ok, Results.Ok,
error => error.Type switch error => error.Type switch
......