initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ProductManager.Commands;
|
||||
|
||||
public class CreateProductResult
|
||||
{
|
||||
public Product? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateProductRequest : IRequest<CreateProductResult>
|
||||
{
|
||||
public string? Number { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? UnitPrice { get; init; }
|
||||
public bool? Physical { get; init; } = true;
|
||||
public string? UnitMeasureId { get; init; }
|
||||
public string? ProductGroupId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateProductValidator : AbstractValidator<CreateProductRequest>
|
||||
{
|
||||
public CreateProductValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.UnitPrice).NotEmpty();
|
||||
RuleFor(x => x.Physical).NotEmpty();
|
||||
RuleFor(x => x.UnitMeasureId).NotEmpty();
|
||||
RuleFor(x => x.ProductGroupId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateProductHandler : IRequestHandler<CreateProductRequest, CreateProductResult>
|
||||
{
|
||||
private readonly ICommandRepository<Product> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateProductHandler(
|
||||
ICommandRepository<Product> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateProductResult> Handle(CreateProductRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Product();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(Product), "", "ART");
|
||||
entity.Name = request.Name;
|
||||
entity.UnitPrice = request.UnitPrice;
|
||||
entity.Physical = request.Physical;
|
||||
entity.Description = request.Description;
|
||||
entity.UnitMeasureId = request.UnitMeasureId;
|
||||
entity.ProductGroupId = request.ProductGroupId;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateProductResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ProductManager.Commands;
|
||||
|
||||
public class DeleteProductResult
|
||||
{
|
||||
public Product? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductRequest : IRequest<DeleteProductResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteProductValidator : AbstractValidator<DeleteProductRequest>
|
||||
{
|
||||
public DeleteProductValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteProductHandler : IRequestHandler<DeleteProductRequest, DeleteProductResult>
|
||||
{
|
||||
private readonly ICommandRepository<Product> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteProductHandler(
|
||||
ICommandRepository<Product> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteProductResult> Handle(DeleteProductRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeleteProductResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ProductManager.Commands;
|
||||
|
||||
public class UpdateProductResult
|
||||
{
|
||||
public Product? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateProductRequest : IRequest<UpdateProductResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? UnitPrice { get; init; }
|
||||
public bool? Physical { get; init; } = true;
|
||||
public string? UnitMeasureId { get; init; }
|
||||
public string? ProductGroupId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateProductValidator : AbstractValidator<UpdateProductRequest>
|
||||
{
|
||||
public UpdateProductValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.UnitPrice).NotEmpty();
|
||||
RuleFor(x => x.Physical).NotEmpty();
|
||||
RuleFor(x => x.UnitMeasureId).NotEmpty();
|
||||
RuleFor(x => x.ProductGroupId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateProductHandler : IRequestHandler<UpdateProductRequest, UpdateProductResult>
|
||||
{
|
||||
private readonly ICommandRepository<Product> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateProductHandler(
|
||||
ICommandRepository<Product> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateProductResult> Handle(UpdateProductRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.UnitPrice = request.UnitPrice;
|
||||
entity.Physical = request.Physical;
|
||||
entity.Description = request.Description;
|
||||
entity.UnitMeasureId = request.UnitMeasureId;
|
||||
entity.ProductGroupId = request.ProductGroupId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateProductResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.ProductManager.Queries;
|
||||
|
||||
public record GetProductListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? UnitPrice { get; init; }
|
||||
public bool? Physical { get; init; }
|
||||
public string? UnitMeasureId { get; init; }
|
||||
public string? UnitMeasureName { get; init; }
|
||||
public string? ProductGroupId { get; init; }
|
||||
public string? ProductGroupName { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetProductListProfile : Profile
|
||||
{
|
||||
public GetProductListProfile()
|
||||
{
|
||||
CreateMap<Product, GetProductListDto>()
|
||||
.ForMember(
|
||||
dest => dest.UnitMeasureName,
|
||||
opt => opt.MapFrom(src => src.UnitMeasure != null ? src.UnitMeasure.Name : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.ProductGroupName,
|
||||
opt => opt.MapFrom(src => src.ProductGroup != null ? src.ProductGroup.Name : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetProductListResult
|
||||
{
|
||||
public List<GetProductListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetProductListRequest : IRequest<GetProductListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetProductListHandler : IRequestHandler<GetProductListRequest, GetProductListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetProductListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetProductListResult> Handle(GetProductListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Product
|
||||
.AsNoTracking()
|
||||
.IsDeletedEqualTo(request.IsDeleted)
|
||||
.Include(x => x.UnitMeasure)
|
||||
.Include(x => x.ProductGroup)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetProductListDto>>(entities);
|
||||
|
||||
return new GetProductListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user