forked from rstropek/TechSummit2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrdersController.cs
44 lines (39 loc) · 1.31 KB
/
OrdersController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Shop.WebApi.DataAccess;
using Shop.WebApi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
namespace Shop.WebApi.Controllers
{
[RoutePrefix("api")]
public class OrdersController : ApiController
{
private readonly IOrderTable OrderTable;
private readonly IProductTable ProductTable;
[ImportingConstructor]
public OrdersController(IProductTable productTable, IOrderTable orderTable)
{
this.ProductTable = productTable;
this.OrderTable = orderTable;
}
[Route("orders")]
[HttpPost]
[ResponseType(typeof(IEnumerable<OrderOutput>))]
public async Task<IHttpActionResult> AddOrder([FromBody] IEnumerable<OrderInput> orderLines)
{
if (!orderLines.Any() || orderLines.Count() > 10)
{
return this.BadRequest("Order must consist of >= 1 and <= 10 items.");
}
if (orderLines.Any(ol => ol.ProductId == 4711))
{
throw new DivideByZeroException();
}
return Created("dummy", await this.OrderTable.AddOrderLinesAsync(orderLines));
}
}
}