using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using System.Web.Http.OData; | |
using Microsoft.AspNetCore.Mvc; | |
using Newtonsoft.Json.Linq; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Services.Examples.Catalog.Commands; | |
using Sitecore.Services.Examples.Catalog.Pipelines.Arguments; | |
namespace Sitecore.Services.Examples.Catalog.Controllers | |
{ | |
public class ApiController : CommerceController | |
{ | |
protected internal const string CreateUpdateSellableItemStr = "createUpdateSellableItem"; | |
protected internal const string ProductsStr = "products"; | |
public ApiController(IServiceProvider serviceProvider, CommerceEnvironment globalEnvironment) | |
: base(serviceProvider, globalEnvironment) | |
{ | |
} | |
[HttpPut] | |
[Route("CreateUpdateSellableItem()")] | |
public async Task<IActionResult> CreateUpdateSellableItem([FromBody] ODataActionParameters value) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return new BadRequestObjectResult(ModelState); | |
} | |
if (!value.ContainsKey(CreateUpdateSellableItemStr)) | |
{ | |
return new BadRequestObjectResult(value); | |
} | |
//Handling scenarios where the input is already converted into JArray (postman) or passed through as string | |
JArray products; | |
if (value[CreateUpdateSellableItemStr] is JArray == false) | |
{ | |
var payload = JObject.Parse(value[CreateUpdateSellableItemStr].ToString()); | |
products = (JArray) payload[ProductsStr]; | |
} | |
else | |
{ | |
products = value[CreateUpdateSellableItemStr] as JArray; | |
} | |
var sellableItemArguments = new List<CreateUpdateSellableItemArgument>(); | |
foreach (var product in products) | |
{ | |
var sellableItemArgument = new CreateUpdateSellableItemArgument(); | |
var productid = product["productid"].ToString(); | |
sellableItemArgument.ProductId = productid; | |
var description = product["description"].ToString(); | |
sellableItemArgument.Description = description; | |
var name = product["name"].ToString(); | |
sellableItemArgument.Name = name; | |
var displayName = product["displayname"].ToString(); | |
sellableItemArgument.DisplayName = displayName; | |
var listprice = product["listprice"].ToString(); | |
var isDecimal = decimal.TryParse(listprice, out var n); | |
if (isDecimal) | |
{ | |
sellableItemArgument.ListPrice = Convert.ToDecimal(listprice); | |
} | |
var variants = (JArray) product["variants"]; | |
foreach (var variant in variants) | |
{ | |
var variantArg = new CreateUpdateSellableItemVariantArgument(sellableItemArgument.ProductId, variant["variantid"].ToString()); | |
var variantListPrice = variant["listprice"].ToString(); | |
if (decimal.TryParse(variantListPrice, out var outPrice)) | |
{ | |
variantArg.ListPrice = Convert.ToDecimal(variantListPrice); | |
} | |
sellableItemArgument.Variants.Add(variantArg); | |
} | |
sellableItemArguments.Add(sellableItemArgument); | |
} | |
var result = await Command<CreateUpdateSellableItemCommand>().Process(CurrentContext, sellableItemArguments); | |
return new ObjectResult(result); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment