Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created May 12, 2021 21:54
Show Gist options
  • Save dcomartin/97747a69ba44c1610d48e3736828a1ee to your computer and use it in GitHub Desktop.
Save dcomartin/97747a69ba44c1610d48e3736828a1ee to your computer and use it in GitHub Desktop.
[SwaggerOperation(
OperationId = "AvailableForSale",
Tags = new[] { "Sales" })]
[HttpPost("/sales/products/{sku}/availableForSale")]
public async Task<IActionResult> AvailableForSale([FromRoute] string sku)
{
var product = await _db.Products.SingleOrDefaultAsync(x => x.Sku == sku);
if (product == null)
{
return NotFound();
}
if (product.ForSale)
{
return Problem(
"Product is already Available For Sale.",
$"/sales/products/{sku}/availableForSale",
400,
"Cannot set product as available.",
"http://example.com/problems/already-available");
}
if (product.QuantityOnHand <= 0)
{
return Problem(
"Product has no Quantity on Hand.",
$"/sales/products/{sku}/availableForSale",
400, "Cannot set product as available.",
"http://example.com/problems/no-quantity");
}
product.ForSale = true;
await _db.SaveChangesAsync();
return Created(_urlHelper.Action("GetSalesProduct", "GetSalesProduct", new { sku }), null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment