83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
|
|
|
public class CreateBranchRequest
|
|
{
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string StreetAddress { get; set; } = string.Empty;
|
|
public string City { get; set; } = string.Empty;
|
|
public string StateProvince { get; set; } = string.Empty;
|
|
public string ZipCode { get; set; } = string.Empty;
|
|
public string Phone { get; set; } = string.Empty;
|
|
public string Email { get; set; } = string.Empty;
|
|
public string OtherInformation1 { get; set; } = string.Empty;
|
|
public string OtherInformation2 { get; set; } = string.Empty;
|
|
public string OtherInformation3 { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CreateBranchResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public record CreateBranchCommand(CreateBranchRequest Data) : IRequest<CreateBranchResponse>;
|
|
|
|
public class CreateBranchHandler : IRequestHandler<CreateBranchCommand, CreateBranchResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateBranchHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateBranchResponse> Handle(CreateBranchCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Branch
|
|
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Branch", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entityName = nameof(Data.Entities.Branch);
|
|
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.Branch
|
|
{
|
|
AutoNumber = autoNo,
|
|
Code = request.Data.Code,
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description,
|
|
StreetAddress = request.Data.StreetAddress,
|
|
City = request.Data.City,
|
|
StateProvince = request.Data.StateProvince,
|
|
ZipCode = request.Data.ZipCode,
|
|
Phone = request.Data.Phone,
|
|
Email = request.Data.Email,
|
|
OtherInformation1 = request.Data.OtherInformation1,
|
|
OtherInformation2 = request.Data.OtherInformation2,
|
|
OtherInformation3 = request.Data.OtherInformation3
|
|
};
|
|
|
|
_context.Branch.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateBranchResponse
|
|
{
|
|
Id = entity.Id,
|
|
Code = entity.Code
|
|
};
|
|
}
|
|
} |