initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorGroup.Cqrs;
|
||||
|
||||
public class CreateVendorGroupRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateVendorGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateVendorGroupCommand(CreateVendorGroupRequest Data) : IRequest<CreateVendorGroupResponse>;
|
||||
|
||||
public class CreateVendorGroupHandler : IRequestHandler<CreateVendorGroupCommand, CreateVendorGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateVendorGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateVendorGroupResponse> Handle(CreateVendorGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.VendorGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Vendor Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
|
||||
var entity = new Data.Entities.VendorGroup
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.VendorGroup.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateVendorGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorGroup.Cqrs;
|
||||
|
||||
public class CreateVendorGroupValidator : AbstractValidator<CreateVendorGroupRequest>
|
||||
{
|
||||
public CreateVendorGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Group 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.VendorGroup.Cqrs;
|
||||
|
||||
public record DeleteVendorGroupByIdRequest(string Id);
|
||||
|
||||
public record DeleteVendorGroupByIdCommand(DeleteVendorGroupByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteVendorGroupByIdHandler : IRequestHandler<DeleteVendorGroupByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteVendorGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteVendorGroupByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.VendorGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.VendorGroup.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.VendorGroup.Cqrs;
|
||||
|
||||
public class GetVendorGroupByIdResponse
|
||||
{
|
||||
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 GetVendorGroupByIdQuery(string Id) : IRequest<GetVendorGroupByIdResponse?>;
|
||||
|
||||
public class GetVendorGroupByIdHandler : IRequestHandler<GetVendorGroupByIdQuery, GetVendorGroupByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetVendorGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetVendorGroupByIdResponse?> Handle(GetVendorGroupByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.VendorGroup
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetVendorGroupByIdResponse
|
||||
{
|
||||
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.VendorGroup.Cqrs;
|
||||
|
||||
public class GetVendorGroupListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetVendorGroupListQuery() : IRequest<List<GetVendorGroupListResponse>>;
|
||||
|
||||
public class GetVendorGroupListHandler : IRequestHandler<GetVendorGroupListQuery, List<GetVendorGroupListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetVendorGroupListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetVendorGroupListResponse>> Handle(GetVendorGroupListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.VendorGroup
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetVendorGroupListResponse
|
||||
{
|
||||
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.VendorGroup.Cqrs;
|
||||
|
||||
public class UpdateVendorGroupRequest
|
||||
{
|
||||
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 UpdateVendorGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateVendorGroupCommand(UpdateVendorGroupRequest Data) : IRequest<UpdateVendorGroupResponse>;
|
||||
|
||||
public class UpdateVendorGroupHandler : IRequestHandler<UpdateVendorGroupCommand, UpdateVendorGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateVendorGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateVendorGroupResponse> Handle(UpdateVendorGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.VendorGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Vendor Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.VendorGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateVendorGroupResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateVendorGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.VendorGroup.Cqrs;
|
||||
|
||||
public class UpdateVendorGroupValidator : AbstractValidator<UpdateVendorGroupRequest>
|
||||
{
|
||||
public UpdateVendorGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Group Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user