initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class CreateUnitMeasureRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateUnitMeasureResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateUnitMeasureCommand(CreateUnitMeasureRequest Data) : IRequest<CreateUnitMeasureResponse>;
|
||||
|
||||
public class CreateUnitMeasureHandler : IRequestHandler<CreateUnitMeasureCommand, CreateUnitMeasureResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateUnitMeasureHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateUnitMeasureResponse> Handle(CreateUnitMeasureCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.UnitMeasure
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Unit of Measure", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.UnitMeasure
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.UnitMeasure.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateUnitMeasureResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class CreateUnitMeasureValidator : AbstractValidator<CreateUnitMeasureRequest>
|
||||
{
|
||||
public CreateUnitMeasureValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Unit Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public record DeleteUnitMeasureByIdRequest(string Id);
|
||||
|
||||
public record DeleteUnitMeasureByIdCommand(DeleteUnitMeasureByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteUnitMeasureByIdHandler : IRequestHandler<DeleteUnitMeasureByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteUnitMeasureByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteUnitMeasureByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UnitMeasure
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.UnitMeasure.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class GetUnitMeasureByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetUnitMeasureByIdQuery(string Id) : IRequest<GetUnitMeasureByIdResponse?>;
|
||||
|
||||
public class GetUnitMeasureByIdHandler : IRequestHandler<GetUnitMeasureByIdQuery, GetUnitMeasureByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetUnitMeasureByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetUnitMeasureByIdResponse?> Handle(GetUnitMeasureByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.UnitMeasure
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetUnitMeasureByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class GetUnitMeasureListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetUnitMeasureListQuery() : IRequest<List<GetUnitMeasureListResponse>>;
|
||||
|
||||
public class GetUnitMeasureListHandler : IRequestHandler<GetUnitMeasureListQuery, List<GetUnitMeasureListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetUnitMeasureListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetUnitMeasureListResponse>> Handle(GetUnitMeasureListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.UnitMeasure
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetUnitMeasureListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class UpdateUnitMeasureRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateUnitMeasureResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateUnitMeasureCommand(UpdateUnitMeasureRequest Data) : IRequest<UpdateUnitMeasureResponse>;
|
||||
|
||||
public class UpdateUnitMeasureHandler : IRequestHandler<UpdateUnitMeasureCommand, UpdateUnitMeasureResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateUnitMeasureHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateUnitMeasureResponse> Handle(UpdateUnitMeasureCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.UnitMeasure
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Unit of Measure", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.UnitMeasure
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateUnitMeasureResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateUnitMeasureResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Inventory.UnitMeasure.Cqrs;
|
||||
|
||||
public class UpdateUnitMeasureValidator : AbstractValidator<UpdateUnitMeasureRequest>
|
||||
{
|
||||
public UpdateUnitMeasureValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Unit Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user