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

Use MER<> for CreateSpace and return Conflict/Created responses #912

Merged
merged 1 commit into from
Nov 13, 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
13 changes: 8 additions & 5 deletions src/protagonist/API/Features/Space/Requests/CreateSpace.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using API.Exceptions;
using API.Infrastructure.Requests;
using DLCS.Core;
using DLCS.Model.Customers;
using DLCS.Model.Spaces;
using MediatR;
Expand All @@ -8,7 +10,7 @@ namespace API.Features.Space.Requests;
/// <remark>
/// See Deliverator: API/Architecture/Request/API/Entities/CustomerSpaces.cs
/// </remark>
public class CreateSpace : IRequest<DLCS.Model.Spaces.Space>
public class CreateSpace : IRequest<ModifyEntityResult<DLCS.Model.Spaces.Space>>
{
public string Name { get; }
public int Customer { get; }
Expand All @@ -25,7 +27,7 @@ public CreateSpace(int customer, string name)
}


public class CreateSpaceHandler : IRequestHandler<CreateSpace, DLCS.Model.Spaces.Space>
public class CreateSpaceHandler : IRequestHandler<CreateSpace, ModifyEntityResult<DLCS.Model.Spaces.Space>>
{
private readonly ISpaceRepository spaceRepository;
private readonly ICustomerRepository customerRepository;
Expand All @@ -38,22 +40,23 @@ public CreateSpaceHandler(
this.customerRepository = customerRepository;
}

public async Task<DLCS.Model.Spaces.Space> Handle(CreateSpace request, CancellationToken cancellationToken)
public async Task<ModifyEntityResult<DLCS.Model.Spaces.Space>> Handle(CreateSpace request, CancellationToken cancellationToken)
{
await ValidateRequest(request);

var existing = await spaceRepository.GetSpace(request.Customer, request.Name, cancellationToken);
if (existing != null)
{
throw new BadRequestException("A space with this name already exists.");
return ModifyEntityResult<DLCS.Model.Spaces.Space>.Failure("A space with this name already exists.",
WriteResult.Conflict);
}

var newSpace = await spaceRepository.CreateSpace(
request.Customer, request.Name, request.ImageBucket,
request.Tags, request.Roles, request.MaxUnauthorised,
cancellationToken);

return newSpace;
return ModifyEntityResult<DLCS.Model.Spaces.Space>.Success(newSpace, WriteResult.Created);
}

private async Task ValidateRequest(CreateSpace request)
Expand Down
24 changes: 5 additions & 19 deletions src/protagonist/API/Features/Space/SpaceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,11 @@ public async Task<IActionResult> CreateSpace(
Tags = space.DefaultTags ?? Array.Empty<string>(),
MaxUnauthorised = space.MaxUnauthorised
};

try
{
var newDbSpace = await Mediator.Send(command, cancellationToken);
var newApiSpace = newDbSpace.ToHydra(GetUrlRoots().BaseUrl);

if (!newApiSpace.Id.HasText())
{
return this.HydraProblem("New space not assigned an ID",
null, 500, "Bad Request");
}

return this.HydraCreated(newApiSpace);
}
catch (BadRequestException badRequestException)
{
return this.HydraProblem(badRequestException.Message,
null, badRequestException.StatusCode, "Bad Request");
}

return await HandleUpsert(command,
s => s.ToHydra(GetUrlRoots().BaseUrl),
errorTitle: "Failed to create space",
cancellationToken: cancellationToken);
}

/// <summary>
Expand Down
Loading