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.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class CreateVendorCategoryRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateVendorCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateVendorCategoryCommand(CreateVendorCategoryRequest Data) : IRequest<CreateVendorCategoryResponse>;
|
||||
|
||||
public class CreateVendorCategoryHandler : IRequestHandler<CreateVendorCategoryCommand, CreateVendorCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateVendorCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateVendorCategoryResponse> Handle(CreateVendorCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.VendorCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Vendor Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.VendorCategory
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.VendorCategory.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateVendorCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class CreateVendorCategoryValidator : AbstractValidator<CreateVendorCategoryRequest>
|
||||
{
|
||||
public CreateVendorCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category 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.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public record DeleteVendorCategoryByIdRequest(string Id);
|
||||
|
||||
public record DeleteVendorCategoryByIdCommand(DeleteVendorCategoryByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteVendorCategoryByIdHandler : IRequestHandler<DeleteVendorCategoryByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteVendorCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteVendorCategoryByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.VendorCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.VendorCategory.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class GetVendorCategoryByIdResponse
|
||||
{
|
||||
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 GetVendorCategoryByIdQuery(string Id) : IRequest<GetVendorCategoryByIdResponse?>;
|
||||
|
||||
public class GetVendorCategoryByIdHandler : IRequestHandler<GetVendorCategoryByIdQuery, GetVendorCategoryByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetVendorCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetVendorCategoryByIdResponse?> Handle(GetVendorCategoryByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.VendorCategory
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetVendorCategoryByIdResponse
|
||||
{
|
||||
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,35 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class GetVendorCategoryListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetVendorCategoryListQuery() : IRequest<List<GetVendorCategoryListResponse>>;
|
||||
|
||||
public class GetVendorCategoryListHandler : IRequestHandler<GetVendorCategoryListQuery, List<GetVendorCategoryListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetVendorCategoryListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetVendorCategoryListResponse>> Handle(GetVendorCategoryListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.VendorCategory
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetVendorCategoryListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class UpdateVendorCategoryRequest
|
||||
{
|
||||
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 UpdateVendorCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateVendorCategoryCommand(UpdateVendorCategoryRequest Data) : IRequest<UpdateVendorCategoryResponse>;
|
||||
|
||||
public class UpdateVendorCategoryHandler : IRequestHandler<UpdateVendorCategoryCommand, UpdateVendorCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateVendorCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateVendorCategoryResponse> Handle(UpdateVendorCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.VendorCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Vendor Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.VendorCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateVendorCategoryResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateVendorCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;
|
||||
|
||||
public class UpdateVendorCategoryValidator : AbstractValidator<UpdateVendorCategoryRequest>
|
||||
{
|
||||
public UpdateVendorCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user