-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from sj-distributor/repo2
Implementing the repository and uow patterns
- Loading branch information
Showing
23 changed files
with
260 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/Wax.Core/Data/Repositories/EfCoreCustomerRepository.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
src/Wax.Core/Handlers/CommandHandlers/Customers/DeleteCustomerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Wax.Core.Domain.Customers; | ||
using Wax.Core.Repositories; | ||
using Wax.Messages.Commands.Customers; | ||
|
||
namespace Wax.Core.Handlers.CommandHandlers.Customers; | ||
|
||
public class DeleteCustomerCommandHandler: ICommandHandler<DeleteCustomerCommand> | ||
{ | ||
private readonly ICustomerRepository _customerRepository; | ||
private readonly IUnitOfWork _unitOfWork; | ||
|
||
public DeleteCustomerCommandHandler(ICustomerRepository customerRepository) | ||
public DeleteCustomerCommandHandler(IUnitOfWork unitOfWork) | ||
{ | ||
_customerRepository = customerRepository; | ||
_unitOfWork = unitOfWork; | ||
} | ||
|
||
public async Task Handle(IReceiveContext<DeleteCustomerCommand> context, CancellationToken cancellationToken) | ||
{ | ||
var customer = await _customerRepository.GetByIdAsync(context.Message.CustomerId, cancellationToken); | ||
var customer = await _unitOfWork.Customers.GetByIdAsync(context.Message.CustomerId, cancellationToken); | ||
|
||
await _customerRepository.DeleteAsync(customer, cancellationToken); | ||
await _unitOfWork.Customers.DeleteAsync(customer, cancellationToken); | ||
await _unitOfWork.SaveChangesAsync(cancellationToken); | ||
} | ||
} |
19 changes: 10 additions & 9 deletions
19
src/Wax.Core/Handlers/CommandHandlers/Customers/UpdateCustomerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
using AutoMapper; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Wax.Core.Domain.Customers; | ||
using Wax.Core.Domain.Customers.Exceptions; | ||
using Wax.Core.Repositories; | ||
using Wax.Messages.Commands.Customers; | ||
|
||
namespace Wax.Core.Handlers.CommandHandlers.Customers; | ||
|
||
public class UpdateCustomerCommandHandler : ICommandHandler<UpdateCustomerCommand> | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly ICustomerRepository _repository; | ||
private readonly IUnitOfWork _unitOfWork; | ||
|
||
public UpdateCustomerCommandHandler(IMapper mapper, ICustomerRepository repository) | ||
public UpdateCustomerCommandHandler(IMapper mapper, IUnitOfWork unitOfWork) | ||
{ | ||
_mapper = mapper; | ||
_repository = repository; | ||
_unitOfWork = unitOfWork; | ||
} | ||
|
||
public async Task Handle(IReceiveContext<UpdateCustomerCommand> context, CancellationToken cancellationToken) | ||
{ | ||
var customer = await _repository.GetByIdAsync(context.Message.CustomerId, cancellationToken) | ||
.ConfigureAwait(false); | ||
var customer = await _unitOfWork.Customers.GetByIdAsync(context.Message.CustomerId, cancellationToken); | ||
|
||
if (customer.Name != context.Message.Name) | ||
{ | ||
if (!await _repository.CheckIsUniqueNameAsync(context.Message.Name, cancellationToken) | ||
.ConfigureAwait(false)) | ||
var existing = await _unitOfWork.Customers.FindByNameAsync(context.Message.Name, cancellationToken); | ||
|
||
if (existing != null) | ||
{ | ||
throw new CustomerNameAlreadyExistsException(); | ||
} | ||
} | ||
|
||
_mapper.Map(context.Message, customer); | ||
|
||
await _repository.UpdateAsync(customer, cancellationToken).ConfigureAwait(false); | ||
await _unitOfWork.Customers.UpdateAsync(customer, cancellationToken).ConfigureAwait(false); | ||
await _unitOfWork.SaveChangesAsync(cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Wax.Core.Data; | ||
using Wax.Core.DependencyInjection; | ||
using Wax.Core.Domain.Customers; | ||
|
||
namespace Wax.Core.Repositories; | ||
|
||
public class EfCoreCustomerRepository : EfCoreBasicRepository<Customer>, ICustomerRepository, IScopedDependency | ||
{ | ||
public EfCoreCustomerRepository(ApplicationDbContext dbContext) : base(dbContext) | ||
{ | ||
} | ||
|
||
public Task<Customer> FindByNameAsync(string name, CancellationToken cancellationToken = default) | ||
{ | ||
return Query(c => c.Name == name).FirstOrDefaultAsync(cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.