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 <ContentCrossSellModel> PopulateContentCrossSellModel(IList <ContentCrossSell> contentCrossSells, ContentCrossSellType type) { var model = new ContentCrossSellModel() { Type = type }; for (var i = 0; i < ContentConstants.CONTENT_CROSS_SELL_LIMIT; i++) { var item = contentCrossSells.SingleOrDefault(x => x.Order == i + 1); var modelItem = item != null ? await PopulateContentCrossSellItemModel(item, type) : new ContentCrossSellItemModel() { Type = type }; model.Items.Add(modelItem); } return(model); }