Created
January 21, 2018 22:38
-
-
Save aramkoukia/bdff6acfc561bbccde03857353129668 to your computer and use it in GitHub Desktop.
Orders Controller
This file contains hidden or 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
| using System; | |
| using System.Net; | |
| using System.Net.Http; | |
| using System.Web.Http; | |
| using Sales.Service.DataTransferObjects.Commands; | |
| using Sales.Service.MicroServices.Order.Commands; | |
| namespace Sales.Service.Controllers | |
| { | |
| public class OrdersController : ApiController | |
| { | |
| [HttpPost] | |
| public IHttpActionResult Post(PlaceOrderCommand cmd) | |
| { | |
| if (Guid.Empty.Equals(cmd.Id)) | |
| { | |
| var response = new HttpResponseMessage(HttpStatusCode.Forbidden) { | |
| Content = new StringContent("order information must be supplied in the POST body"), | |
| ReasonPhrase = "Missing Order Id" | |
| }; | |
| throw new HttpResponseException(response); | |
| } | |
| var command = new StartNewOrder(cmd.Id, cmd.ProductId, cmd.Quantity); | |
| try | |
| { | |
| ServiceLocator.OrderCommands.Handle(command); | |
| var link = new Uri(string.Format("http://localhost:8182/api/orders/{0}", command.Id)); | |
| return Created(link, command); | |
| } | |
| catch (ArgumentException argEx) | |
| { | |
| return BadRequest(argEx.Message); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment