initial commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.WarehouseManager.Commands;
|
||||
|
||||
public class CreateWarehouseResult
|
||||
{
|
||||
public Warehouse? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateWarehouseRequest : IRequest<CreateWarehouseResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateWarehouseValidator : AbstractValidator<CreateWarehouseRequest>
|
||||
{
|
||||
public CreateWarehouseValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateWarehouseHandler : IRequestHandler<CreateWarehouseRequest, CreateWarehouseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Warehouse> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public CreateWarehouseHandler(
|
||||
ICommandRepository<Warehouse> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<CreateWarehouseResult> Handle(CreateWarehouseRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Warehouse();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.SystemWarehouse = false;
|
||||
entity.Description = request.Description;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateWarehouseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.WarehouseManager.Commands;
|
||||
|
||||
public class DeleteWarehouseResult
|
||||
{
|
||||
public Warehouse? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteWarehouseRequest : IRequest<DeleteWarehouseResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteWarehouseValidator : AbstractValidator<DeleteWarehouseRequest>
|
||||
{
|
||||
public DeleteWarehouseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteWarehouseHandler : IRequestHandler<DeleteWarehouseRequest, DeleteWarehouseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Warehouse> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteWarehouseHandler(
|
||||
ICommandRepository<Warehouse> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteWarehouseResult> Handle(DeleteWarehouseRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
if (entity.SystemWarehouse == true)
|
||||
{
|
||||
throw new Exception($"Updating system warehouse is not allowed.");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeleteWarehouseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.WarehouseManager.Commands;
|
||||
|
||||
public class UpdateWarehouseResult
|
||||
{
|
||||
public Warehouse? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateWarehouseRequest : IRequest<UpdateWarehouseResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateWarehouseValidator : AbstractValidator<UpdateWarehouseRequest>
|
||||
{
|
||||
public UpdateWarehouseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateWarehouseHandler : IRequestHandler<UpdateWarehouseRequest, UpdateWarehouseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Warehouse> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateWarehouseHandler(
|
||||
ICommandRepository<Warehouse> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateWarehouseResult> Handle(UpdateWarehouseRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
if (entity.SystemWarehouse == true)
|
||||
{
|
||||
throw new Exception($"Updating system warehouse is not allowed.");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateWarehouseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.WarehouseManager.Queries;
|
||||
|
||||
public record GetWarehouseListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public bool? SystemWarehouse { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetWarehouseListProfile : Profile
|
||||
{
|
||||
public GetWarehouseListProfile()
|
||||
{
|
||||
CreateMap<Warehouse, GetWarehouseListDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetWarehouseListResult
|
||||
{
|
||||
public List<GetWarehouseListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetWarehouseListRequest : IRequest<GetWarehouseListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetWarehouseListHandler : IRequestHandler<GetWarehouseListRequest, GetWarehouseListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetWarehouseListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetWarehouseListResult> Handle(GetWarehouseListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Warehouse
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetWarehouseListDto>>(entities);
|
||||
|
||||
return new GetWarehouseListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Domain.Entities;
|
||||
|
||||
namespace Application.Features.WarehouseManager;
|
||||
|
||||
public class WarehouseService
|
||||
{
|
||||
private readonly IQueryContext _queryContext;
|
||||
|
||||
public WarehouseService(
|
||||
IQueryContext queryContext
|
||||
)
|
||||
{
|
||||
_queryContext = queryContext;
|
||||
}
|
||||
|
||||
|
||||
public Warehouse? GetCustomerWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "Customer").FirstOrDefault();
|
||||
}
|
||||
|
||||
public Warehouse? GetVendorWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "Vendor").FirstOrDefault();
|
||||
}
|
||||
|
||||
public Warehouse? GetTransferWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "Transfer").FirstOrDefault();
|
||||
}
|
||||
|
||||
public Warehouse? GetAdjustmentWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "Adjustment").FirstOrDefault();
|
||||
}
|
||||
|
||||
public Warehouse? GetStockCountWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "StockCount").FirstOrDefault();
|
||||
}
|
||||
|
||||
public Warehouse? GetScrappingWarehouse()
|
||||
{
|
||||
return _queryContext.Set<Warehouse>().Where(x => x.Name == "Scrapping").FirstOrDefault();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user