initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class CreateProgramResourceRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateProgramResourceResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateProgramResourceCommand(CreateProgramResourceRequest Data) : IRequest<CreateProgramResourceResponse>;
|
||||
|
||||
public class CreateProgramResourceHandler : IRequestHandler<CreateProgramResourceCommand, CreateProgramResourceResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateProgramResourceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateProgramResourceResponse> Handle(CreateProgramResourceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = new Data.Entities.ProgramManagerResource
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.ProgramManagerResource.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateProgramResourceResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class CreateProgramResourceValidator : AbstractValidator<CreateProgramResourceRequest>
|
||||
{
|
||||
public CreateProgramResourceValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public record DeleteProgramResourceCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteProgramResourceHandler : IRequestHandler<DeleteProgramResourceCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteProgramResourceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteProgramResourceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProgramManagerResource
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_context.ProgramManagerResource.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class GetProgramResourceByIdResponse
|
||||
{
|
||||
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 GetProgramResourceByIdQuery(string Id) : IRequest<GetProgramResourceByIdResponse?>;
|
||||
|
||||
public class GetProgramResourceByIdHandler : IRequestHandler<GetProgramResourceByIdQuery, GetProgramResourceByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetProgramResourceByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetProgramResourceByIdResponse?> Handle(GetProgramResourceByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.ProgramManagerResource
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetProgramResourceByIdResponse
|
||||
{
|
||||
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.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class GetProgramResourceListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetProgramResourceListQuery() : IRequest<List<GetProgramResourceListResponse>>;
|
||||
|
||||
public class GetProgramResourceListHandler : IRequestHandler<GetProgramResourceListQuery, List<GetProgramResourceListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetProgramResourceListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetProgramResourceListResponse>> Handle(GetProgramResourceListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.ProgramManagerResource
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetProgramResourceListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class UpdateProgramResourceRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateProgramResourceResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateProgramResourceCommand(UpdateProgramResourceRequest Data) : IRequest<UpdateProgramResourceResponse>;
|
||||
|
||||
public class UpdateProgramResourceHandler : IRequestHandler<UpdateProgramResourceCommand, UpdateProgramResourceResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateProgramResourceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateProgramResourceResponse> Handle(UpdateProgramResourceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProgramManagerResource
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateProgramResourceResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateProgramResourceResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;
|
||||
|
||||
public class UpdateProgramResourceValidator : AbstractValidator<UpdateProgramResourceRequest>
|
||||
{
|
||||
public UpdateProgramResourceValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user