Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/controller.cs Secret

Created May 12, 2021
Embed
What would you like to do?
[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