initial commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.VendorGroupManager.Commands;
|
||||
|
||||
public class CreateVendorGroupResult
|
||||
{
|
||||
public VendorGroup? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateVendorGroupRequest : IRequest<CreateVendorGroupResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateVendorGroupValidator : AbstractValidator<CreateVendorGroupRequest>
|
||||
{
|
||||
public CreateVendorGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateVendorGroupHandler : IRequestHandler<CreateVendorGroupRequest, CreateVendorGroupResult>
|
||||
{
|
||||
private readonly ICommandRepository<VendorGroup> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public CreateVendorGroupHandler(
|
||||
ICommandRepository<VendorGroup> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<CreateVendorGroupResult> Handle(CreateVendorGroupRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new VendorGroup();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateVendorGroupResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.VendorGroupManager.Commands;
|
||||
|
||||
public class DeleteVendorGroupResult
|
||||
{
|
||||
public VendorGroup? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteVendorGroupRequest : IRequest<DeleteVendorGroupResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteVendorGroupValidator : AbstractValidator<DeleteVendorGroupRequest>
|
||||
{
|
||||
public DeleteVendorGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteVendorGroupHandler : IRequestHandler<DeleteVendorGroupRequest, DeleteVendorGroupResult>
|
||||
{
|
||||
private readonly ICommandRepository<VendorGroup> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteVendorGroupHandler(
|
||||
ICommandRepository<VendorGroup> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteVendorGroupResult> Handle(DeleteVendorGroupRequest 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 DeleteVendorGroupResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.VendorGroupManager.Commands;
|
||||
|
||||
public class UpdateVendorGroupResult
|
||||
{
|
||||
public VendorGroup? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateVendorGroupRequest : IRequest<UpdateVendorGroupResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateVendorGroupValidator : AbstractValidator<UpdateVendorGroupRequest>
|
||||
{
|
||||
public UpdateVendorGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateVendorGroupHandler : IRequestHandler<UpdateVendorGroupRequest, UpdateVendorGroupResult>
|
||||
{
|
||||
private readonly ICommandRepository<VendorGroup> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateVendorGroupHandler(
|
||||
ICommandRepository<VendorGroup> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateVendorGroupResult> Handle(UpdateVendorGroupRequest 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.Description = request.Description;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateVendorGroupResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.VendorGroupManager.Queries;
|
||||
|
||||
public record GetVendorGroupListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetVendorGroupListProfile : Profile
|
||||
{
|
||||
public GetVendorGroupListProfile()
|
||||
{
|
||||
CreateMap<VendorGroup, GetVendorGroupListDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetVendorGroupListResult
|
||||
{
|
||||
public List<GetVendorGroupListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetVendorGroupListRequest : IRequest<GetVendorGroupListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetVendorGroupListHandler : IRequestHandler<GetVendorGroupListRequest, GetVendorGroupListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetVendorGroupListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetVendorGroupListResult> Handle(GetVendorGroupListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.VendorGroup
|
||||
.AsNoTracking()
|
||||
.IsDeletedEqualTo(request.IsDeleted)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetVendorGroupListDto>>(entities);
|
||||
|
||||
return new GetVendorGroupListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user