public async Task <Result <ContentCrossSellModel> > UpdateContentCrossSells([FromBody] ContentCrossSellModel contentCrossSellsModel) { if (!Validate(contentCrossSellsModel)) { return(null); } var sUserId = _userManager.GetUserId(User); var userId = Convert.ToInt32(sUserId); var alreadyExists = await contentCrossSellService.GetContentCrossSellsAsync(contentCrossSellsModel.Type); var itemsToStore = new List <ContentCrossSell>(); for (var i = 0; i < contentCrossSellsModel.Items.Count; i++) { ContentCrossSell item; if (contentCrossSellsModel.Items[i].Id > 0) { item = alreadyExists.Single(x => x.Id == contentCrossSellsModel.Items[i].Id); item.Title = contentCrossSellsModel.Items[i].Title; item.ImageUrl = contentCrossSellsModel.Items[i].ImageUrl; item.IdSku = contentCrossSellsModel.Items[i].IdSku; item.IdEditedBy = userId; item.DateEdited = DateTime.Now; item.Order = i + 1; } else { item = new ContentCrossSell() { Type = contentCrossSellsModel.Items[i].Type, Title = contentCrossSellsModel.Items[i].Title, ImageUrl = contentCrossSellsModel.Items[i].ImageUrl, IdSku = contentCrossSellsModel.Items[i].IdSku, IdEditedBy = userId, DateCreated = DateTime.Now, DateEdited = DateTime.Now, Order = i + 1 }; } itemsToStore.Add(item); } var contentCrossSells = await contentCrossSellService.AddUpdateContentCrossSellsAsync(itemsToStore); return(await PopulateContentCrossSellModel(contentCrossSells, contentCrossSellsModel.Type)); }
private async Task <ContentCrossSellItemModel> PopulateContentCrossSellItemModel(ContentCrossSell item, ContentCrossSellType type) { var sku = await productService.GetSkuAsync(item.IdSku); if (sku != null) { return(new ContentCrossSellItemModel() { Type = item.Type, Title = item.Title, ImageUrl = item.ImageUrl, IdSku = item.IdSku, Id = item.Id, SkuCode = sku.Code, RetailPrice = sku.Price, WholesalePrice = sku.WholesalePrice }); } else { return(new ContentCrossSellItemModel() { Type = type }); } }