97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Thirdparty.Customer.Cqrs;
|
|
|
|
public class CreateCustomerRequest
|
|
{
|
|
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 class CreateCustomerResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Name { get; set; }
|
|
}
|
|
|
|
public record CreateCustomerCommand(CreateCustomerRequest Data) : IRequest<CreateCustomerResponse>;
|
|
|
|
public class CreateCustomerHandler : IRequestHandler<CreateCustomerCommand, CreateCustomerResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateCustomerHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateCustomerResponse> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Customer
|
|
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Customer", request.Data.Name ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Customer);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Customer
|
|
{
|
|
AutoNumber = autoNo,
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description,
|
|
Street = request.Data.Street,
|
|
City = request.Data.City,
|
|
State = request.Data.State,
|
|
ZipCode = request.Data.ZipCode,
|
|
Country = request.Data.Country,
|
|
PhoneNumber = request.Data.PhoneNumber,
|
|
FaxNumber = request.Data.FaxNumber,
|
|
EmailAddress = request.Data.EmailAddress,
|
|
Website = request.Data.Website,
|
|
WhatsApp = request.Data.WhatsApp,
|
|
LinkedIn = request.Data.LinkedIn,
|
|
Facebook = request.Data.Facebook,
|
|
Instagram = request.Data.Instagram,
|
|
TwitterX = request.Data.TwitterX,
|
|
TikTok = request.Data.TikTok,
|
|
CustomerGroupId = request.Data.CustomerGroupId,
|
|
CustomerCategoryId = request.Data.CustomerCategoryId
|
|
};
|
|
|
|
_context.Customer.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateCustomerResponse
|
|
{
|
|
Id = entity.Id,
|
|
Name = entity.Name
|
|
};
|
|
}
|
|
} |