public ActionResult Create(string url) { if (!ModelState.IsValid) { return(View(url)); } else { var flyerViewModel = new FlyerViewModel { Url = url }; if (!_flyerRepository.IsExist(f => f.Url == url)) { var crawlObject = Function.GetAllCrawlModels(url); var merchant = new Merchant { Name = crawlObject.merchant, Url = crawlObject.merchant_url, LogoFile = crawlObject.merchant_logo }; if (!_merchantRepository.IsExist(m => m.Name == merchant.Name)) { _merchantRepository.Add(merchant); } else { merchant = _merchantRepository.GetByCondition(m => m.Name == merchant.Name).FirstOrDefault(); } foreach (var crawlcate in crawlObject.categories) { var category = new Category(); category.Name = crawlcate.name; if (!_categoryReposity.IsExist(c => c.Name == category.Name)) { _categoryReposity.Add(category); } else { category = _categoryReposity.GetByCondition(c => c.Name == category.Name).FirstOrDefault(); } var merchantCategory = new MerchantCategory { Merchant = merchant, Category = category }; var exist = _merchantCategoryRepo.GetByCondition(c => c.Category == category & c.Merchant == merchant).Count > 0 ? true : false; if (!exist) { _merchantCategoryRepo.Add(merchantCategory); merchant.MerchantCategories.Add(merchantCategory); category.MerchantCategories.Add(merchantCategory); } } foreach (var item in crawlObject.items) { if (item.current_price != null) { var product = new Product { Name = item.name, CurrentPrice = Convert.ToDecimal(item.current_price), Brand = item.brand, Description = item.description, Discount_percent = item.discount_percent.ToString(), DisplayName = item.display_name, Dist_coupon_image_url = item.dist_coupon_image_url, Image = item.large_image_url, Url = item.url, InStoreOnly = item.in_store_only, X_large_image_url = item.x_large_image_url, Sale_Story = item.sale_story, Item_Id = item.flyer_item_id, Valid_from = DateTime.Parse(item.valid_from), Valid_to = DateTime.Parse(item.valid_to) }; if (item.category_names.Count > 0) { var cate = item.category_names[0]; var proCate = _categoryReposity.GetByCondition(c => c.Name == cate).FirstOrDefault(); if (proCate == null) { proCate = new Category(); proCate.Name = cate; _categoryReposity.Add(proCate); } var merchantCategory = new MerchantCategory { Merchant = merchant, Category = proCate }; var exist = _merchantCategoryRepo.GetByCondition(c => c.Category == proCate & c.Merchant == merchant).Count > 0 ? true : false; if (!exist) { _merchantCategoryRepo.Add(merchantCategory); merchant.MerchantCategories.Add(merchantCategory); proCate.MerchantCategories.Add(merchantCategory); } product.Category = proCate; } if (!_productRepository.IsExist(p => p.Item_Id == product.Item_Id)) { _productRepository.Add(product); if (!merchant.Products.Contains(product)) { merchant.Products.Add(product); } } } } _merchantRepository.Update(merchant); var flyer = new Flyer { Url = url, Valid_from = crawlObject.valid_from, Valid_to = crawlObject.valid_to }; _flyerRepository.Add(flyer); } } return(View()); }
public async Task <IHttpActionResult> Put(int id, [FromBody] FlyerViewModel vm) { try { if (vm == null) { return(BadRequest("Flyer cannot be null")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var flyer = await DB.Flyers.FirstOrDefaultAsync(x => x.ID == id); int rangeMax = flyer.Range.Max, rangeMin = flyer.Range.Min; if (!string.IsNullOrWhiteSpace(vm.RangeMax)) { int.TryParse(new string(vm.RangeMax.Trim().Where(c => Char.IsDigit(c)).ToArray()), out rangeMax); if (rangeMax == 0) { rangeMax = flyer.Range.Max; } } if (!string.IsNullOrWhiteSpace(vm.RangeMin)) { int.TryParse(new string(vm.RangeMin.Trim().Where(c => Char.IsDigit(c)).ToArray()), out rangeMin); if (rangeMin == 0) { rangeMin = flyer.Range.Min; } } if (rangeMin > rangeMax) { rangeMin = rangeMin ^ rangeMax ^ (rangeMax = rangeMin); } var range = new Range { Max = rangeMax, Min = rangeMin }; var cat = await DB.Categories.FirstOrDefaultAsync(x => x.ID == vm.CategoryID); flyer.Name = vm.Name; flyer.Active = vm.Active; flyer.Category = cat; flyer.Range = range; var result = await DB.SaveChangesAsync(); if (result == 0) { return(Conflict()); } else { return(Ok($"Flyer {flyer.Name} updated.")); } } catch { return(InternalServerError()); } }