Skip to content

Commit

Permalink
#126 Accessibility - query metadata in booked records
Browse files Browse the repository at this point in the history
  • Loading branch information
kgniazdowski committed Nov 30, 2021
1 parent 938c66e commit 371112f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System;
using System.Collections.Generic;
using Core.Queries;

namespace Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQuery : IQuery<List<BookedRecordOfFacilityDto>>
public class GetBookedRecordsOfFacilityQuery : IQuery<QueryCollectionResult<BookedRecordOfFacilityDto>>
{
public GetBookedRecordsOfFacilityQuery(Guid facilityId, DateTime dateFrom, DateTime dateTo)
public GetBookedRecordsOfFacilityQuery(Guid facilityId, GetBookedRecordsOfFacilityQueryParams @params)
{
FacilityId = facilityId;
DateFrom = dateFrom;
DateTo = dateTo;
Params = @params;
}

public Guid FacilityId { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public Guid FacilityId { get; }
public GetBookedRecordsOfFacilityQueryParams Params { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,24 @@
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using System.Collections.Generic;
using Core.Database;
using Core.Queries;

namespace Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQueryHandler : IQueryHandler<GetBookedRecordsOfFacilityQuery, List<BookedRecordOfFacilityDto>>
public class GetBookedRecordsOfFacilityQueryHandler : IQueryHandler<GetBookedRecordsOfFacilityQuery, QueryCollectionResult<BookedRecordOfFacilityDto>>
{
private readonly ISqlConnectionFactory sqlConnectionFactory;
private readonly IBookingQueryRepository repository;

public GetBookedRecordsOfFacilityQueryHandler(ISqlConnectionFactory sqlConnectionFactory)
public GetBookedRecordsOfFacilityQueryHandler(IBookingQueryRepository repository)
{
this.sqlConnectionFactory = sqlConnectionFactory;
this.repository = repository;
}

public async Task<List<BookedRecordOfFacilityDto>> Handle(GetBookedRecordsOfFacilityQuery request, CancellationToken cancellationToken)
public Task<QueryCollectionResult<BookedRecordOfFacilityDto>> Handle(GetBookedRecordsOfFacilityQuery request, CancellationToken cancellationToken)
{
var connection = sqlConnectionFactory.GetConnection();

return (await connection.QueryAsync<BookedRecordOfFacilityDto>(
@"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,
null as EmployeeName,
b.customer_id as CustomerId,
r.date as DateFrom,
r.date + r.duration * INTERVAL '1 minute' as DateTo,
r.duration as Duration,
r.status as Status,
r.price as Price,
r.currency as Currency
FROM
booking.bookings b
INNER JOIN
booking.booked_records r ON b.booking_id = r.booking_id
INNER JOIN
facility.offers o ON r.offer_id = o.offer_id
WHERE
b.facility_id = @FacilityId AND
r.date BETWEEN @DateFrom::timestamp AND @DateTo::timestamp",
new
{
request.FacilityId,
request.DateFrom,
request.DateTo
}))
.AsList();
return repository.GetBookedRecords(
request.FacilityId,
request.Params
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Core.Queries;

namespace Accessibility.Application.Bookings.Queries.GetBookedRecordsOfFacility
{
public class GetBookedRecordsOfFacilityQueryParams : QueryParams
{
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
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;

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 @@ -5,6 +5,9 @@
<ProjectReference Include="..\Core.RabbitMQ.MassTransit\Core.RabbitMQ.MassTransit.csproj" />
<ProjectReference Include="..\Core.Processing\Core.Processing.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SqlKata" Version="2.3.7" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,66 @@
using System;
using System.Collections.Generic;
using System.Data;
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
{
public class BookingQueryRepository : IBookingQueryRepository
public class BookingQueryRepository : QueryRepositoryBase, IBookingQueryRepository
{
private readonly IDbConnection connection;

public BookingQueryRepository(ISqlConnectionFactory sqlConnectionFactory)
public BookingQueryRepository(ISqlConnectionFactory sqlConnectionFactory) : base(sqlConnectionFactory)
{
connection = sqlConnectionFactory.GetConnection();
}

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;

return (await connection.QueryAsync<BookedTerm>(
return (await Connection.QueryAsync<BookedTerm>(
@"SELECT
r.employee_id as EmployeeId,
r.date as DateFrom,
Expand All @@ -48,7 +84,7 @@ INNER JOIN

public async Task<int?> GetBookingStatus(BookingId id, FacilityId facilityId)
{
return (await connection.ExecuteScalarAsync<int?>(
return (await Connection.ExecuteScalarAsync<int?>(
@"SELECT
b.status
FROM
Expand Down

0 comments on commit 371112f

Please sign in to comment.