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)
......@@ -4,7 +4,7 @@ using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands;
namespace SocialNetwork.Social.Application.Feed.Commands;
public record CommentPostCommand(Guid PostId, Guid CommenterId, string Content) : IRequest<ErrorOr<Comment>>;
......
......@@ -4,7 +4,7 @@ using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands;
namespace SocialNetwork.Social.Application.Feed.Commands;
public record PublishPostCommand(Guid AuthorId, string Title, string Content) : IRequest<ErrorOr<Post>>;
......
......@@ -3,7 +3,7 @@ using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Events.Feed;
namespace SocialNetwork.Social.Application.Posts.Commands;
namespace SocialNetwork.Social.Application.Feed.Commands;
public record LikePostCommand(Guid PostId, Guid LikerId) : IRequest<ErrorOr<Success>>;
......
using ErrorOr;
namespace SocialNetwork.Social.Application.Posts;
namespace SocialNetwork.Social.Application.Feed;
public static class PostErrors
{
......
......@@ -3,7 +3,7 @@ using MediatR;
using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed;
namespace SocialNetwork.Social.Application.Posts.Queries;
namespace SocialNetwork.Social.Application.Feed.Queries;
public record GetPostByIdQuery(Guid PostId) : IRequest<ErrorOr<Post>>;
......
......@@ -2,7 +2,7 @@
using SocialNetwork.Social.Application.Common.Interfaces;
using SocialNetwork.Social.Domain.Entities.Feed;
namespace SocialNetwork.Social.Application.Posts.Queries;
namespace SocialNetwork.Social.Application.Feed.Queries;
public class GetPostsQuery : IRequest<List<Post>>; // Could include a filter and pagination here
......
namespace SocialNetwork.Social.Domain.Common;
using MediatR;
public abstract class BaseEvent;
namespace SocialNetwork.Social.Domain.Common;
public abstract class BaseEvent : INotification;
......@@ -5,4 +5,8 @@
<AssemblyName>SocialNetwork.Social.Domain</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" />
</ItemGroup>
</Project>
using SocialNetwork.Social.Domain.Common;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using SocialNetwork.Social.Domain.Common;
namespace SocialNetwork.Social.Infrastructure.Data.Interceptors;
public class DispatchDomainEventsInterceptor : SaveChangesInterceptor
public class DispatchDomainEventsInterceptor(IMediator mediator) : SaveChangesInterceptor
{
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
{
......@@ -13,34 +14,35 @@ public class DispatchDomainEventsInterceptor : SaveChangesInterceptor
return base.SavingChanges(eventData, result);
}
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = default)
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(
DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = default
)
{
await DispatchDomainEvents(eventData.Context);
return await base.SavingChangesAsync(eventData, result, cancellationToken);
}
public Task DispatchDomainEvents(DbContext? context)
public async Task DispatchDomainEvents(DbContext? context)
{
if (context == null) return Task.CompletedTask;
if (context == null) return;
var entities = context.ChangeTracker
.Entries<BaseEntity>()
.Where(e => e.Entity.DomainEvents.Any())
.Select(e => e.Entity);
.Select(e => e.Entity)
.ToList();
var domainEvents = entities
.SelectMany(e => e.DomainEvents)
.ToList();
entities.ToList().ForEach(e => e.ClearDomainEvents());
entities.ForEach(e => e.ClearDomainEvents());
foreach (var domainEvent in domainEvents)
{
// TODO Publish domain event
await mediator.Publish(domainEvent);
}
return Task.CompletedTask;
}
}
using ErrorOr;
using MediatR;
using SocialNetwork.Social.Application.Posts.Commands;
using SocialNetwork.Social.Application.Posts.Queries;
using SocialNetwork.Social.Application.Feed.Commands;
using SocialNetwork.Social.Application.Feed.Queries;
namespace WebApi.Endpoints;
public class PostEndpoints
public class FeedEndpoints
{
public void Map(IEndpointRouteBuilder builder)
{
......
......@@ -10,7 +10,7 @@ public static class WebApplicationExtensions
private static RouteGroupBuilder MapPostEndpoints(RouteGroupBuilder routeBuilder)
{
var postGroup = routeBuilder.MapGroup("posts");
new PostEndpoints().Map(postGroup);
new FeedEndpoints().Map(postGroup);
return routeBuilder;
}
......