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.CustomerGroup.Cqrs;
|
||||
|
||||
public class CreateCustomerGroupRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCustomerGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateCustomerGroupCommand(CreateCustomerGroupRequest Data) : IRequest<CreateCustomerGroupResponse>;
|
||||
|
||||
public class CreateCustomerGroupHandler : IRequestHandler<CreateCustomerGroupCommand, CreateCustomerGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateCustomerGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateCustomerGroupResponse> Handle(CreateCustomerGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.CustomerGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Customer Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.CustomerGroup
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.CustomerGroup.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerGroup.Cqrs;
|
||||
|
||||
public class CreateCustomerGroupValidator : AbstractValidator<CreateCustomerGroupRequest>
|
||||
{
|
||||
public CreateCustomerGroupValidator()
|
||||
{
|
||||
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.CustomerGroup.Cqrs;
|
||||
|
||||
public record DeleteCustomerGroupByIdRequest(string Id);
|
||||
|
||||
public record DeleteCustomerGroupByIdCommand(DeleteCustomerGroupByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteCustomerGroupByIdHandler : IRequestHandler<DeleteCustomerGroupByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteCustomerGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteCustomerGroupByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.CustomerGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.CustomerGroup.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.CustomerGroup.Cqrs;
|
||||
|
||||
public class GetCustomerGroupByIdResponse
|
||||
{
|
||||
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 GetCustomerGroupByIdQuery(string Id) : IRequest<GetCustomerGroupByIdResponse?>;
|
||||
|
||||
public class GetCustomerGroupByIdHandler : IRequestHandler<GetCustomerGroupByIdQuery, GetCustomerGroupByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetCustomerGroupByIdResponse?> Handle(GetCustomerGroupByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerGroup
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetCustomerGroupByIdResponse
|
||||
{
|
||||
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,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerGroup.Cqrs;
|
||||
|
||||
public class GetCustomerGroupListResponse
|
||||
{
|
||||
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 GetCustomerGroupListQuery() : IRequest<List<GetCustomerGroupListResponse>>;
|
||||
|
||||
public class GetCustomerGroupListHandler : IRequestHandler<GetCustomerGroupListQuery, List<GetCustomerGroupListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerGroupListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetCustomerGroupListResponse>> Handle(GetCustomerGroupListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerGroup
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetCustomerGroupListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerGroup.Cqrs;
|
||||
|
||||
public class UpdateCustomerGroupRequest
|
||||
{
|
||||
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 UpdateCustomerGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateCustomerGroupCommand(UpdateCustomerGroupRequest Data) : IRequest<UpdateCustomerGroupResponse>;
|
||||
|
||||
public class UpdateCustomerGroupHandler : IRequestHandler<UpdateCustomerGroupCommand, UpdateCustomerGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateCustomerGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateCustomerGroupResponse> Handle(UpdateCustomerGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.CustomerGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Customer Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.CustomerGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateCustomerGroupResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerGroup.Cqrs;
|
||||
|
||||
public class UpdateCustomerGroupValidator : AbstractValidator<UpdateCustomerGroupRequest>
|
||||
{
|
||||
public UpdateCustomerGroupValidator()
|
||||
{
|
||||
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