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.PatientCategory.Cqrs;
|
||||
|
||||
public class CreatePatientCategoryRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePatientCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreatePatientCategoryCommand(CreatePatientCategoryRequest Data) : IRequest<CreatePatientCategoryResponse>;
|
||||
|
||||
public class CreatePatientCategoryHandler : IRequestHandler<CreatePatientCategoryCommand, CreatePatientCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreatePatientCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreatePatientCategoryResponse> Handle(CreatePatientCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.PatientCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Patient Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.PatientCategory
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.PatientCategory.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreatePatientCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.PatientCategory.Cqrs;
|
||||
|
||||
public class CreatePatientCategoryValidator : AbstractValidator<CreatePatientCategoryRequest>
|
||||
{
|
||||
public CreatePatientCategoryValidator()
|
||||
{
|
||||
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.PatientCategory.Cqrs;
|
||||
|
||||
public record DeletePatientCategoryByIdRequest(string Id);
|
||||
|
||||
public record DeletePatientCategoryByIdCommand(DeletePatientCategoryByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeletePatientCategoryByIdHandler : IRequestHandler<DeletePatientCategoryByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeletePatientCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeletePatientCategoryByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.PatientCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.PatientCategory.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.PatientCategory.Cqrs;
|
||||
|
||||
public class GetPatientCategoryByIdResponse
|
||||
{
|
||||
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 GetPatientCategoryByIdQuery(string Id) : IRequest<GetPatientCategoryByIdResponse?>;
|
||||
|
||||
public class GetPatientCategoryByIdHandler : IRequestHandler<GetPatientCategoryByIdQuery, GetPatientCategoryByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetPatientCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetPatientCategoryByIdResponse?> Handle(GetPatientCategoryByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.PatientCategory
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetPatientCategoryByIdResponse
|
||||
{
|
||||
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.PatientCategory.Cqrs;
|
||||
|
||||
public class GetPatientCategoryListResponse
|
||||
{
|
||||
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 GetPatientCategoryListQuery() : IRequest<List<GetPatientCategoryListResponse>>;
|
||||
|
||||
public class GetPatientCategoryListHandler : IRequestHandler<GetPatientCategoryListQuery, List<GetPatientCategoryListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetPatientCategoryListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetPatientCategoryListResponse>> Handle(GetPatientCategoryListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.PatientCategory
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetPatientCategoryListResponse
|
||||
{
|
||||
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.PatientCategory.Cqrs;
|
||||
|
||||
public class UpdatePatientCategoryRequest
|
||||
{
|
||||
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 UpdatePatientCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdatePatientCategoryCommand(UpdatePatientCategoryRequest Data) : IRequest<UpdatePatientCategoryResponse>;
|
||||
|
||||
public class UpdatePatientCategoryHandler : IRequestHandler<UpdatePatientCategoryCommand, UpdatePatientCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdatePatientCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdatePatientCategoryResponse> Handle(UpdatePatientCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.PatientCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Patient Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.PatientCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdatePatientCategoryResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdatePatientCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.PatientCategory.Cqrs;
|
||||
|
||||
public class UpdatePatientCategoryValidator : AbstractValidator<UpdatePatientCategoryRequest>
|
||||
{
|
||||
public UpdatePatientCategoryValidator()
|
||||
{
|
||||
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