diff --git a/Application/VoteForComment/VoteForCommentCommandHandler.cs b/Application/VoteForComment/VoteForCommentCommandHandler.cs index acaa918..ad3bb67 100644 --- a/Application/VoteForComment/VoteForCommentCommandHandler.cs +++ b/Application/VoteForComment/VoteForCommentCommandHandler.cs @@ -31,8 +31,6 @@ namespace HN.Application break; } - await _commentRepository.UpdateAsync(comment); - return Unit.Value; } } diff --git a/Application/VoteForLink/VoteForLinkCommandHandler.cs b/Application/VoteForLink/VoteForLinkCommandHandler.cs index f9aad21..38332e2 100644 --- a/Application/VoteForLink/VoteForLinkCommandHandler.cs +++ b/Application/VoteForLink/VoteForLinkCommandHandler.cs @@ -31,8 +31,6 @@ namespace HN.Application break; } - await _linkRepository.UpdateAsync(link); - return Unit.Value; } } diff --git a/Apps/Website/Controllers/AccountsController.cs b/Apps/Website/Controllers/AccountsController.cs index c62e13e..0215466 100644 --- a/Apps/Website/Controllers/AccountsController.cs +++ b/Apps/Website/Controllers/AccountsController.cs @@ -1,6 +1,6 @@ using System.Linq; using System.Threading.Tasks; -using HN.Infrastructure; +using HN.Infrastructure.Identity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; diff --git a/Apps/Website/HttpExecutingUserProvider.cs b/Apps/Website/HttpExecutingUserProvider.cs index e42e9ce..420b668 100644 --- a/Apps/Website/HttpExecutingUserProvider.cs +++ b/Apps/Website/HttpExecutingUserProvider.cs @@ -1,6 +1,6 @@ using System; using HN.Application; -using HN.Infrastructure; +using HN.Infrastructure.Identity; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; diff --git a/Apps/Website/Startup.cs b/Apps/Website/Startup.cs index 360a510..5aa2e61 100644 --- a/Apps/Website/Startup.cs +++ b/Apps/Website/Startup.cs @@ -1,6 +1,9 @@ using HN.Application; using HN.Domain; using HN.Infrastructure; +using HN.Infrastructure.Behaviors; +using HN.Infrastructure.Identity; +using HN.Infrastructure.Repositories; using MediatR; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; @@ -32,6 +35,7 @@ namespace Website services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>)); services.AddMediatR(typeof(HN.Application.IHNContext)); // Permet d'avoir des routes en lowercase diff --git a/Domain/ICommentRepository.cs b/Domain/ICommentRepository.cs index 77706bc..a11f874 100644 --- a/Domain/ICommentRepository.cs +++ b/Domain/ICommentRepository.cs @@ -6,7 +6,6 @@ namespace HN.Domain public interface ICommentRepository { Task AddAsync(Comment comment); - Task UpdateAsync(Comment comment); Task GetByIdAsync(Guid id); } } \ No newline at end of file diff --git a/Domain/ILinkRepository.cs b/Domain/ILinkRepository.cs index 016ee1f..f7f6607 100644 --- a/Domain/ILinkRepository.cs +++ b/Domain/ILinkRepository.cs @@ -6,7 +6,6 @@ namespace HN.Domain public interface ILinkRepository { Task AddAsync(Link link); - Task UpdateAsync(Link link); Task GetByIdAsync(Guid id); } } \ No newline at end of file diff --git a/Infrastructure/Behaviors/UnitOfWorkBehavior.cs b/Infrastructure/Behaviors/UnitOfWorkBehavior.cs new file mode 100644 index 0000000..a320f7f --- /dev/null +++ b/Infrastructure/Behaviors/UnitOfWorkBehavior.cs @@ -0,0 +1,25 @@ +using System.Threading; +using System.Threading.Tasks; +using MediatR; + +namespace HN.Infrastructure.Behaviors +{ + public sealed class UnitOfWorkBehavior : IPipelineBehavior + { + private readonly HNDbContext _context; + + public UnitOfWorkBehavior(HNDbContext context) + { + _context = context; + } + + public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + { + var response = await next(); + + await _context.SaveChangesAsync(); + + return response; + } + } +} \ No newline at end of file diff --git a/Infrastructure/EntityTypes/CommentEntityType.cs b/Infrastructure/EntityTypes/CommentEntityType.cs index dffdd31..b13d246 100644 --- a/Infrastructure/EntityTypes/CommentEntityType.cs +++ b/Infrastructure/EntityTypes/CommentEntityType.cs @@ -1,8 +1,9 @@ using HN.Domain; +using HN.Infrastructure.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; -namespace HN.Infrastructure +namespace HN.Infrastructure.EntityTypes { public sealed class CommentEntityType : IEntityTypeConfiguration { diff --git a/Infrastructure/EntityTypes/LinkEntityType.cs b/Infrastructure/EntityTypes/LinkEntityType.cs index 5fdaa51..dc91ee7 100644 --- a/Infrastructure/EntityTypes/LinkEntityType.cs +++ b/Infrastructure/EntityTypes/LinkEntityType.cs @@ -1,4 +1,5 @@ using HN.Domain; +using HN.Infrastructure.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; diff --git a/Infrastructure/HNDbContext.cs b/Infrastructure/HNDbContext.cs index ba600f1..38e17b2 100644 --- a/Infrastructure/HNDbContext.cs +++ b/Infrastructure/HNDbContext.cs @@ -2,6 +2,7 @@ using System.Linq; using HN.Application; using HN.Domain; +using HN.Infrastructure.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; diff --git a/Infrastructure/Role.cs b/Infrastructure/Identity/Role.cs similarity index 74% rename from Infrastructure/Role.cs rename to Infrastructure/Identity/Role.cs index 82e6405..d7abed0 100644 --- a/Infrastructure/Role.cs +++ b/Infrastructure/Identity/Role.cs @@ -1,7 +1,7 @@ using System; using Microsoft.AspNetCore.Identity; -namespace HN.Infrastructure +namespace HN.Infrastructure.Identity { public sealed class Role : IdentityRole { diff --git a/Infrastructure/User.cs b/Infrastructure/Identity/User.cs similarity index 84% rename from Infrastructure/User.cs rename to Infrastructure/Identity/User.cs index fb961fa..e106eba 100644 --- a/Infrastructure/User.cs +++ b/Infrastructure/Identity/User.cs @@ -2,7 +2,7 @@ using System; using HN.Application; using Microsoft.AspNetCore.Identity; -namespace HN.Infrastructure +namespace HN.Infrastructure.Identity { public sealed class User : IdentityUser, IUser { diff --git a/Infrastructure/CommentRepository.cs b/Infrastructure/Repositories/CommentRepository.cs similarity index 79% rename from Infrastructure/CommentRepository.cs rename to Infrastructure/Repositories/CommentRepository.cs index 3fbe0f7..2f43828 100644 --- a/Infrastructure/CommentRepository.cs +++ b/Infrastructure/Repositories/CommentRepository.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using HN.Domain; using Microsoft.EntityFrameworkCore; -namespace HN.Infrastructure +namespace HN.Infrastructure.Repositories { public sealed class CommentRepository : Repository, ICommentRepository { @@ -20,10 +20,5 @@ namespace HN.Infrastructure { return Entries.SingleOrDefaultAsync(o => o.Id == id); } - - public Task UpdateAsync(Comment comment) - { - return base.UpdateAsync(comment); - } } } \ No newline at end of file diff --git a/Infrastructure/LinkRepository.cs b/Infrastructure/Repositories/LinkRepository.cs similarity index 79% rename from Infrastructure/LinkRepository.cs rename to Infrastructure/Repositories/LinkRepository.cs index 87b8614..b3e7802 100644 --- a/Infrastructure/LinkRepository.cs +++ b/Infrastructure/Repositories/LinkRepository.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using HN.Domain; using Microsoft.EntityFrameworkCore; -namespace HN.Infrastructure +namespace HN.Infrastructure.Repositories { public sealed class LinkRepository : Repository, ILinkRepository { @@ -20,10 +20,5 @@ namespace HN.Infrastructure { return Entries.SingleOrDefaultAsync(o => o.Id == id); } - - public Task UpdateAsync(Link link) - { - return base.UpdateAsync(link); - } } } \ No newline at end of file diff --git a/Infrastructure/Repository.cs b/Infrastructure/Repositories/Repository.cs similarity index 71% rename from Infrastructure/Repository.cs rename to Infrastructure/Repositories/Repository.cs index fa85942..d3bcd9d 100644 --- a/Infrastructure/Repository.cs +++ b/Infrastructure/Repositories/Repository.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -namespace HN.Infrastructure +namespace HN.Infrastructure.Repositories { public abstract class Repository where TEntity : class { @@ -18,12 +18,6 @@ namespace HN.Infrastructure protected async Task AddAsync(params TEntity[] entities) { await Entries.AddRangeAsync(entities); - await _context.SaveChangesAsync(); - } - - protected async Task UpdateAsync(params TEntity[] entities) - { - await _context.SaveChangesAsync(); } } } \ No newline at end of file