initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public class CreateBranchValidator : AbstractValidator<CreateBranchRequest>
|
||||
{
|
||||
public CreateBranchValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Branch Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Branch Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.City)
|
||||
.NotEmpty().WithMessage("City is required");
|
||||
|
||||
RuleFor(x => x.StreetAddress)
|
||||
.NotEmpty().WithMessage("Street Address is required");
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.EmailAddress().When(x => !string.IsNullOrEmpty(x.Email));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public record DeleteBranchByIdRequest(string Id);
|
||||
|
||||
public record DeleteBranchByIdCommand(DeleteBranchByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteBranchByIdHandler : IRequestHandler<DeleteBranchByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteBranchByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteBranchByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Branch
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Branch.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public class GetBranchByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { 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? 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 GetBranchByIdQuery(string Id) : IRequest<GetBranchByIdResponse?>;
|
||||
|
||||
public class GetBranchByIdHandler : IRequestHandler<GetBranchByIdQuery, GetBranchByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBranchByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetBranchByIdResponse?> Handle(GetBranchByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Branch
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetBranchByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
StreetAddress = x.StreetAddress,
|
||||
City = x.City,
|
||||
StateProvince = x.StateProvince,
|
||||
ZipCode = x.ZipCode,
|
||||
Phone = x.Phone,
|
||||
Email = x.Email,
|
||||
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,49 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public class GetBranchListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? StreetAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetBranchListQuery() : IRequest<List<GetBranchListResponse>>;
|
||||
|
||||
public class GetBranchListHandler : IRequestHandler<GetBranchListQuery, List<GetBranchListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBranchListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetBranchListResponse>> Handle(GetBranchListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Branch
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetBranchListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
City = x.City,
|
||||
Phone = x.Phone,
|
||||
Email = x.Email,
|
||||
StreetAddress = x.StreetAddress,
|
||||
Description = x.Description,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public class UpdateBranchRequest : CreateBranchRequest
|
||||
{
|
||||
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 UpdateBranchResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateBranchCommand(UpdateBranchRequest Data) : IRequest<UpdateBranchResponse>;
|
||||
|
||||
public class UpdateBranchHandler : IRequestHandler<UpdateBranchCommand, UpdateBranchResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateBranchHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateBranchResponse> Handle(UpdateBranchCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Branch
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Branch", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Branch
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateBranchResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.StreetAddress = request.Data.StreetAddress;
|
||||
entity.City = request.Data.City;
|
||||
entity.StateProvince = request.Data.StateProvince;
|
||||
entity.ZipCode = request.Data.ZipCode;
|
||||
entity.Phone = request.Data.Phone;
|
||||
entity.Email = request.Data.Email;
|
||||
entity.OtherInformation1 = request.Data.OtherInformation1;
|
||||
entity.OtherInformation2 = request.Data.OtherInformation2;
|
||||
entity.OtherInformation3 = request.Data.OtherInformation3;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateBranchResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Branch.Cqrs;
|
||||
|
||||
public class UpdateBranchValidator : AbstractValidator<UpdateBranchRequest>
|
||||
{
|
||||
public UpdateBranchValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Branch Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Branch Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.City)
|
||||
.NotEmpty().WithMessage("City is required");
|
||||
|
||||
RuleFor(x => x.StreetAddress)
|
||||
.NotEmpty().WithMessage("Street Address is required");
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.EmailAddress().When(x => !string.IsNullOrEmpty(x.Email));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user