-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from dlcs/feature/deleteStorageCollection
Adding delete collection
- Loading branch information
Showing
10 changed files
with
318 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/IIIFPresentation/API/Features/Storage/Requests/DeleteCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Core; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Models.API.General; | ||
using Repository; | ||
|
||
namespace API.Features.Storage.Requests; | ||
|
||
public class DeleteCollection (int customerId, string collectionId) : IRequest<ResultMessage<DeleteResult, DeleteCollectionType>> | ||
{ | ||
public int CustomerId { get; } = customerId; | ||
|
||
public string CollectionId { get; } = collectionId; | ||
} | ||
|
||
public class DeleteCollectionHandler( | ||
PresentationContext dbContext, | ||
ILogger<CreateCollection> logger) | ||
: IRequestHandler<DeleteCollection, ResultMessage<DeleteResult, DeleteCollectionType>> | ||
{ | ||
private const string RootCollection = "root"; | ||
|
||
public async Task<ResultMessage<DeleteResult, DeleteCollectionType>> Handle(DeleteCollection request, CancellationToken cancellationToken) | ||
{ | ||
logger.LogDebug("Deleting collection {CollectionId} for customer {CustomerId}", request.CollectionId, | ||
request.CustomerId); | ||
|
||
if (request.CollectionId.Equals(RootCollection, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.BadRequest, | ||
DeleteCollectionType.CannotDeleteRootCollection, "Cannot delete a root collection"); | ||
} | ||
|
||
var collection = await dbContext.Collections.FirstOrDefaultAsync( | ||
c => c.Id == request.CollectionId && c.CustomerId == request.CustomerId, | ||
cancellationToken: cancellationToken); | ||
|
||
if (collection is null) return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.NotFound); | ||
|
||
if (collection.Parent is null) | ||
{ | ||
return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.BadRequest, | ||
DeleteCollectionType.CannotDeleteRootCollection, "Cannot delete a root collection"); | ||
} | ||
|
||
var hasItems = await dbContext.Collections.AnyAsync( | ||
c => c.CustomerId == request.CustomerId && c.Parent == collection.Id, | ||
cancellationToken: cancellationToken); | ||
|
||
if (hasItems) | ||
{ | ||
return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.BadRequest, | ||
DeleteCollectionType.CollectionNotEmpty, "Cannot delete a collection with child items"); | ||
} | ||
|
||
dbContext.Collections.Remove(collection); | ||
try | ||
{ | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
catch (DbUpdateConcurrencyException ex) | ||
{ | ||
logger.LogError(ex, "Error attempting to delete collection {CollectionId} for customer {CustomerId}", | ||
request.CollectionId, request.CustomerId); | ||
return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.Error, | ||
DeleteCollectionType.Unknown, "Error deleting collection"); | ||
} | ||
|
||
return new ResultMessage<DeleteResult, DeleteCollectionType>(DeleteResult.Deleted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.