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.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class CreateMedicalRecordGroupRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateMedicalRecordGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateMedicalRecordGroupCommand(CreateMedicalRecordGroupRequest Data) : IRequest<CreateMedicalRecordGroupResponse>;
|
||||
|
||||
public class CreateMedicalRecordGroupHandler : IRequestHandler<CreateMedicalRecordGroupCommand, CreateMedicalRecordGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateMedicalRecordGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateMedicalRecordGroupResponse> Handle(CreateMedicalRecordGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.MedicalRecordGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("MedicalRecord Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.MedicalRecordGroup
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.MedicalRecordGroup.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateMedicalRecordGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class CreateMedicalRecordGroupValidator : AbstractValidator<CreateMedicalRecordGroupRequest>
|
||||
{
|
||||
public CreateMedicalRecordGroupValidator()
|
||||
{
|
||||
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.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public record DeleteMedicalRecordGroupByIdRequest(string Id);
|
||||
|
||||
public record DeleteMedicalRecordGroupByIdCommand(DeleteMedicalRecordGroupByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteMedicalRecordGroupByIdHandler : IRequestHandler<DeleteMedicalRecordGroupByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteMedicalRecordGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteMedicalRecordGroupByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.MedicalRecordGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.MedicalRecordGroup.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class GetMedicalRecordGroupByIdResponse
|
||||
{
|
||||
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 GetMedicalRecordGroupByIdQuery(string Id) : IRequest<GetMedicalRecordGroupByIdResponse?>;
|
||||
|
||||
public class GetMedicalRecordGroupByIdHandler : IRequestHandler<GetMedicalRecordGroupByIdQuery, GetMedicalRecordGroupByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetMedicalRecordGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetMedicalRecordGroupByIdResponse?> Handle(GetMedicalRecordGroupByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.MedicalRecordGroup
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetMedicalRecordGroupByIdResponse
|
||||
{
|
||||
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.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class GetMedicalRecordGroupListResponse
|
||||
{
|
||||
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 GetMedicalRecordGroupListQuery() : IRequest<List<GetMedicalRecordGroupListResponse>>;
|
||||
|
||||
public class GetMedicalRecordGroupListHandler : IRequestHandler<GetMedicalRecordGroupListQuery, List<GetMedicalRecordGroupListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetMedicalRecordGroupListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetMedicalRecordGroupListResponse>> Handle(GetMedicalRecordGroupListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.MedicalRecordGroup
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetMedicalRecordGroupListResponse
|
||||
{
|
||||
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.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class UpdateMedicalRecordGroupRequest
|
||||
{
|
||||
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 UpdateMedicalRecordGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateMedicalRecordGroupCommand(UpdateMedicalRecordGroupRequest Data) : IRequest<UpdateMedicalRecordGroupResponse>;
|
||||
|
||||
public class UpdateMedicalRecordGroupHandler : IRequestHandler<UpdateMedicalRecordGroupCommand, UpdateMedicalRecordGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateMedicalRecordGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateMedicalRecordGroupResponse> Handle(UpdateMedicalRecordGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.MedicalRecordGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("MedicalRecord Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.MedicalRecordGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateMedicalRecordGroupResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateMedicalRecordGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Medical.MedicalRecordGroup.Cqrs;
|
||||
|
||||
public class UpdateMedicalRecordGroupValidator : AbstractValidator<UpdateMedicalRecordGroupRequest>
|
||||
{
|
||||
public UpdateMedicalRecordGroupValidator()
|
||||
{
|
||||
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