initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class CreateBookingRequest
|
||||
{
|
||||
public string? Subject { get; set; }
|
||||
public DateTime? StartTime { get; set; }
|
||||
public DateTime? EndTime { get; set; }
|
||||
public string? StartTimezone { get; set; }
|
||||
public string? EndTimezone { get; set; }
|
||||
public string? Location { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? IsAllDay { get; set; }
|
||||
public bool? IsReadOnly { get; set; }
|
||||
public bool? IsBlock { get; set; }
|
||||
public string? RecurrenceRule { get; set; }
|
||||
public BookingStatus Status { get; set; }
|
||||
public string? BookingResourceId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateBookingResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateBookingCommand(CreateBookingRequest Data) : IRequest<CreateBookingResponse>;
|
||||
|
||||
public class CreateBookingHandler : IRequestHandler<CreateBookingCommand, CreateBookingResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateBookingHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateBookingResponse> Handle(CreateBookingCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Booking);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Booking
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Subject = request.Data.Subject,
|
||||
StartTime = request.Data.StartTime,
|
||||
EndTime = request.Data.EndTime,
|
||||
StartTimezone = request.Data.StartTimezone,
|
||||
EndTimezone = request.Data.EndTimezone,
|
||||
Location = request.Data.Location,
|
||||
Description = request.Data.Description,
|
||||
IsAllDay = request.Data.IsAllDay,
|
||||
IsReadOnly = request.Data.IsReadOnly,
|
||||
IsBlock = request.Data.IsBlock,
|
||||
RecurrenceRule = request.Data.RecurrenceRule,
|
||||
Status = request.Data.Status,
|
||||
BookingResourceId = request.Data.BookingResourceId
|
||||
};
|
||||
|
||||
_context.Booking.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateBookingResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class CreateBookingValidator : AbstractValidator<CreateBookingRequest>
|
||||
{
|
||||
public CreateBookingValidator()
|
||||
{
|
||||
RuleFor(x => x.Subject)
|
||||
.NotEmpty().WithMessage("Subject is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.StartTime)
|
||||
.NotEmpty().WithMessage("Start Time is required");
|
||||
|
||||
RuleFor(x => x.EndTime)
|
||||
.NotEmpty().WithMessage("End Time is required");
|
||||
|
||||
RuleFor(x => x.BookingResourceId)
|
||||
.NotEmpty().WithMessage("Resource is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class DeleteBookingByIdRequest
|
||||
{
|
||||
public DeleteBookingByIdRequest(string id) => Id = id;
|
||||
public string Id { get; set; }
|
||||
}
|
||||
|
||||
public record DeleteBookingByIdCommand(DeleteBookingByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteBookingByIdHandler : IRequestHandler<DeleteBookingByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteBookingByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteBookingByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Booking
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Booking.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class GetBookingByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Subject { get; set; }
|
||||
public DateTime? StartTime { get; set; }
|
||||
public DateTime? EndTime { get; set; }
|
||||
public string? StartTimezone { get; set; }
|
||||
public string? EndTimezone { get; set; }
|
||||
public string? Location { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? IsAllDay { get; set; }
|
||||
public bool? IsReadOnly { get; set; }
|
||||
public bool? IsBlock { get; set; }
|
||||
public string? RecurrenceRule { get; set; }
|
||||
public string? RecurrenceID { get; set; }
|
||||
public string? FollowingID { get; set; }
|
||||
public string? RecurrenceException { get; set; }
|
||||
public BookingStatus Status { get; set; }
|
||||
public string? BookingResourceId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBookingByIdQuery(string Id) : IRequest<GetBookingByIdResponse?>;
|
||||
|
||||
public class GetBookingByIdHandler : IRequestHandler<GetBookingByIdQuery, GetBookingByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetBookingByIdResponse?> Handle(GetBookingByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Booking
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetBookingByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Subject = x.Subject,
|
||||
StartTime = x.StartTime,
|
||||
EndTime = x.EndTime,
|
||||
StartTimezone = x.StartTimezone,
|
||||
EndTimezone = x.EndTimezone,
|
||||
Location = x.Location,
|
||||
Description = x.Description,
|
||||
IsAllDay = x.IsAllDay,
|
||||
IsReadOnly = x.IsReadOnly,
|
||||
IsBlock = x.IsBlock,
|
||||
RecurrenceRule = x.RecurrenceRule,
|
||||
RecurrenceID = x.RecurrenceID,
|
||||
FollowingID = x.FollowingID,
|
||||
RecurrenceException = x.RecurrenceException,
|
||||
Status = x.Status,
|
||||
BookingResourceId = x.BookingResourceId,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class GetBookingListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Subject { get; set; }
|
||||
public DateTime? StartTime { get; set; }
|
||||
public DateTime? EndTime { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public string? ResourceName { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBookingListQuery() : IRequest<List<GetBookingListResponse>>;
|
||||
|
||||
public class GetBookingListHandler : IRequestHandler<GetBookingListQuery, List<GetBookingListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBookingListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetBookingListResponse>> Handle(GetBookingListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Booking
|
||||
.AsNoTracking()
|
||||
.Include(x => x.BookingResource)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetBookingListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Subject = x.Subject,
|
||||
StartTime = x.StartTime,
|
||||
EndTime = x.EndTime,
|
||||
Status = x.Status.GetDescription(),
|
||||
ResourceName = x.BookingResource != null ? x.BookingResource.Name : string.Empty,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class LookupBookingDataResponse
|
||||
{
|
||||
public List<LookupResourceItem> Resources { get; set; } = new();
|
||||
public List<LookupStatusItem> Statuses { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupResourceItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class LookupStatusItem
|
||||
{
|
||||
public int Value { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record LookupBookingDataQuery() : IRequest<LookupBookingDataResponse>;
|
||||
|
||||
public class LookupBookingDataHandler : IRequestHandler<LookupBookingDataQuery, LookupBookingDataResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public LookupBookingDataHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<LookupBookingDataResponse> Handle(LookupBookingDataQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var resources = await _context.BookingResource
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupResourceItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var statuses = Enum.GetValues(typeof(BookingStatus))
|
||||
.Cast<BookingStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new LookupBookingDataResponse
|
||||
{
|
||||
Resources = resources,
|
||||
Statuses = statuses
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class UpdateBookingRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Subject { get; set; }
|
||||
public DateTime? StartTime { get; set; }
|
||||
public DateTime? EndTime { get; set; }
|
||||
public string? StartTimezone { get; set; }
|
||||
public string? EndTimezone { get; set; }
|
||||
public string? Location { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool? IsAllDay { get; set; }
|
||||
public bool? IsReadOnly { get; set; }
|
||||
public bool? IsBlock { get; set; }
|
||||
public string? RecurrenceRule { get; set; }
|
||||
public BookingStatus Status { get; set; }
|
||||
public string? BookingResourceId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateBookingResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateBookingCommand(UpdateBookingRequest Data) : IRequest<UpdateBookingResponse>;
|
||||
|
||||
public class UpdateBookingHandler : IRequestHandler<UpdateBookingCommand, UpdateBookingResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateBookingHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateBookingResponse> Handle(UpdateBookingCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Booking
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateBookingResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Subject = request.Data.Subject;
|
||||
entity.StartTime = request.Data.StartTime;
|
||||
entity.EndTime = request.Data.EndTime;
|
||||
entity.StartTimezone = request.Data.StartTimezone;
|
||||
entity.EndTimezone = request.Data.EndTimezone;
|
||||
entity.Location = request.Data.Location;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.IsAllDay = request.Data.IsAllDay;
|
||||
entity.IsReadOnly = request.Data.IsReadOnly;
|
||||
entity.IsBlock = request.Data.IsBlock;
|
||||
entity.RecurrenceRule = request.Data.RecurrenceRule;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.BookingResourceId = request.Data.BookingResourceId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateBookingResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Utilities.BookingManager.Cqrs;
|
||||
|
||||
public class UpdateBookingValidator : AbstractValidator<UpdateBookingRequest>
|
||||
{
|
||||
public UpdateBookingValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
|
||||
RuleFor(x => x.Subject)
|
||||
.NotEmpty().WithMessage("Subject is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.StartTime)
|
||||
.NotEmpty().WithMessage("Start Time is required");
|
||||
|
||||
RuleFor(x => x.EndTime)
|
||||
.NotEmpty().WithMessage("End Time is required");
|
||||
|
||||
RuleFor(x => x.BookingResourceId)
|
||||
.NotEmpty().WithMessage("Resource is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user