Skip to content

Commit

Permalink
Add error message if cart operation does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkyjac committed May 17, 2024
1 parent 09a6f7d commit 2e52e0f
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace DancingGoat.Controllers.Shopify
{
public class ShopifyShoppingCartController : Controller
{
private const string ERROR_MESSAGES_KEY = "ErrorMessages";

private readonly IShoppingService shoppingService;
private readonly IShopifyContentItemService contentItemService;
private readonly IWebPageUrlRetriever webPageUrlRetriever;
Expand All @@ -47,7 +49,7 @@ public async Task<IActionResult> Index()
{
var cart = await shoppingService.GetCurrentShoppingCart();
ShoppingCartContentViewModel model = null;
string[] errorMessages = TempData["ErrorMessages"] as string[] ?? [];
string[] errorMessages = TempData[ERROR_MESSAGES_KEY] as string[] ?? [];
string language = currentLanguageRetriever.Get();
string storePageUrl = (await webPageUrlRetriever.Retrieve(DancingGoatConstants.STORE_PAGE_PATH, websiteChannelContext.WebsiteChannelName, language)).RelativePath;
if (cart == null)
Expand Down Expand Up @@ -77,9 +79,9 @@ public async Task<IActionResult> Index()
public async Task<IActionResult> Update([FromForm] string variantGraphQLId, [FromForm] int quantity, [FromForm] string cartOperation)
{
var country = ShopifySharp.GraphQL.CountryCode.CZ;
var operationEnum = Enum.Parse<CartOperation>(cartOperation);

var result = operationEnum == CartOperation.Remove
if (Enum.TryParse<CartOperation>(cartOperation, out var operationEnum))
{
var result = operationEnum == CartOperation.Remove
? await shoppingService.RemoveCartItem(variantGraphQLId)
: await shoppingService.UpdateCartItem(new ShoppingCartItemParameters()
{
Expand All @@ -88,7 +90,13 @@ public async Task<IActionResult> Update([FromForm] string variantGraphQLId, [Fro
MerchandiseID = variantGraphQLId
});

AddErrorsToTempData(result);
AddErrorsToTempData(result);
}
else
{
TempData[ERROR_MESSAGES_KEY] = new string[] { $"Invalid operation name {cartOperation}." };
}

return Redirect(await GetCartUrl());
}

Expand Down Expand Up @@ -134,7 +142,7 @@ private void AddErrorsToTempData(CartOperationResult result)
{
if (!result.Success)
{
TempData["ErrorMessages"] = result.ErrorMessages.ToArray();
TempData[ERROR_MESSAGES_KEY] = result.ErrorMessages.ToArray();
}
}

Expand Down

0 comments on commit 2e52e0f

Please sign in to comment.