Created
September 18, 2012 23:19
-
-
Save ChrisMissal/3746676 to your computer and use it in GitHub Desktop.
A helper class to nicely wrap service calls and provide JSON responses back.
This file contains 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
private class Helper<T> : IDisposable where T : CheckoutInformation | |
{ | |
private readonly T _input; | |
private readonly IOrderService _orderService; | |
private readonly Func<object, JsonResult> _jsonResultSelector; | |
private object _data; | |
public CheckoutHelper(T input, IOrderService orderService, Func<object, JsonResult> jsonResultSelector) | |
{ | |
_input = input; | |
_orderService = orderService; | |
_jsonResultSelector = jsonResultSelector; | |
} | |
public ActionResult Response { get; private set; } | |
public void Dispose() | |
{ | |
Response = _jsonResultSelector(_data); | |
} | |
public void Action(Action<T, IOrderService, Order> action) | |
{ | |
try | |
{ | |
var order = _orderService.GetOrder(); | |
if (order == null) | |
{ | |
_data = new { success = false, redirect = "/cart" }; | |
return; | |
} | |
if (_input.ClientId != order.Id) | |
{ | |
Response = new HttpStatusCodeResult((int) HttpStatusCode.Unauthorized); | |
return; | |
} | |
action(_input, _orderService, order); | |
_data = order; | |
} | |
catch (Exception ex) | |
{ | |
_data = new { success = false, message = ex.Message }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment