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.CustomerCategory.Cqrs;
|
||||
|
||||
public class CreateCustomerCategoryRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCustomerCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateCustomerCategoryCommand(CreateCustomerCategoryRequest Data) : IRequest<CreateCustomerCategoryResponse>;
|
||||
|
||||
public class CreateCustomerCategoryHandler : IRequestHandler<CreateCustomerCategoryCommand, CreateCustomerCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateCustomerCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateCustomerCategoryResponse> Handle(CreateCustomerCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.CustomerCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Customer Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.CustomerCategory
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.CustomerCategory.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerCategory.Cqrs;
|
||||
|
||||
public class CreateCustomerCategoryValidator : AbstractValidator<CreateCustomerCategoryRequest>
|
||||
{
|
||||
public CreateCustomerCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerCategory.Cqrs;
|
||||
|
||||
public record DeleteCustomerCategoryByIdRequest(string Id);
|
||||
|
||||
public record DeleteCustomerCategoryByIdCommand(DeleteCustomerCategoryByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteCustomerCategoryByIdHandler : IRequestHandler<DeleteCustomerCategoryByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteCustomerCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteCustomerCategoryByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.CustomerCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.CustomerCategory.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.CustomerCategory.Cqrs;
|
||||
|
||||
public class GetCustomerCategoryByIdResponse
|
||||
{
|
||||
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 GetCustomerCategoryByIdQuery(string Id) : IRequest<GetCustomerCategoryByIdResponse?>;
|
||||
|
||||
public class GetCustomerCategoryByIdHandler : IRequestHandler<GetCustomerCategoryByIdQuery, GetCustomerCategoryByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetCustomerCategoryByIdResponse?> Handle(GetCustomerCategoryByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerCategory
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetCustomerCategoryByIdResponse
|
||||
{
|
||||
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.CustomerCategory.Cqrs;
|
||||
|
||||
public class GetCustomerCategoryListResponse
|
||||
{
|
||||
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 GetCustomerCategoryListQuery() : IRequest<List<GetCustomerCategoryListResponse>>;
|
||||
|
||||
public class GetCustomerCategoryListHandler : IRequestHandler<GetCustomerCategoryListQuery, List<GetCustomerCategoryListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerCategoryListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetCustomerCategoryListResponse>> Handle(GetCustomerCategoryListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerCategory
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetCustomerCategoryListResponse
|
||||
{
|
||||
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.CustomerCategory.Cqrs;
|
||||
|
||||
public class UpdateCustomerCategoryRequest
|
||||
{
|
||||
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 UpdateCustomerCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateCustomerCategoryCommand(UpdateCustomerCategoryRequest Data) : IRequest<UpdateCustomerCategoryResponse>;
|
||||
|
||||
public class UpdateCustomerCategoryHandler : IRequestHandler<UpdateCustomerCategoryCommand, UpdateCustomerCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateCustomerCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateCustomerCategoryResponse> Handle(UpdateCustomerCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.CustomerCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Customer Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.CustomerCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateCustomerCategoryResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerCategory.Cqrs;
|
||||
|
||||
public class UpdateCustomerCategoryValidator : AbstractValidator<UpdateCustomerCategoryRequest>
|
||||
{
|
||||
public UpdateCustomerCategoryValidator()
|
||||
{
|
||||
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