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.Designation.Cqrs;
|
||||
|
||||
public class CreateDesignationRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Level { get; set; }
|
||||
public string OtherInformation1 { get; set; } = string.Empty;
|
||||
public string OtherInformation2 { get; set; } = string.Empty;
|
||||
public string OtherInformation3 { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CreateDesignationResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateDesignationCommand(CreateDesignationRequest Data) : IRequest<CreateDesignationResponse>;
|
||||
|
||||
public class CreateDesignationHandler : IRequestHandler<CreateDesignationCommand, CreateDesignationResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateDesignationHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateDesignationResponse> Handle(CreateDesignationCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Designation
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Designation", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Designation);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Designation
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
Level = request.Data.Level,
|
||||
OtherInformation1 = request.Data.OtherInformation1,
|
||||
OtherInformation2 = request.Data.OtherInformation2,
|
||||
OtherInformation3 = request.Data.OtherInformation3
|
||||
};
|
||||
|
||||
_context.Designation.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateDesignationResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Designation.Cqrs;
|
||||
|
||||
public class CreateDesignationValidator : AbstractValidator<CreateDesignationRequest>
|
||||
{
|
||||
public CreateDesignationValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Designation Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Designation Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Level)
|
||||
.NotEmpty().WithMessage("Level is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Designation.Cqrs;
|
||||
|
||||
public record DeleteDesignationByIdRequest(string Id);
|
||||
|
||||
public record DeleteDesignationByIdCommand(DeleteDesignationByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteDesignationByIdHandler : IRequestHandler<DeleteDesignationByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteDesignationByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteDesignationByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Designation
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Designation.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.Designation.Cqrs;
|
||||
|
||||
public class GetDesignationByIdResponse
|
||||
{
|
||||
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? Level { 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 GetDesignationByIdQuery(string Id) : IRequest<GetDesignationByIdResponse?>;
|
||||
|
||||
public class GetDesignationByIdHandler : IRequestHandler<GetDesignationByIdQuery, GetDesignationByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetDesignationByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetDesignationByIdResponse?> Handle(GetDesignationByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Designation
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetDesignationByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Level = x.Level,
|
||||
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.Designation.Cqrs;
|
||||
|
||||
public class GetDesignationListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Level { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetDesignationListQuery() : IRequest<List<GetDesignationListResponse>>;
|
||||
|
||||
public class GetDesignationListHandler : IRequestHandler<GetDesignationListQuery, List<GetDesignationListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetDesignationListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetDesignationListResponse>> Handle(GetDesignationListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Designation
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetDesignationListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Level = x.Level,
|
||||
Description = x.Description,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Designation.Cqrs;
|
||||
|
||||
public class UpdateDesignationRequest : CreateDesignationRequest
|
||||
{
|
||||
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 UpdateDesignationResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateDesignationCommand(UpdateDesignationRequest Data) : IRequest<UpdateDesignationResponse>;
|
||||
|
||||
public class UpdateDesignationHandler : IRequestHandler<UpdateDesignationCommand, UpdateDesignationResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateDesignationHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateDesignationResponse> Handle(UpdateDesignationCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Designation
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return new UpdateDesignationResponse { Id = request.Data.Id, Success = false };
|
||||
|
||||
var isExists = await _context.Designation
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Designation", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.Level = request.Data.Level;
|
||||
entity.OtherInformation1 = request.Data.OtherInformation1;
|
||||
entity.OtherInformation2 = request.Data.OtherInformation2;
|
||||
entity.OtherInformation3 = request.Data.OtherInformation3;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateDesignationResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.Designation.Cqrs;
|
||||
|
||||
public class UpdateDesignationValidator : AbstractValidator<UpdateDesignationRequest>
|
||||
{
|
||||
public UpdateDesignationValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Designation Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Designation Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Level)
|
||||
.NotEmpty().WithMessage("Level is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user