Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round down the timestamps returned to the whole second for PresentationCollection #47

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/IIIFPresentation/API.Tests/Integration/GetCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net;
#nullable disable

using System.Net;
using API.Tests.Integration.Infrastructure;
using Core.Response;
using FluentAssertions;
Expand All @@ -8,8 +10,6 @@
using Test.Helpers.Helpers;
using Test.Helpers.Integration;

#nullable disable

namespace API.Tests.Integration;

[Trait("Category", "Integration")]
Expand Down Expand Up @@ -137,7 +137,7 @@ public async Task Get_RootFlat_ReturnsEntryPointFlat_WhenAuthAndHeader()
// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);

var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();
var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
Expand All @@ -160,7 +160,7 @@ public async Task Get_ChildFlat_ReturnsEntryPointFlat_WhenCalledByChildId()
// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);

var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();
var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
Expand All @@ -184,7 +184,7 @@ public async Task Get_PrivateChild_ReturnsCorrectlyFlatAndHierarchical()
var flatResponse = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var hierarchicalResponse = await httpClient.AsCustomer(1).GetAsync("1/non-public");

var flatCollection = await flatResponse.ReadAsPresentationJsonAsync<FlatCollection>();
var flatCollection = await flatResponse.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
flatResponse.StatusCode.Should().Be(HttpStatusCode.OK);
Expand All @@ -207,8 +207,8 @@ public async Task Get_RootFlat_ReturnsItems_WhenCalledWithPageSize()

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -227,8 +227,8 @@ public async Task Get_ChildFlat_ReturnsReducedItems_WhenCalledWithSmallPageSize(

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -248,8 +248,8 @@ public async Task Get_RootFlat_ReturnsSecondPage_WhenCalledWithSmallPageSize()

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -269,8 +269,8 @@ public async Task Get_RootFlat_ReturnsDefaults_WhenCalledWithZeroPage()

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -289,8 +289,8 @@ public async Task Get_RootFlat_ReturnsMaxPageSize_WhenCalledPageSizeExceedsMax()

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -313,8 +313,8 @@ public async Task Get_ChildFlat_ReturnsCorrectItem_WhenCalledWithSmallPageSizeAn

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -338,8 +338,8 @@ public async Task Get_RootFlat_ReturnsFirstPageWithSecondItem_WhenCalledWithSmal

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand All @@ -361,8 +361,8 @@ public async Task Get_ChildFlat_IgnoresOrderBy_WhenCalledWithInvalidOrderBy()

// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);
var collection = await response.ReadAsPresentationJsonAsync<FlatCollection>();

var collection = await response.ReadAsPresentationJsonAsync<PresentationCollection>();

// Assert
collection.TotalItems.Should().Be(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task CreateCollection_CreatesCollection_WhenAllValuesProvided()
// Act
var response = await httpClient.AsCustomer(1).SendAsync(requestMessage);

var responseCollection = await response.ReadAsPresentationResponseAsync<FlatCollection>();
var responseCollection = await response.ReadAsPresentationResponseAsync<PresentationCollection>();

var fromDatabase = dbContext.Collections.First(c => c.Id == responseCollection!.Id!.Split('/', StringSplitOptions.TrimEntries).Last());

Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task CreateCollection_FailsToCreateCollection_WhenIsStorageCollecti
public async Task CreateCollection_FailsToCreateCollection_WhenDuplicateSlug()
{
// Arrange
var collection = new FlatCollection()
var collection = new PresentationCollection()
{
Behavior = new List<string>()
{
Expand Down Expand Up @@ -274,7 +274,7 @@ public async Task UpdateCollection_UpdatesCollection_WhenAllValuesProvided()
// Act
var response = await httpClient.AsCustomer(1).SendAsync(updateRequestMessage);

var responseCollection = await response.ReadAsPresentationResponseAsync<FlatCollection>();
var responseCollection = await response.ReadAsPresentationResponseAsync<PresentationCollection>();

var fromDatabase = dbContext.Collections.First(c => c.Id == responseCollection!.Id!.Split('/', StringSplitOptions.TrimEntries).Last());

Expand Down Expand Up @@ -318,7 +318,7 @@ public async Task UpdateCollection_CreatesCollection_WhenUnknownCollectionIdProv
// Act
var response = await httpClient.AsCustomer(1).SendAsync(updateRequestMessage);

var responseCollection = await response.ReadAsPresentationResponseAsync<FlatCollection>();
var responseCollection = await response.ReadAsPresentationResponseAsync<PresentationCollection>();

var fromDatabase = dbContext.Collections.First(c => c.Id == responseCollection!.Id!.Split('/', StringSplitOptions.TrimEntries).Last());

Expand Down Expand Up @@ -420,7 +420,7 @@ public async Task UpdateCollection_UpdatesCollection_WhenAllValuesProvidedWithou
// Act
var response = await httpClient.AsCustomer(1).SendAsync(updateRequestMessage);

var responseCollection = await response.ReadAsPresentationResponseAsync<FlatCollection>();
var responseCollection = await response.ReadAsPresentationResponseAsync<PresentationCollection>();

var fromDatabase = dbContext.Collections.First(c => c.Id == responseCollection!.Id!.Split('/', StringSplitOptions.TrimEntries).Last());

Expand Down
6 changes: 3 additions & 3 deletions src/IIIFPresentation/API/Converters/CollectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
return collection;
}

public static FlatCollection ToFlatCollection(this Models.Database.Collections.Collection dbAsset,
public static PresentationCollection ToFlatCollection(this Models.Database.Collections.Collection dbAsset,
UrlRoots urlRoots, int pageSize, int currentPage, int totalItems,
List<Models.Database.Collections.Collection>? items, string? orderQueryParam = null)
{
Expand Down Expand Up @@ -97,8 +97,8 @@
}
],

Created = dbAsset.Created,
Modified = dbAsset.Modified,
Created = dbAsset.Created.Floor(DateTimeX.Precision.Second),
Modified = dbAsset.Modified.Floor(DateTimeX.Precision.Second),
CreatedBy = dbAsset.CreatedBy,
ModifiedBy = dbAsset.ModifiedBy
};
Expand All @@ -110,7 +110,7 @@
{
var view = new View()
{
Id = dbAsset.GenerateFlatCollectionViewId(urlRoots, currentPage, pageSize, orderQueryParam),

Check warning on line 113 in src/IIIFPresentation/API/Converters/CollectionConverter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'orderQueryParam' in 'string CollectionHelperX.GenerateFlatCollectionViewId(Collection collection, UrlRoots urlRoots, int currentPage, int pageSize, string orderQueryParam)'.

Check warning on line 113 in src/IIIFPresentation/API/Converters/CollectionConverter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'orderQueryParam' in 'string CollectionHelperX.GenerateFlatCollectionViewId(Collection collection, UrlRoots urlRoots, int currentPage, int pageSize, string orderQueryParam)'.
Type = PresentationType.PartialCollectionView,
Page = currentPage,
PageSize = pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace API.Features.Storage.Helpers;

public static class PresentationContextX
{
public static async Task<ModifyEntityResult<FlatCollection>?> TrySaveCollection(
public static async Task<ModifyEntityResult<PresentationCollection>?> TrySaveCollection(
this PresentationContext dbContext,
int customerId,
ILogger logger,
Expand All @@ -26,11 +26,11 @@ public static class PresentationContextX

if (ex.IsCustomerIdSlugParentViolation())
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
$"The collection could not be created due to a duplicate slug value", WriteResult.Conflict);
}
return ModifyEntityResult<FlatCollection>.Failure(

return ModifyEntityResult<PresentationCollection>.Failure(
$"The collection could not be created");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace API.Features.Storage.Requests;

public class CreateCollection(int customerId, UpsertFlatCollection collection, UrlRoots urlRoots)
: IRequest<ModifyEntityResult<FlatCollection>>
: IRequest<ModifyEntityResult<PresentationCollection>>
{
public int CustomerId { get; } = customerId;

Expand All @@ -30,21 +30,21 @@ public class CreateCollectionHandler(
PresentationContext dbContext,
ILogger<CreateCollection> logger,
IOptions<ApiSettings> options)
: IRequestHandler<CreateCollection, ModifyEntityResult<FlatCollection>>
: IRequestHandler<CreateCollection, ModifyEntityResult<PresentationCollection>>
{
private readonly ApiSettings settings = options.Value;

private const int CurrentPage = 1;

public async Task<ModifyEntityResult<FlatCollection>> Handle(CreateCollection request, CancellationToken cancellationToken)
public async Task<ModifyEntityResult<PresentationCollection>> Handle(CreateCollection request, CancellationToken cancellationToken)
{
// check parent exists
var parentCollection = await dbContext.RetrieveCollection(request.CustomerId,
request.Collection.Parent.GetLastPathElement(), cancellationToken);

if (parentCollection == null)
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
$"The parent collection could not be found", WriteResult.Conflict);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(CreateCollection re
collection.FullPath = CollectionRetrieval.RetrieveFullPathForCollection(collection, dbContext);
}

return ModifyEntityResult<FlatCollection>.Success(
return ModifyEntityResult<PresentationCollection>.Success(
collection.ToFlatCollection(request.UrlRoots, settings.PageSize, CurrentPage, 0, []), // there can be no items attached to this, as it's just been created
WriteResult.Created);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace API.Features.Storage.Requests;

public class UpsertCollection(int customerId, string collectionId, UpsertFlatCollection collection, UrlRoots urlRoots,
string? eTag)
: IRequest<ModifyEntityResult<FlatCollection>>
: IRequest<ModifyEntityResult<PresentationCollection>>
{
public int CustomerId { get; } = customerId;

Expand All @@ -37,13 +37,13 @@ public class UpsertCollectionHandler(
IETagManager eTagManager,
ILogger<CreateCollection> logger,
IOptions<ApiSettings> options)
: IRequestHandler<UpsertCollection, ModifyEntityResult<FlatCollection>>
: IRequestHandler<UpsertCollection, ModifyEntityResult<PresentationCollection>>
{
private readonly ApiSettings settings = options.Value;

private const int DefaultCurrentPage = 1;

public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection request,
public async Task<ModifyEntityResult<PresentationCollection>> Handle(UpsertCollection request,
CancellationToken cancellationToken)
{
var databaseCollection =
Expand All @@ -53,7 +53,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection re
{
if (request.ETag is not null)
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
"ETag should not be added when inserting a collection via PUT", WriteResult.PreConditionFailed);
}

Expand All @@ -63,7 +63,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection re
request.Collection.Parent.GetLastPathElement(), cancellationToken);
if (parentCollection == null)
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
"The parent collection could not be found", WriteResult.BadRequest);
}

Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection re

if (request.ETag != eTag)
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
"ETag does not match", WriteResult.PreConditionFailed);
}

Expand All @@ -103,7 +103,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection re

if (parentCollection == null)
{
return ModifyEntityResult<FlatCollection>.Failure(
return ModifyEntityResult<PresentationCollection>.Failure(
$"The parent collection could not be found", WriteResult.BadRequest);
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public async Task<ModifyEntityResult<FlatCollection>> Handle(UpsertCollection re
CollectionRetrieval.RetrieveFullPathForCollection(databaseCollection, dbContext);
}

return ModifyEntityResult<FlatCollection>.Success(
return ModifyEntityResult<PresentationCollection>.Success(
databaseCollection.ToFlatCollection(request.UrlRoots, settings.PageSize, DefaultCurrentPage, total,
await items.ToListAsync(cancellationToken: cancellationToken)));
}
Expand Down
12 changes: 12 additions & 0 deletions src/IIIFPresentation/Core/Helpers/DateTimeX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Core.Helpers;

public static class DateTimeX
{
public static class Precision
{
public static readonly TimeSpan Second = TimeSpan.FromSeconds(1);
}

public static DateTime Floor(this DateTime dateTime, TimeSpan interval) =>
dateTime.AddTicks(-(dateTime.Ticks % interval.Ticks));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Models.API.Collection;

public class FlatCollection : IIIF.Presentation.V3.Collection
public class PresentationCollection : IIIF.Presentation.V3.Collection
{
[JsonProperty("@context")]
public new List<string>? Context
Expand Down
Loading