initial commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class CreateCompanyRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool IsDefault { get; set; }
|
||||
public string? CurrencyId { get; set; }
|
||||
public string? TaxIdentification { get; set; }
|
||||
public string? BusinessLicense { get; set; }
|
||||
public string? CompanyLogo { get; set; }
|
||||
public string? StreetAddress { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? StateProvince { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? SocialMediaLinkedIn { get; set; }
|
||||
public string? SocialMediaX { get; set; }
|
||||
public string? SocialMediaFacebook { get; set; }
|
||||
public string? SocialMediaInstagram { get; set; }
|
||||
public string? SocialMediaTikTok { get; set; }
|
||||
public string? OtherInformation1 { get; set; }
|
||||
public string? OtherInformation2 { get; set; }
|
||||
public string? OtherInformation3 { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCompanyResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateCompanyCommand(CreateCompanyRequest Data) : IRequest<CreateCompanyResponse>;
|
||||
|
||||
public class CreateCompanyHandler : IRequestHandler<CreateCompanyCommand, CreateCompanyResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateCompanyHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateCompanyResponse> Handle(CreateCompanyCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Company
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Company", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
if (request.Data.IsDefault)
|
||||
{
|
||||
var existingDefaults = await _context.Company
|
||||
.Where(x => x.IsDefault)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var comp in existingDefaults)
|
||||
{
|
||||
comp.IsDefault = false;
|
||||
}
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Company);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Company
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Name = request.Data.Name ?? string.Empty,
|
||||
Description = request.Data.Description ?? string.Empty,
|
||||
IsDefault = request.Data.IsDefault,
|
||||
CurrencyId = request.Data.CurrencyId ?? string.Empty,
|
||||
TaxIdentification = request.Data.TaxIdentification ?? string.Empty,
|
||||
BusinessLicense = request.Data.BusinessLicense ?? string.Empty,
|
||||
StreetAddress = request.Data.StreetAddress ?? string.Empty,
|
||||
City = request.Data.City ?? string.Empty,
|
||||
StateProvince = request.Data.StateProvince ?? string.Empty,
|
||||
ZipCode = request.Data.ZipCode ?? string.Empty,
|
||||
Phone = request.Data.Phone ?? string.Empty,
|
||||
Email = request.Data.Email ?? string.Empty,
|
||||
SocialMediaLinkedIn = request.Data.SocialMediaLinkedIn ?? string.Empty,
|
||||
SocialMediaX = request.Data.SocialMediaX ?? string.Empty,
|
||||
SocialMediaFacebook = request.Data.SocialMediaFacebook ?? string.Empty,
|
||||
SocialMediaInstagram = request.Data.SocialMediaInstagram ?? string.Empty,
|
||||
SocialMediaTikTok = request.Data.SocialMediaTikTok ?? string.Empty,
|
||||
OtherInformation1 = request.Data.OtherInformation1 ?? string.Empty,
|
||||
OtherInformation2 = request.Data.OtherInformation2 ?? string.Empty,
|
||||
OtherInformation3 = request.Data.OtherInformation3 ?? string.Empty
|
||||
};
|
||||
|
||||
_context.Company.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCompanyResponse { Id = entity.Id, Name = entity.Name };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class CreateCompanyValidator : AbstractValidator<CreateCompanyRequest>
|
||||
{
|
||||
public CreateCompanyValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Company Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CurrencyId)
|
||||
.NotEmpty().WithMessage("Default Currency is required");
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required")
|
||||
.EmailAddress().WithMessage("Invalid email format");
|
||||
|
||||
RuleFor(x => x.Phone)
|
||||
.NotEmpty().WithMessage("Phone number is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public record DeleteCompanyByIdRequest(string Id);
|
||||
public record DeleteCompanyByIdCommand(DeleteCompanyByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteCompanyByIdHandler : IRequestHandler<DeleteCompanyByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteCompanyByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteCompanyByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Company
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
if (entity.IsDefault) return false;
|
||||
|
||||
var totalCount = await _context.Company.CountAsync(cancellationToken);
|
||||
if (totalCount <= 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_context.Company.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class GetCompanyByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool IsDefault { get; set; }
|
||||
public string? CurrencyId { get; set; }
|
||||
public string? TaxIdentification { get; set; }
|
||||
public string? BusinessLicense { get; set; }
|
||||
public string? CompanyLogo { get; set; }
|
||||
public string? StreetAddress { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? StateProvince { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? SocialMediaLinkedIn { get; set; }
|
||||
public string? SocialMediaX { get; set; }
|
||||
public string? SocialMediaFacebook { get; set; }
|
||||
public string? SocialMediaInstagram { get; set; }
|
||||
public string? SocialMediaTikTok { get; set; }
|
||||
public string? OtherInformation1 { get; set; }
|
||||
public string? OtherInformation2 { get; set; }
|
||||
public string? OtherInformation3 { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetCompanyByIdQuery(string Id) : IRequest<GetCompanyByIdResponse?>;
|
||||
|
||||
public class GetCompanyByIdHandler : IRequestHandler<GetCompanyByIdQuery, GetCompanyByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCompanyByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetCompanyByIdResponse?> Handle(GetCompanyByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Company
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetCompanyByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
IsDefault = x.IsDefault,
|
||||
CurrencyId = x.CurrencyId,
|
||||
TaxIdentification = x.TaxIdentification,
|
||||
BusinessLicense = x.BusinessLicense,
|
||||
CompanyLogo = x.CompanyLogo,
|
||||
StreetAddress = x.StreetAddress,
|
||||
City = x.City,
|
||||
StateProvince = x.StateProvince,
|
||||
ZipCode = x.ZipCode,
|
||||
Phone = x.Phone,
|
||||
Email = x.Email,
|
||||
SocialMediaLinkedIn = x.SocialMediaLinkedIn,
|
||||
SocialMediaX = x.SocialMediaX,
|
||||
SocialMediaFacebook = x.SocialMediaFacebook,
|
||||
SocialMediaInstagram = x.SocialMediaInstagram,
|
||||
SocialMediaTikTok = x.SocialMediaTikTok,
|
||||
OtherInformation1 = x.OtherInformation1,
|
||||
OtherInformation2 = x.OtherInformation2,
|
||||
OtherInformation3 = x.OtherInformation3,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class GetCompanyListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public string? City { get; set; }
|
||||
public bool IsDefault { get; set; }
|
||||
}
|
||||
|
||||
public record GetCompanyListQuery() : IRequest<List<GetCompanyListResponse>>;
|
||||
|
||||
public class GetCompanyListHandler : IRequestHandler<GetCompanyListQuery, List<GetCompanyListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCompanyListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetCompanyListResponse>> Handle(GetCompanyListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Company
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetCompanyListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Email = x.Email,
|
||||
Phone = x.Phone,
|
||||
City = x.City,
|
||||
IsDefault = x.IsDefault
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class UpdateCompanyRequest : CreateCompanyRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCompanyResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateCompanyCommand(UpdateCompanyRequest Data) : IRequest<UpdateCompanyResponse>;
|
||||
|
||||
public class UpdateCompanyHandler : IRequestHandler<UpdateCompanyCommand, UpdateCompanyResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateCompanyHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateCompanyResponse> Handle(UpdateCompanyCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Company
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return new UpdateCompanyResponse { Id = request.Data.Id, Success = false };
|
||||
|
||||
var isNameExists = await _context.Company
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isNameExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Company", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
if (entity.IsDefault && !request.Data.IsDefault)
|
||||
{
|
||||
var totalCount = await _context.Company.CountAsync(cancellationToken);
|
||||
|
||||
if (totalCount <= 1)
|
||||
{
|
||||
request.Data.IsDefault = true;
|
||||
throw new Exception("At least one company must be set as default.");
|
||||
}
|
||||
}
|
||||
|
||||
if (request.Data.IsDefault)
|
||||
{
|
||||
var otherDefaults = await _context.Company
|
||||
.Where(x => x.IsDefault && x.Id != entity.Id)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var comp in otherDefaults)
|
||||
{
|
||||
comp.IsDefault = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var anyOtherDefault = await _context.Company
|
||||
.AnyAsync(x => x.IsDefault && x.Id != entity.Id, cancellationToken);
|
||||
|
||||
if (!anyOtherDefault)
|
||||
{
|
||||
request.Data.IsDefault = true;
|
||||
}
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name ?? string.Empty;
|
||||
entity.Description = request.Data.Description ?? string.Empty;
|
||||
entity.IsDefault = request.Data.IsDefault;
|
||||
entity.CurrencyId = request.Data.CurrencyId ?? string.Empty;
|
||||
entity.TaxIdentification = request.Data.TaxIdentification ?? string.Empty;
|
||||
entity.BusinessLicense = request.Data.BusinessLicense ?? string.Empty;
|
||||
entity.StreetAddress = request.Data.StreetAddress ?? string.Empty;
|
||||
entity.City = request.Data.City ?? string.Empty;
|
||||
entity.StateProvince = request.Data.StateProvince ?? string.Empty;
|
||||
entity.ZipCode = request.Data.ZipCode ?? string.Empty;
|
||||
entity.Phone = request.Data.Phone ?? string.Empty;
|
||||
entity.Email = request.Data.Email ?? string.Empty;
|
||||
entity.SocialMediaLinkedIn = request.Data.SocialMediaLinkedIn ?? string.Empty;
|
||||
entity.SocialMediaX = request.Data.SocialMediaX ?? string.Empty;
|
||||
entity.SocialMediaFacebook = request.Data.SocialMediaFacebook ?? string.Empty;
|
||||
entity.SocialMediaInstagram = request.Data.SocialMediaInstagram ?? string.Empty;
|
||||
entity.SocialMediaTikTok = request.Data.SocialMediaTikTok ?? string.Empty;
|
||||
entity.OtherInformation1 = request.Data.OtherInformation1 ?? string.Empty;
|
||||
entity.OtherInformation2 = request.Data.OtherInformation2 ?? string.Empty;
|
||||
entity.OtherInformation3 = request.Data.OtherInformation3 ?? string.Empty;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCompanyResponse { Id = entity.Id, Success = true };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Company.Cqrs;
|
||||
|
||||
public class UpdateCompanyValidator : AbstractValidator<UpdateCompanyRequest>
|
||||
{
|
||||
public UpdateCompanyValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty().WithMessage("ID is required for update");
|
||||
RuleFor(x => x.Name).NotEmpty().MaximumLength(GlobalConsts.StringLengthShort);
|
||||
RuleFor(x => x.CurrencyId).NotEmpty();
|
||||
RuleFor(x => x.Email).NotEmpty().EmailAddress();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user