tiny-refactors #27
@ -31,8 +31,6 @@ namespace HN.Application
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _commentRepository.UpdateAsync(comment);
|
|
||||||
|
|
||||||
return Unit.Value;
|
return Unit.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,8 +31,6 @@ namespace HN.Application
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _linkRepository.UpdateAsync(link);
|
|
||||||
|
|
||||||
return Unit.Value;
|
return Unit.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using HN.Infrastructure;
|
using HN.Infrastructure.Identity;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using HN.Application;
|
using HN.Application;
|
||||||
using HN.Infrastructure;
|
using HN.Infrastructure.Identity;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
using HN.Application;
|
using HN.Application;
|
||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
using HN.Infrastructure;
|
using HN.Infrastructure;
|
||||||
|
using HN.Infrastructure.Behaviors;
|
||||||
|
using HN.Infrastructure.Identity;
|
||||||
|
using HN.Infrastructure.Repositories;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
@ -32,6 +35,7 @@ namespace Website
|
|||||||
services.AddScoped<ILinkRepository, LinkRepository>();
|
services.AddScoped<ILinkRepository, LinkRepository>();
|
||||||
services.AddScoped<ICommentRepository, CommentRepository>();
|
services.AddScoped<ICommentRepository, CommentRepository>();
|
||||||
services.AddScoped<IExecutingUserProvider, HttpExecutingUserProvider>();
|
services.AddScoped<IExecutingUserProvider, HttpExecutingUserProvider>();
|
||||||
|
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>));
|
||||||
services.AddMediatR(typeof(HN.Application.IHNContext));
|
services.AddMediatR(typeof(HN.Application.IHNContext));
|
||||||
|
|
||||||
// Permet d'avoir des routes en lowercase
|
// Permet d'avoir des routes en lowercase
|
||||||
|
|||||||
@ -6,7 +6,6 @@ namespace HN.Domain
|
|||||||
public interface ICommentRepository
|
public interface ICommentRepository
|
||||||
{
|
{
|
||||||
Task AddAsync(Comment comment);
|
Task AddAsync(Comment comment);
|
||||||
Task UpdateAsync(Comment comment);
|
|
||||||
Task<Comment> GetByIdAsync(Guid id);
|
Task<Comment> GetByIdAsync(Guid id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,7 +6,6 @@ namespace HN.Domain
|
|||||||
public interface ILinkRepository
|
public interface ILinkRepository
|
||||||
{
|
{
|
||||||
Task AddAsync(Link link);
|
Task AddAsync(Link link);
|
||||||
Task UpdateAsync(Link link);
|
|
||||||
Task<Link> GetByIdAsync(Guid id);
|
Task<Link> GetByIdAsync(Guid id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
25
Infrastructure/Behaviors/UnitOfWorkBehavior.cs
Normal file
25
Infrastructure/Behaviors/UnitOfWorkBehavior.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HN.Infrastructure.Behaviors
|
||||||
|
{
|
||||||
|
public sealed class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||||
|
{
|
||||||
|
private readonly HNDbContext _context;
|
||||||
|
|
||||||
|
public UnitOfWorkBehavior(HNDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||||
|
{
|
||||||
|
var response = await next();
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,9 @@
|
|||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
|
using HN.Infrastructure.Identity;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.EntityTypes
|
||||||
{
|
{
|
||||||
public sealed class CommentEntityType : IEntityTypeConfiguration<Comment>
|
public sealed class CommentEntityType : IEntityTypeConfiguration<Comment>
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
|
using HN.Infrastructure.Identity;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using HN.Application;
|
using HN.Application;
|
||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
|
using HN.Infrastructure.Identity;
|
||||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.Identity
|
||||||
{
|
{
|
||||||
public sealed class Role : IdentityRole<Guid>
|
public sealed class Role : IdentityRole<Guid>
|
||||||
{
|
{
|
||||||
@ -2,7 +2,7 @@ using System;
|
|||||||
using HN.Application;
|
using HN.Application;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.Identity
|
||||||
{
|
{
|
||||||
public sealed class User : IdentityUser<Guid>, IUser
|
public sealed class User : IdentityUser<Guid>, IUser
|
||||||
{
|
{
|
||||||
@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
|||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.Repositories
|
||||||
{
|
{
|
||||||
public sealed class CommentRepository : Repository<Comment>, ICommentRepository
|
public sealed class CommentRepository : Repository<Comment>, ICommentRepository
|
||||||
{
|
{
|
||||||
@ -20,10 +20,5 @@ namespace HN.Infrastructure
|
|||||||
{
|
{
|
||||||
return Entries.SingleOrDefaultAsync(o => o.Id == id);
|
return Entries.SingleOrDefaultAsync(o => o.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task UpdateAsync(Comment comment)
|
|
||||||
{
|
|
||||||
return base.UpdateAsync(comment);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
|||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.Repositories
|
||||||
{
|
{
|
||||||
public sealed class LinkRepository : Repository<Link>, ILinkRepository
|
public sealed class LinkRepository : Repository<Link>, ILinkRepository
|
||||||
{
|
{
|
||||||
@ -20,10 +20,5 @@ namespace HN.Infrastructure
|
|||||||
{
|
{
|
||||||
return Entries.SingleOrDefaultAsync(o => o.Id == id);
|
return Entries.SingleOrDefaultAsync(o => o.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task UpdateAsync(Link link)
|
|
||||||
{
|
|
||||||
return base.UpdateAsync(link);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace HN.Infrastructure
|
namespace HN.Infrastructure.Repositories
|
||||||
{
|
{
|
||||||
public abstract class Repository<TEntity> where TEntity : class
|
public abstract class Repository<TEntity> where TEntity : class
|
||||||
{
|
{
|
||||||
@ -18,12 +18,6 @@ namespace HN.Infrastructure
|
|||||||
protected async Task AddAsync(params TEntity[] entities)
|
protected async Task AddAsync(params TEntity[] entities)
|
||||||
{
|
{
|
||||||
await Entries.AddRangeAsync(entities);
|
await Entries.AddRangeAsync(entities);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task UpdateAsync(params TEntity[] entities)
|
|
||||||
{
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user