initial commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerContactManager.Commands;
|
||||
|
||||
public class CreateCustomerContactResult
|
||||
{
|
||||
public CustomerContact? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCustomerContactRequest : IRequest<CreateCustomerContactResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateCustomerContactValidator : AbstractValidator<CreateCustomerContactRequest>
|
||||
{
|
||||
public CreateCustomerContactValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.JobTitle).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateCustomerContactHandler : IRequestHandler<CreateCustomerContactRequest, CreateCustomerContactResult>
|
||||
{
|
||||
private readonly ICommandRepository<CustomerContact> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateCustomerContactHandler(
|
||||
ICommandRepository<CustomerContact> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateCustomerContactResult> Handle(CreateCustomerContactRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new CustomerContact();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(CustomerContact), "", "CC");
|
||||
entity.JobTitle = request.JobTitle;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Description = request.Description;
|
||||
entity.CustomerId = request.CustomerId;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerContactResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerContactManager.Commands;
|
||||
|
||||
public class DeleteCustomerContactResult
|
||||
{
|
||||
public CustomerContact? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteCustomerContactRequest : IRequest<DeleteCustomerContactResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteCustomerContactValidator : AbstractValidator<DeleteCustomerContactRequest>
|
||||
{
|
||||
public DeleteCustomerContactValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteCustomerContactHandler : IRequestHandler<DeleteCustomerContactRequest, DeleteCustomerContactResult>
|
||||
{
|
||||
private readonly ICommandRepository<CustomerContact> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteCustomerContactHandler(
|
||||
ICommandRepository<CustomerContact> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteCustomerContactResult> Handle(DeleteCustomerContactRequest 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 DeleteCustomerContactResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CustomerContactManager.Commands;
|
||||
|
||||
public class UpdateCustomerContactResult
|
||||
{
|
||||
public CustomerContact? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCustomerContactRequest : IRequest<UpdateCustomerContactResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateCustomerContactValidator : AbstractValidator<UpdateCustomerContactRequest>
|
||||
{
|
||||
public UpdateCustomerContactValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.JobTitle).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateCustomerContactHandler : IRequestHandler<UpdateCustomerContactRequest, UpdateCustomerContactResult>
|
||||
{
|
||||
private readonly ICommandRepository<CustomerContact> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateCustomerContactHandler(
|
||||
ICommandRepository<CustomerContact> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateCustomerContactResult> Handle(UpdateCustomerContactRequest 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.JobTitle = request.JobTitle;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Description = request.Description;
|
||||
entity.CustomerId = request.CustomerId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerContactResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CustomerContactManager.Queries;
|
||||
|
||||
public record GetCustomerContactByCustomerIdListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; set; }
|
||||
public string? Number { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerContactByCustomerIdListProfile : Profile
|
||||
{
|
||||
public GetCustomerContactByCustomerIdListProfile()
|
||||
{
|
||||
CreateMap<CustomerContact, GetCustomerContactByCustomerIdListDto>()
|
||||
.ForMember(
|
||||
dest => dest.CustomerName,
|
||||
opt => opt.MapFrom(src => src.Customer != null ? src.Customer.Name : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCustomerContactByCustomerIdListResult
|
||||
{
|
||||
public List<GetCustomerContactByCustomerIdListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerContactByCustomerIdListRequest : IRequest<GetCustomerContactByCustomerIdListResult>
|
||||
{
|
||||
public string? CustomerId { get; init; }
|
||||
}
|
||||
|
||||
|
||||
public class GetCustomerContactByCustomerIdListHandler : IRequestHandler<GetCustomerContactByCustomerIdListRequest, GetCustomerContactByCustomerIdListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCustomerContactByCustomerIdListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerContactByCustomerIdListResult> Handle(GetCustomerContactByCustomerIdListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.CustomerContact
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(false)
|
||||
.Include(x => x.Customer)
|
||||
.Where(x => x.CustomerId == request.CustomerId)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetCustomerContactByCustomerIdListDto>>(entities);
|
||||
|
||||
return new GetCustomerContactByCustomerIdListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CustomerContactManager.Queries;
|
||||
|
||||
public record GetCustomerContactListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; set; }
|
||||
public string? Number { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerContactListProfile : Profile
|
||||
{
|
||||
public GetCustomerContactListProfile()
|
||||
{
|
||||
CreateMap<CustomerContact, GetCustomerContactListDto>()
|
||||
.ForMember(
|
||||
dest => dest.CustomerName,
|
||||
opt => opt.MapFrom(src => src.Customer != null ? src.Customer.Name : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCustomerContactListResult
|
||||
{
|
||||
public List<GetCustomerContactListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCustomerContactListRequest : IRequest<GetCustomerContactListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetCustomerContactListHandler : IRequestHandler<GetCustomerContactListRequest, GetCustomerContactListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCustomerContactListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerContactListResult> Handle(GetCustomerContactListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.CustomerContact
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.Include(x => x.Customer)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetCustomerContactListDto>>(entities);
|
||||
|
||||
return new GetCustomerContactListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user