-
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.
- Loading branch information
Showing
9 changed files
with
358 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace GeoCop.Api.Contracts | ||
{ | ||
/// <summary> | ||
/// Request for transforming a validation to a delivery. | ||
/// </summary> | ||
public class DeliveryRequest | ||
{ | ||
/// <summary> | ||
/// The job identification provided by the validation endpoint. | ||
/// </summary> | ||
[Required] | ||
public Guid JobId { get; set; } | ||
|
||
/// <summary> | ||
/// The id of a DeliveryMandate selected by the user to reference. | ||
/// </summary> | ||
[Required] | ||
public int DeliveryMandateId { get; set; } | ||
} | ||
} |
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,83 @@ | ||
using System.Globalization; | ||
using GeoCop.Api.Contracts; | ||
using GeoCop.Api.Models; | ||
using GeoCop.Api.Validation; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace GeoCop.Api.Controllers | ||
{ | ||
/// <summary> | ||
/// Controller for declaring deliveries. | ||
/// </summary> | ||
[ApiController] | ||
[Route("api/v{version:apiVersion}/[controller]")] | ||
public class DeliveryController : ControllerBase | ||
{ | ||
private readonly ILogger<DeliveryController> logger; | ||
private readonly Context context; | ||
private readonly IValidationService validatorService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DeliveryController"/> class. | ||
/// </summary> | ||
public DeliveryController(ILogger<DeliveryController> logger, Context context, IValidationService validatorService) | ||
{ | ||
this.logger = logger; | ||
this.context = context; | ||
this.validatorService = validatorService; | ||
} | ||
|
||
/// <summary> | ||
/// Create a delivery from a validation with the status <see cref="Status.Completed"/>. | ||
/// </summary> | ||
/// <param name="declaration"><see cref="DeliveryRequest"/> containing all information for the declaration process.</param> | ||
/// <returns>Created <see cref="Delivery"/>.</returns> | ||
[HttpPost] | ||
public IActionResult Create(DeliveryRequest declaration) | ||
{ | ||
logger.LogTrace("Declaration for job <{JobId}> requested.", declaration.JobId); | ||
|
||
var job = validatorService.GetJobStatus(declaration.JobId); | ||
if (job == default) | ||
{ | ||
logger.LogTrace("No job information available for job id <{JobId}>.", declaration.JobId); | ||
return Problem($"No job information available for job id <{declaration.JobId}>", statusCode: StatusCodes.Status404NotFound); | ||
} | ||
else if (job.Status != Status.Completed) | ||
{ | ||
logger.LogTrace("Job <{JobId}> is not completed.", declaration.JobId); | ||
return Problem($"Job <{declaration.JobId}> is not completed.", statusCode: StatusCodes.Status400BadRequest); | ||
} | ||
|
||
var mandate = context.DeliveryMandates | ||
.Include(m => m.Organisations) | ||
.ThenInclude(o => o.Users) | ||
.FirstOrDefault(m => m.Id == declaration.DeliveryMandateId); | ||
|
||
var dummyUser = mandate?.Organisations.SelectMany(u => u.Users).First() ?? new User(); | ||
|
||
if (mandate is null || !mandate.Organisations.SelectMany(u => u.Users).Any(u => u.AuthIdentifier.Equals(dummyUser.AuthIdentifier, StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
logger.LogTrace("User <{AuthIdentifier}> is not authorized to create a delivery for mandate <{MandateId}>.", dummyUser, declaration.DeliveryMandateId); | ||
return Problem("Mandate with id <{declaration.DeliveryMandateId}> not found or user is not authorized.", statusCode: StatusCodes.Status404NotFound); | ||
} | ||
|
||
var delivery = new Delivery | ||
{ | ||
DeliveryMandate = mandate, | ||
DeclaringUser = dummyUser, | ||
Assets = new List<Asset>(), | ||
}; | ||
|
||
var entityEntry = context.Deliveries.Add(delivery); | ||
context.SaveChanges(); | ||
|
||
var location = new Uri( | ||
string.Format(CultureInfo.InvariantCulture, "/api/v1/delivery/{0}", entityEntry.Entity.Id), | ||
UriKind.Relative); | ||
|
||
return Created(location, entityEntry.Entity); | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.