public async Task <IActionResult> Update(int id, [FromBody] Web.Request.ResourceViewModels.Order order) { try { var model = OrderMapper.MapOrderRequestToDomain(order); model.Id = id; var updatedModel = await _orderService.UpdateOrder(model); if (updatedModel.Id == -1) { return(NotFound()); } if (updatedModel.OrderState == OrderState.OrderPaid) { //updating a paid order violates domain logic //another method to handle this is to throw custom exception for domain logic violation return(new StatusCodeResult(StatusCodes.Status409Conflict)); } var response = OrderMapper.MapDomainToOrderResponse(updatedModel); response.Links = new System.Collections.Generic.List <HyperMediaLink> { new HyperMediaLink { Rel = "self", Uri = $"http://localhost:52776/api/order/{id}" }, new HyperMediaLink { Rel = "complete", Uri = $"http://localhost:52776/api/order/{id}" } }; return(Ok(response)); } catch (Exception) { return(new ObjectResult("Internal Server Error") { StatusCode = StatusCodes.Status500InternalServerError }); } }
public async Task <IActionResult> Create([FromBody] Web.Request.ResourceViewModels.Order order) { try { var model = OrderMapper.MapOrderRequestToDomain(order); var newOrder = await _orderService.CreateOrder(model); var response = OrderMapper.MapDomainToOrderResponse(newOrder); response.Links = new System.Collections.Generic.List <HyperMediaLink> { new HyperMediaLink { Rel = "self", Uri = $"http://localhost:52776/api/order/{newOrder.Id}" }, new HyperMediaLink { Rel = "update", Uri = $"http://localhost:52776/api/order/{newOrder.Id}" }, new HyperMediaLink { Rel = "complete", Uri = $"http://localhost:52776/api/order/{newOrder.Id}" } }; return(Created($"http://localhost:52776/api/order/{newOrder.Id}", response)); } catch (Exception) { return(new ObjectResult("Internal Server Error") { StatusCode = StatusCodes.Status500InternalServerError }); } }