initial commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class CreateWarehouseRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? SystemWarehouse { get; set; } = false;
|
||||
}
|
||||
|
||||
public class CreateWarehouseResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateWarehouseCommand(CreateWarehouseRequest Data) : IRequest<CreateWarehouseResponse>;
|
||||
|
||||
public class CreateWarehouseHandler : IRequestHandler<CreateWarehouseCommand, CreateWarehouseResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateWarehouseHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateWarehouseResponse> Handle(CreateWarehouseCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Warehouse
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Warehouse", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.Warehouse
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
SystemWarehouse = request.Data.SystemWarehouse
|
||||
};
|
||||
|
||||
_context.Warehouse.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateWarehouseResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class CreateWarehouseValidator : AbstractValidator<CreateWarehouseRequest>
|
||||
{
|
||||
public CreateWarehouseValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Warehouse Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public record DeleteWarehouseByIdRequest(string Id);
|
||||
|
||||
public record DeleteWarehouseByIdCommand(DeleteWarehouseByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteWarehouseByIdHandler : IRequestHandler<DeleteWarehouseByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteWarehouseByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteWarehouseByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Warehouse
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
if (entity.SystemWarehouse == true)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_context.Warehouse.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class GetWarehouseByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? SystemWarehouse { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetWarehouseByIdQuery(string Id) : IRequest<GetWarehouseByIdResponse?>;
|
||||
|
||||
public class GetWarehouseByIdHandler : IRequestHandler<GetWarehouseByIdQuery, GetWarehouseByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetWarehouseByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetWarehouseByIdResponse?> Handle(GetWarehouseByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Warehouse
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetWarehouseByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
SystemWarehouse = x.SystemWarehouse,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class GetWarehouseListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? SystemWarehouse { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetWarehouseListQuery() : IRequest<List<GetWarehouseListResponse>>;
|
||||
|
||||
public class GetWarehouseListHandler : IRequestHandler<GetWarehouseListQuery, List<GetWarehouseListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetWarehouseListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetWarehouseListResponse>> Handle(GetWarehouseListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Warehouse
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetWarehouseListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
SystemWarehouse = x.SystemWarehouse,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class UpdateWarehouseRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? SystemWarehouse { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateWarehouseResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateWarehouseCommand(UpdateWarehouseRequest Data) : IRequest<UpdateWarehouseResponse>;
|
||||
|
||||
public class UpdateWarehouseHandler : IRequestHandler<UpdateWarehouseCommand, UpdateWarehouseResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateWarehouseHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateWarehouseResponse> Handle(UpdateWarehouseCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Warehouse
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Warehouse", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Warehouse
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateWarehouseResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.SystemWarehouse = request.Data.SystemWarehouse;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateWarehouseResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Warehouse.Cqrs;
|
||||
|
||||
public class UpdateWarehouseValidator : AbstractValidator<UpdateWarehouseRequest>
|
||||
{
|
||||
public UpdateWarehouseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Warehouse Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user