I have an action on my controller called DeleteLine
. This removes a line from the users basket.
My controller action looks like this:
[HttpGet]
public ActionResult DeleteLine(int orderId, int lineId)
{
var orderResult = _salesOrderService.DeleteLine(_workerContext.CurrentCustomer, orderId, lineId);
var errorMessage = "";
switch (orderResult.Type)
{
case SalesOrderResultType.Deleted:
errorMessage = "Item Removed. You now have no items in your basket.";
break;
case SalesOrderResultType.Failed:
errorMessage =
"Something went wrong and your item could not be removed from your cart. Please try again.";
break;
case SalesOrderResultType.FailedAlreadyComplete:
errorMessage =
"The item you are trying to remove is part of an order that has already been completed.";
break;
case SalesOrderResultType.NotFoundForCustomer:
errorMessage =
"The item which you are trying to delete does not belong to an order we can find for you.";
break;
}
if (errorMessage.IsNotEmpty())
return ErrorNotificationView(errorMessage);
AddSuccessNotification("Your item has been deleted successfully.", false);
if (Request.UrlReferrer != null)
return Redirect(Request.UrlReferrer.ToString());
return View();
}
Most of the actual leg work is done in _salesOrderService.DeleteLine
which does checks to see if the order id belongs to the customer, removes the selected item and then finally removes the whole order if no lines are left on it.
My Question Is: Should the service be handling this or is it up to the controller?
From my perspective, I’ve put it in the service as it means I can remove an item from a users basket anywhere with relative ease but I want to make sure this is the correct approach before continuing.
I’m in the “thin controllers” school of thought — or in this case the service should be responsible for all persistance operations — the controller is just there to be a traffic cop between HTTP requests and the service layer — sending input in, interperting results out and doing the right thing. On the flip side the service layer should be blissfully unaware it is being called via a web controller, web api, console app or whatever else you might use.
1