initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class CreateBookingResourceRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? BookingGroupId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateBookingResourceResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateBookingResourceCommand(CreateBookingResourceRequest Data) : IRequest<CreateBookingResourceResponse>;
|
||||
|
||||
public class CreateBookingResourceHandler : IRequestHandler<CreateBookingResourceCommand, CreateBookingResourceResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateBookingResourceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateBookingResourceResponse> Handle(CreateBookingResourceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.BookingResource
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Booking Resource", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.BookingResource
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
BookingGroupId = request.Data.BookingGroupId
|
||||
};
|
||||
|
||||
_context.BookingResource.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateBookingResourceResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class CreateBookingResourceValidator : AbstractValidator<CreateBookingResourceRequest>
|
||||
{
|
||||
public CreateBookingResourceValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.BookingGroupId)
|
||||
.NotEmpty().WithMessage("Booking Group is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class DeleteBookingResourceByIdRequest
|
||||
{
|
||||
public DeleteBookingResourceByIdRequest(string id) => Id = id;
|
||||
public string Id { get; set; }
|
||||
}
|
||||
|
||||
public record DeleteBookingResourceByIdCommand(DeleteBookingResourceByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteBookingResourceByIdHandler : IRequestHandler<DeleteBookingResourceByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteBookingResourceByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteBookingResourceByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.BookingResource
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_context.BookingResource.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class GetBookingResourceByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? BookingGroupId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBookingResourceByIdQuery(string Id) : IRequest<GetBookingResourceByIdResponse?>;
|
||||
|
||||
public class GetBookingResourceByIdHandler : IRequestHandler<GetBookingResourceByIdQuery, GetBookingResourceByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingResourceByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetBookingResourceByIdResponse?> Handle(GetBookingResourceByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.BookingResource
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetBookingResourceByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
BookingGroupId = x.BookingGroupId,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class GetBookingResourceListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? BookingGroupName { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBookingResourceListQuery() : IRequest<List<GetBookingResourceListResponse>>;
|
||||
|
||||
public class GetBookingResourceListHandler : IRequestHandler<GetBookingResourceListQuery, List<GetBookingResourceListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingResourceListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetBookingResourceListResponse>> Handle(GetBookingResourceListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.BookingResource
|
||||
.AsNoTracking()
|
||||
.Include(x => x.BookingGroup)
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetBookingResourceListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
BookingGroupName = x.BookingGroup != null ? x.BookingGroup.Name : string.Empty,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class LookupBookingGroupResponse
|
||||
{
|
||||
public List<LookupBookingGroupItem> BookingGroups { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupBookingGroupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record LookupBookingGroupQuery() : IRequest<LookupBookingGroupResponse>;
|
||||
|
||||
public class LookupBookingGroupHandler : IRequestHandler<LookupBookingGroupQuery, LookupBookingGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public LookupBookingGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<LookupBookingGroupResponse> Handle(LookupBookingGroupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await _context.BookingGroup
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupBookingGroupItem
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return new LookupBookingGroupResponse { BookingGroups = items };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class UpdateBookingResourceRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? BookingGroupId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateBookingResourceResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateBookingResourceCommand(UpdateBookingResourceRequest Data) : IRequest<UpdateBookingResourceResponse>;
|
||||
|
||||
public class UpdateBookingResourceHandler : IRequestHandler<UpdateBookingResourceCommand, UpdateBookingResourceResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateBookingResourceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateBookingResourceResponse> Handle(UpdateBookingResourceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.BookingResource
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Booking Resource", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.BookingResource
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateBookingResourceResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.BookingGroupId = request.Data.BookingGroupId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateBookingResourceResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingResource.Cqrs;
|
||||
|
||||
public class UpdateBookingResourceValidator : AbstractValidator<UpdateBookingResourceRequest>
|
||||
{
|
||||
public UpdateBookingResourceValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.BookingGroupId)
|
||||
.NotEmpty().WithMessage("Booking Group is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user