initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class CreateDepartmentRequest
|
||||
{
|
||||
public string? CostCenter { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string HeadOfDeptartment { 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 CreateDepartmentResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? CostCenter { get; set; }
|
||||
}
|
||||
|
||||
public record CreateDepartmentCommand(CreateDepartmentRequest Data) : IRequest<CreateDepartmentResponse>;
|
||||
|
||||
public class CreateDepartmentHandler : IRequestHandler<CreateDepartmentCommand, CreateDepartmentResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateDepartmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateDepartmentResponse> Handle(CreateDepartmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Department
|
||||
.AnyAsync(x => x.CostCenter == request.Data.CostCenter, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Department", request.Data.CostCenter ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Department);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Department
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
CostCenter = request.Data.CostCenter,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
HeadOfDeptartment = request.Data.HeadOfDeptartment,
|
||||
OtherInformation1 = request.Data.OtherInformation1,
|
||||
OtherInformation2 = request.Data.OtherInformation2,
|
||||
OtherInformation3 = request.Data.OtherInformation3
|
||||
};
|
||||
|
||||
_context.Department.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateDepartmentResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
CostCenter = entity.CostCenter
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class CreateDepartmentValidator : AbstractValidator<CreateDepartmentRequest>
|
||||
{
|
||||
public CreateDepartmentValidator()
|
||||
{
|
||||
RuleFor(x => x.CostCenter)
|
||||
.NotEmpty().WithMessage("Cost Center is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Department Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.HeadOfDeptartment)
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public record DeleteDepartmentByIdRequest(string Id);
|
||||
public record DeleteDepartmentByIdCommand(DeleteDepartmentByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteDepartmentByIdHandler : IRequestHandler<DeleteDepartmentByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteDepartmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteDepartmentByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Department
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Department.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class GetDepartmentByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? CostCenter { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? HeadOfDeptartment { 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 GetDepartmentByIdQuery(string Id) : IRequest<GetDepartmentByIdResponse?>;
|
||||
|
||||
public class GetDepartmentByIdHandler : IRequestHandler<GetDepartmentByIdQuery, GetDepartmentByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetDepartmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetDepartmentByIdResponse?> Handle(GetDepartmentByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Department
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetDepartmentByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
CostCenter = x.CostCenter,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
HeadOfDeptartment = x.HeadOfDeptartment,
|
||||
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,43 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class GetDepartmentListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? CostCenter { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? HeadOfDeptartment { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetDepartmentListQuery() : IRequest<List<GetDepartmentListResponse>>;
|
||||
|
||||
public class GetDepartmentListHandler : IRequestHandler<GetDepartmentListQuery, List<GetDepartmentListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetDepartmentListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetDepartmentListResponse>> Handle(GetDepartmentListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Department
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.OrderBy(x => x.CostCenter)
|
||||
.Select(x => new GetDepartmentListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
CostCenter = x.CostCenter,
|
||||
Name = x.Name,
|
||||
HeadOfDeptartment = x.HeadOfDeptartment,
|
||||
Description = x.Description,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class UpdateDepartmentRequest : CreateDepartmentRequest
|
||||
{
|
||||
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 UpdateDepartmentResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateDepartmentCommand(UpdateDepartmentRequest Data) : IRequest<UpdateDepartmentResponse>;
|
||||
|
||||
public class UpdateDepartmentHandler : IRequestHandler<UpdateDepartmentCommand, UpdateDepartmentResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateDepartmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateDepartmentResponse> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Department
|
||||
.AnyAsync(x => x.CostCenter == request.Data.CostCenter && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Department", request.Data.CostCenter ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Department
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateDepartmentResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.CostCenter = request.Data.CostCenter;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.HeadOfDeptartment = request.Data.HeadOfDeptartment;
|
||||
entity.OtherInformation1 = request.Data.OtherInformation1;
|
||||
entity.OtherInformation2 = request.Data.OtherInformation2;
|
||||
entity.OtherInformation3 = request.Data.OtherInformation3;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateDepartmentResponse { Id = entity.Id, Success = true };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class UpdateDepartmentValidator : AbstractValidator<UpdateDepartmentRequest>
|
||||
{
|
||||
public UpdateDepartmentValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty().WithMessage("ID is required");
|
||||
RuleFor(x => x.CostCenter).NotEmpty().MaximumLength(GlobalConsts.StringLengthShort);
|
||||
RuleFor(x => x.Name).NotEmpty().MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user