initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class CreateBookingGroupRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateBookingGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateBookingGroupCommand(CreateBookingGroupRequest Data) : IRequest<CreateBookingGroupResponse>;
|
||||
|
||||
public class CreateBookingGroupHandler : IRequestHandler<CreateBookingGroupCommand, CreateBookingGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateBookingGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateBookingGroupResponse> Handle(CreateBookingGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.BookingGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Booking Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.BookingGroup
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.BookingGroup.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateBookingGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class CreateBookingGroupValidator : AbstractValidator<CreateBookingGroupRequest>
|
||||
{
|
||||
public CreateBookingGroupValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class DeleteBookingGroupByIdRequest
|
||||
{
|
||||
public DeleteBookingGroupByIdRequest(string id) => Id = id;
|
||||
public string Id { get; set; }
|
||||
}
|
||||
|
||||
public record DeleteBookingGroupByIdCommand(DeleteBookingGroupByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteBookingGroupByIdHandler : IRequestHandler<DeleteBookingGroupByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteBookingGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteBookingGroupByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.BookingGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_context.BookingGroup.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class GetBookingGroupByIdResponse
|
||||
{
|
||||
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 GetBookingGroupByIdQuery(string Id) : IRequest<GetBookingGroupByIdResponse?>;
|
||||
|
||||
public class GetBookingGroupByIdHandler : IRequestHandler<GetBookingGroupByIdQuery, GetBookingGroupByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingGroupByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetBookingGroupByIdResponse?> Handle(GetBookingGroupByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.BookingGroup
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetBookingGroupByIdResponse
|
||||
{
|
||||
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.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class GetBookingGroupListResponse
|
||||
{
|
||||
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 GetBookingGroupListQuery() : IRequest<List<GetBookingGroupListResponse>>;
|
||||
|
||||
public class GetBookingGroupListHandler : IRequestHandler<GetBookingGroupListQuery, List<GetBookingGroupListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingGroupListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetBookingGroupListResponse>> Handle(GetBookingGroupListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.BookingGroup
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetBookingGroupListResponse
|
||||
{
|
||||
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,62 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class UpdateBookingGroupRequest
|
||||
{
|
||||
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 UpdateBookingGroupResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateBookingGroupCommand(UpdateBookingGroupRequest Data) : IRequest<UpdateBookingGroupResponse>;
|
||||
|
||||
public class UpdateBookingGroupHandler : IRequestHandler<UpdateBookingGroupCommand, UpdateBookingGroupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateBookingGroupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateBookingGroupResponse> Handle(UpdateBookingGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.BookingGroup
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Booking Group", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.BookingGroup
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateBookingGroupResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateBookingGroupResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
||||
|
||||
public class UpdateBookingGroupValidator : AbstractValidator<UpdateBookingGroupRequest>
|
||||
{
|
||||
public UpdateBookingGroupValidator()
|
||||
{
|
||||
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