Commit 23f45a3d authored by hasan khaddour's avatar hasan khaddour

Add Customer Domain Use Cases.

parent 1ac943e4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.Common
{
public record AddressDTO(
int StreetNumber,
int ZipCode ,
String StreetName ,
String City );
}
using PSManagement.Domain.Customers.ValueObjects;
using System;
namespace PSManagement.Application.Customers.Common
{
public class ContactInfoDTO
{
public String ConatctValue { get; set; }
public String ContactTpe { get; set; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.Common
{
public class CustomerDTO
{
public int Id { get; set; }
public String CustomerName { get; set; }
public AddressDTO Address { get; set; }
public String Email { get; set; }
public IEnumerable<ContactInfoDTO> ContactInfo { get; private set; }
}
}
using PSManagement.Domain.Customers.DomainEvents;
using PSManagement.SharedKernel.DomainEvents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.Events
{
public class CustomerCreatedEventHandler : IDomainEventHandler<CutsomerCreatedEvent>
{
public async Task Handle(CutsomerCreatedEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine("fdgfg");
}
}
}
using FluentResults;
using PSManagement.SharedKernel.CQRS.Command;
using PSManagement.SharedKernel.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PSManagement.Application.Customers.UseCases.Commands.AddContactInfo
{
public record AddContactInfoCommand(int CustomerId,String ContactType,String ContactValue ) : ICommand<Result>;
}
using FluentResults;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.DomainErrors;
using PSManagement.Domain.Customers.Entities;
using PSManagement.Domain.Customers.Repositories;
using PSManagement.SharedKernel.CQRS.Command;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.AddContactInfo
{
public class AddContactInfoCommandHandler : ICommandHandler<AddContactInfoCommand, Result>
{
private readonly ICustomersRepository _customersRepository;
public AddContactInfoCommandHandler(ICustomersRepository customersRepository)
{
_customersRepository = customersRepository;
}
public async Task<Result> Handle(AddContactInfoCommand request, CancellationToken cancellationToken)
{
Customer customer = await _customersRepository.GetByIdAsync(request.CustomerId);
if (customer is null) {
return Result.Fail(CustomerErrors.InvalidEntryError);
}
ContactInfo contact = new ContactInfo(request.ContactValue,request.ContactType);
customer.AddContactInfo(contact);
await _customersRepository.UpdateAsync(customer);
return Result.Ok();
}
}
}
using FluentResults;
using PSManagement.Application.Customers.Common;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.ValueObjects;
using PSManagement.SharedKernel.CQRS.Command;
......@@ -10,9 +11,10 @@ using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.CreateCustomer
{
public record CreateCustomerCommand (
String CustomerName ,
Address Address
public record CreateCustomerCommand(
string CustomerName,
string Email,
AddressDTO Address
) : ICommand<Result<int>>;
}
using FluentResults;
using AutoMapper;
using FluentResults;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.DomainEvents;
using PSManagement.Domain.Customers.Repositories;
using PSManagement.Domain.Customers.ValueObjects;
using PSManagement.SharedKernel.CQRS.Command;
using System;
using System.Collections.Generic;
......@@ -14,16 +17,22 @@ namespace PSManagement.Application.Customers.UseCases.Commands.CreateCustomer
public class CreateCustomerCommandHandler : ICommandHandler<CreateCustomerCommand, Result<int>>
{
private readonly ICustomersRepository _customerRepository;
public CreateCustomerCommandHandler(ICustomersRepository customerRepository)
private readonly IMapper _mapper;
public CreateCustomerCommandHandler(ICustomersRepository customerRepository, IMapper mapper)
{
_customerRepository = customerRepository;
_mapper = mapper;
}
public async Task<Result<int>> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
Customer customer = new Customer(request.CustomerName ,request.Address);
Customer customer = new (request.CustomerName ,_mapper.Map<Address>(request.Address),request.Email);
customer =await _customerRepository.AddAsync(customer);
customer.AddDomainEvent(new CutsomerCreatedEvent(customer.Id ,customer.CustomerName));
await _customerRepository.AddAsync(customer);
return Result.Ok(customer.Id);
}
}
......
using FluentResults;
using PSManagement.SharedKernel.CQRS.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer
{
public record DeleteCustomerCommand (int CustomerId) : ICommand<Result>;
}
using FluentResults;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.DomainErrors;
using PSManagement.Domain.Customers.Repositories;
using PSManagement.SharedKernel.CQRS.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer
{
public class DeleteCustomerCommandHandler : ICommandHandler<DeleteCustomerCommand, Result>
{
private readonly ICustomersRepository _customersRepository;
public DeleteCustomerCommandHandler(ICustomersRepository customersRepository)
{
_customersRepository = customersRepository;
}
public async Task<Result> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
{
Customer customer = await _customersRepository.GetByIdAsync(request.CustomerId);
if (customer is null)
{
return Result.Fail(CustomerErrors.InvalidEntryError);
}
await _customersRepository.DeleteAsync(customer);
return Result.Ok();
}
}
}
using FluentResults;
using PSManagement.Application.Customers.Common;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.ValueObjects;
using PSManagement.SharedKernel.CQRS.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer
{
public record UpdateCustomerCommand (
int CustomerId,
String CustomerName ,
String Email,
AddressDTO Address
) : ICommand<Result>;
}
using AutoMapper;
using FluentResults;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.DomainEvents;
using PSManagement.Domain.Customers.Repositories;
using PSManagement.Domain.Customers.ValueObjects;
using PSManagement.SharedKernel.CQRS.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer
{
public class UpdateCustomerCommandHandler : ICommandHandler<UpdateCustomerCommand, Result>
{
private readonly ICustomersRepository _customerRepository;
private readonly IMapper _mapper;
public UpdateCustomerCommandHandler(ICustomersRepository customerRepository, IMapper mapper)
{
_customerRepository = customerRepository;
_mapper = mapper;
}
public async Task<Result> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
{
Customer customer = new (request.CustomerName ,_mapper.Map<Address>(request.Address),request.Email);
customer.Id = request.CustomerId;
await _customerRepository.UpdateAsync(customer);
return Result.Ok();
}
}
}
......@@ -3,6 +3,8 @@ using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Contracts.Authentication;
using MediatR;
using System.Reflection;
using AutoMapper;
using PSManagement.Application.Mappers;
namespace PSManagement.Application.DI
{
......@@ -12,6 +14,8 @@ namespace PSManagement.Application.DI
{
//services.AddMediatR();
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddAutoMapper(typeof(CustomerMapperConfiguration));
return services;
}
......
using AutoMapper;
using PSManagement.Application.Customers.Common;
using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Customers.Entities;
using PSManagement.Domain.Customers.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Mappers
{
class CustomerMapperConfiguration :Profile
{
public CustomerMapperConfiguration()
{
CreateMap<AddressDTO, Address>().ReverseMap();
CreateMap<CustomerDTO, Customer>().ReverseMap();
CreateMap<ContactInfoDTO, ContactInfo>().ReverseMap();
}
}
}
......@@ -7,10 +7,13 @@
<ItemGroup>
<Folder Include="Abstraction\" />
<Folder Include="Behaviors\AuthorizationBehavior\" />
<Folder Include="Services\" />
<Folder Include="Customers\UseCases\Queries\ListAllCustomers\" />
<Folder Include="Customers\UseCases\Queries\GetCustomer\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" />
<PackageReference Include="MediatR" Version="5.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="5.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Contracts.Authentication
{
public record LoginRequest(
String Email ,
String PassWord
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment