Skip to content

Commit

Permalink
#40 Accessibility - booked records refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kgniazdowski committed Dec 21, 2021
1 parent 4c70235 commit cd4ba02
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Accessibility.Application.BookedRecords.Commands.SetBookedRecordStatus;
using Accessibility.Application.BookedRecords.Queries;
using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfCustomer;
using Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility;
using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility;
using Accessibility.Domain.Bookings.BookedRecords;
using Core.Queries;
using MediatR;
Expand Down Expand Up @@ -63,7 +63,7 @@ await mediator.Send(new SetBookedRecordStatusCommand(


[HttpGet("facilities/{facilityId}/bookings/records")]
[ProducesResponseType(typeof(QueryCollectionResult<BookedRecordOfFacilityDto>), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(QueryCollectionResult<BookedRecordDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetBookedRecords(
[FromRoute] Guid facilityId,
[FromQuery] GetBookedRecordsOfFacilityQueryParams @params
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Core.Queries;

namespace Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility
namespace Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQuery : IQuery<QueryCollectionResult<BookedRecordOfFacilityDto>>
public class GetBookedRecordsOfFacilityQuery : IQuery<QueryCollectionResult<BookedRecordDto>>
{
public GetBookedRecordsOfFacilityQuery(Guid facilityId, GetBookedRecordsOfFacilityQueryParams @params)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading;
using System.Threading.Tasks;
using Core.Queries;

namespace Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQueryHandler : IQueryHandler<GetBookedRecordsOfFacilityQuery, QueryCollectionResult<BookedRecordDto>>
{
private readonly IBookedRecordQueryRepository repository;

public GetBookedRecordsOfFacilityQueryHandler(IBookedRecordQueryRepository repository)
{
this.repository = repository;
}

public Task<QueryCollectionResult<BookedRecordDto>> Handle(GetBookedRecordsOfFacilityQuery request, CancellationToken cancellationToken)
{
return repository.GetBookedRecords(
request.FacilityId,
request.Params
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Core.Queries;

namespace Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility
namespace Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQueryParams : QueryParams
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Threading.Tasks;

using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfCustomer;

using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility;
using Core.Queries;

namespace Accessibility.Application.BookedRecords.Queries
{
public interface IBookedRecordQueryRepository
{
Task<QueryCollectionResult<BookedRecordDto>> GetBookedRecords(Guid customerId, GetBookedRecordsOfCustomerQueryParams @params);
Task<QueryCollectionResult<BookedRecordDto>> GetBookedRecords(Guid facilityId, GetBookedRecordsOfFacilityQueryParams @params);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Accessibility.Application.Bookings.Queries.GetAvailableBookingTerms;
using Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility;
using Accessibility.Domain.Bookings;
using Accessibility.Domain.SharedKernel;
using Core.Queries;
Expand All @@ -11,7 +10,6 @@ namespace Accessibility.Application.Bookings.Queries
{
public interface IBookingQueryRepository
{
Task<QueryCollectionResult<BookedRecordOfFacilityDto>> GetBookedRecords(Guid facilityId, GetBookedRecordsOfFacilityQueryParams @params);
Task<IEnumerable<BookedTerm>> GetBookedTerms(Guid facilityId, DateTime dateFrom, DateTime dateTo);
Task<int?> GetBookingStatus(BookingId id, FacilityId facilityId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Accessibility.Application.BookedRecords.Queries;
using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfCustomer;
using Accessibility.Application.BookedRecords.Queries.GetBookedRecordsOfFacility;
using Core.Database;
using Core.Persistence.Postgres;
using Core.Queries;
Expand Down Expand Up @@ -50,5 +51,40 @@ GetBookedRecordsOfCustomerQueryParams @params
.Where("e.status", 1),
@params
);

public Task<QueryCollectionResult<BookedRecordDto>> GetBookedRecords(
Guid facilityId,
GetBookedRecordsOfFacilityQueryParams @params
) =>
GetCollectionResultAsync<BookedRecordDto>(
new Query()
.Select(
"b.booking_id as BookingId",
"r.booked_record_id as BookedRecordId",
"r.offer_id as OfferId",
"o.name as OfferName",
"r.employee_id as EmployeeId",
"e.name as EmployeeName",
"b.customer_id as CustomerId",
"r.date as DateFrom")
.ForPostgreSql(q => q.SelectRaw("r.date + r.duration * INTERVAL '1 minute' as DateTo"))
.Select(
"r.duration as Duration",
"r.status as Status",
"r.price as Price",
"r.currency as Currency")
.From(
"booking.bookings as b")
.Join(
"booking.booked_records as r", "b.booking_id", "r.booking_id")
.Join(
"facility.offers as o", "r.offer_id", "o.offer_id")
.Join(
"facility.employees as e", "r.employee_id", "e.employee_id")
.Where("b.facility_id", facilityId)
.Where("e.status", 1)
.WhereBetween("r.date", @params.DateFrom, @params.DateTo),
@params
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
using System.Threading.Tasks;
using Accessibility.Application.Bookings.Queries;
using Accessibility.Application.Bookings.Queries.GetAvailableBookingTerms;
using Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility;
using Accessibility.Domain.Bookings;
using Accessibility.Domain.SharedKernel;
using Core.Database;
using Core.Persistence.Postgres;
using Core.Queries;
using Dapper;
using SqlKata;
using SqlKata.Extensions;

namespace Accessibility.Infrastructure.Application.Bookings
{
Expand All @@ -21,41 +17,6 @@ public BookingQueryRepository(ISqlConnectionFactory sqlConnectionFactory) : base
{
}

public Task<QueryCollectionResult<BookedRecordOfFacilityDto>> GetBookedRecords(
Guid facilityId,
GetBookedRecordsOfFacilityQueryParams @params
) =>
GetCollectionResultAsync<BookedRecordOfFacilityDto>(
new Query()
.Select(
"b.booking_id as BookingId",
"r.booked_record_id as BookedRecordId",
"r.offer_id as OfferId",
"o.name as OfferName",
"r.employee_id as EmployeeId",
"e.name as EmployeeName",
"b.customer_id as CustomerId",
"r.date as DateFrom")
.ForPostgreSql(q => q.SelectRaw("r.date + r.duration * INTERVAL '1 minute' as DateTo"))
.Select(
"r.duration as Duration",
"r.status as Status",
"r.price as Price",
"r.currency as Currency")
.From(
"booking.bookings as b")
.Join(
"booking.booked_records as r", "b.booking_id", "r.booking_id")
.Join(
"facility.offers as o", "r.offer_id", "o.offer_id")
.Join(
"facility.employees as e", "r.employee_id", "e.employee_id")
.Where("b.facility_id", facilityId)
.Where("e.status", 1)
.WhereBetween("r.date", @params.DateFrom, @params.DateTo),
@params
);

public async Task<IEnumerable<BookedTerm>> GetBookedTerms(Guid facilityId, DateTime dateFrom, DateTime dateTo)
{
var now = DateTime.Now;
Expand Down

0 comments on commit cd4ba02

Please sign in to comment.