initial commit
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerManager.Commands;
|
||||
|
||||
public class CreateCustomerResult
|
||||
{
|
||||
public Customer? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCustomerRequest : IRequest<CreateCustomerResult>
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public string? WhatsApp { get; set; }
|
||||
public string? LinkedIn { get; set; }
|
||||
public string? Facebook { get; set; }
|
||||
public string? Instagram { get; set; }
|
||||
public string? TwitterX { get; set; }
|
||||
public string? TikTok { get; set; }
|
||||
public string? CustomerGroupId { get; set; }
|
||||
public string? CustomerCategoryId { get; set; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateCustomerValidator : AbstractValidator<CreateCustomerRequest>
|
||||
{
|
||||
public CreateCustomerValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.Street).NotEmpty();
|
||||
RuleFor(x => x.City).NotEmpty();
|
||||
RuleFor(x => x.State).NotEmpty();
|
||||
RuleFor(x => x.ZipCode).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
RuleFor(x => x.CustomerGroupId).NotEmpty();
|
||||
RuleFor(x => x.CustomerCategoryId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateCustomerHandler : IRequestHandler<CreateCustomerRequest, CreateCustomerResult>
|
||||
{
|
||||
private readonly ICommandRepository<Customer> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateCustomerHandler(
|
||||
ICommandRepository<Customer> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateCustomerResult> Handle(CreateCustomerRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Customer();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(Customer), "", "CST");
|
||||
entity.Description = request.Description;
|
||||
entity.Street = request.Street;
|
||||
entity.City = request.City;
|
||||
entity.State = request.State;
|
||||
entity.ZipCode = request.ZipCode;
|
||||
entity.Country = request.Country;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.FaxNumber = request.FaxNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Website = request.Website;
|
||||
entity.WhatsApp = request.WhatsApp;
|
||||
entity.LinkedIn = request.LinkedIn;
|
||||
entity.Facebook = request.Facebook;
|
||||
entity.Instagram = request.Instagram;
|
||||
entity.TwitterX = request.TwitterX;
|
||||
entity.TikTok = request.TikTok;
|
||||
entity.CustomerGroupId = request.CustomerGroupId;
|
||||
entity.CustomerCategoryId = request.CustomerCategoryId;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerManager.Commands;
|
||||
|
||||
public class DeleteCustomerResult
|
||||
{
|
||||
public Customer? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteCustomerRequest : IRequest<DeleteCustomerResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteCustomerValidator : AbstractValidator<DeleteCustomerRequest>
|
||||
{
|
||||
public DeleteCustomerValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteCustomerHandler : IRequestHandler<DeleteCustomerRequest, DeleteCustomerResult>
|
||||
{
|
||||
private readonly ICommandRepository<Customer> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteCustomerHandler(
|
||||
ICommandRepository<Customer> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteCustomerResult> Handle(DeleteCustomerRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeleteCustomerResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerManager.Commands;
|
||||
|
||||
public class UpdateCustomerResult
|
||||
{
|
||||
public Customer? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCustomerRequest : IRequest<UpdateCustomerResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public string? WhatsApp { get; set; }
|
||||
public string? LinkedIn { get; set; }
|
||||
public string? Facebook { get; set; }
|
||||
public string? Instagram { get; set; }
|
||||
public string? TwitterX { get; set; }
|
||||
public string? TikTok { get; set; }
|
||||
public string? CustomerGroupId { get; set; }
|
||||
public string? CustomerCategoryId { get; set; }
|
||||
public string? CreatedById { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateCustomerValidator : AbstractValidator<UpdateCustomerRequest>
|
||||
{
|
||||
public UpdateCustomerValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.Street).NotEmpty();
|
||||
RuleFor(x => x.City).NotEmpty();
|
||||
RuleFor(x => x.State).NotEmpty();
|
||||
RuleFor(x => x.ZipCode).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
RuleFor(x => x.CustomerGroupId).NotEmpty();
|
||||
RuleFor(x => x.CustomerCategoryId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateCustomerHandler : IRequestHandler<UpdateCustomerRequest, UpdateCustomerResult>
|
||||
{
|
||||
private readonly ICommandRepository<Customer> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateCustomerHandler(
|
||||
ICommandRepository<Customer> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateCustomerResult> Handle(UpdateCustomerRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
entity.Street = request.Street;
|
||||
entity.City = request.City;
|
||||
entity.State = request.State;
|
||||
entity.ZipCode = request.ZipCode;
|
||||
entity.Country = request.Country;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.FaxNumber = request.FaxNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Website = request.Website;
|
||||
entity.WhatsApp = request.WhatsApp;
|
||||
entity.LinkedIn = request.LinkedIn;
|
||||
entity.Facebook = request.Facebook;
|
||||
entity.Instagram = request.Instagram;
|
||||
entity.TwitterX = request.TwitterX;
|
||||
entity.TikTok = request.TikTok;
|
||||
entity.CustomerGroupId = request.CustomerGroupId;
|
||||
entity.CustomerCategoryId = request.CustomerCategoryId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CustomerManager.Queries;
|
||||
|
||||
public record GetCustomerListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; set; }
|
||||
public string? Number { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public string? WhatsApp { get; set; }
|
||||
public string? LinkedIn { get; set; }
|
||||
public string? Facebook { get; set; }
|
||||
public string? Instagram { get; set; }
|
||||
public string? TwitterX { get; set; }
|
||||
public string? TikTok { get; set; }
|
||||
public string? CustomerGroupId { get; set; }
|
||||
public string? CustomerGroupName { get; set; }
|
||||
public string? CustomerCategoryId { get; set; }
|
||||
public string? CustomerCategoryName { get; set; }
|
||||
public string? CreatedById { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerListProfile : Profile
|
||||
{
|
||||
public GetCustomerListProfile()
|
||||
{
|
||||
CreateMap<Customer, GetCustomerListDto>()
|
||||
.ForMember(
|
||||
dest => dest.CustomerGroupName,
|
||||
opt => opt.MapFrom(src => src.CustomerGroup != null ? src.CustomerGroup.Name : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.CustomerCategoryName,
|
||||
opt => opt.MapFrom(src => src.CustomerCategory != null ? src.CustomerCategory.Name : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCustomerListResult
|
||||
{
|
||||
public List<GetCustomerListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerListRequest : IRequest<GetCustomerListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetCustomerListHandler : IRequestHandler<GetCustomerListRequest, GetCustomerListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCustomerListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerListResult> Handle(GetCustomerListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Customer
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.Include(x => x.CustomerGroup)
|
||||
.Include(x => x.CustomerCategory)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetCustomerListDto>>(entities);
|
||||
|
||||
return new GetCustomerListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user