public ResponseVM AddRecepi(RecepiVM recepiVM) { ResponseVM responseVM = new ResponseVM(); //if (file.Length == 0 || file != null) //{ // responseVM.IsSuccess = false; // responseVM.Message = "File not found"; // return responseVM; //} //if (!ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(file.FileName).ToLower())) //{ // responseVM.IsSuccess = false; // responseVM.Message = "File format not proper"; // return responseVM; //} //var uploadFilesPath = Path.Combine(host.WebRootPath, "RecepiImages"); //if (!Directory.Exists(uploadFilesPath)) // Directory.CreateDirectory(uploadFilesPath); //var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); //var filePath = Path.Combine(uploadFilesPath, fileName); //using (var stream = new FileStream(filePath, FileMode.Create)) //{ // file.CopyTo(stream); //} if (recepiVM != null) { if (recepiVM.RecepiId > 0) { var data = new Recepi { RecepiId = recepiVM.RecepiId, RecepiName = recepiVM.RecepiName, RecepiDesc = recepiVM.RecepiDesc //RecepiImage = fileName }; _dbContext.Recepies.Update(data); } else { var data = new Recepi { RecepiName = recepiVM.RecepiName, RecepiDesc = recepiVM.RecepiDesc //RecepiImage = fileName }; _dbContext.Recepies.Add(data); } _dbContext.SaveChanges(); } responseVM.IsSuccess = true; responseVM.Message = "Recepi added successfully"; return(responseVM); }
//populates the page. private void Search(object sender, RoutedEventArgs e) { if (ID.Text.Length != 5 || !isDigitsOnly(ID.Text)) { MessageBox.Show("ID must be (5) numbers "); ID.Text = "ID"; } else { recepi = gethtml(ID.Text); if (recepi == null) { MessageBox.Show("404 No Recipe Found"); } else { //fill the ingreds box Ingreds.Text = convertListToStr(recepi.Fingredients); //fill the instruciton box Instruction.Text = convertListToStr(recepi.Directions); //fill the image >> NOT WORKING Img.Source = imageSorce(recepi.Pic); } } }
//updates the database with a new recipe public void writeToDB() { //MessageBox.Show("thread started"); Recepi rec = recepi; this.Dispatcher.Invoke(() => { rec.Fingredients = convertStrToList(Ingreds.Text.Trim()); }); this.Dispatcher.Invoke(() => { rec.Directions = convertStrToList(Instruction.Text.Trim()); }); if (rec != null) { try { var result1 = Collection.Document(rec.Id).SetAsync(rec).GetAwaiter().GetResult(); MessageBox.Show(rec.getjson() + "\n\n was added!"); } catch (Exception e) { MessageBox.Show(e.ToString()); } } }
public static Recepi gethtml(string id) { string url = "https://www.allrecipes.com/recipe/" + id + "/"; Recepi recepi = new Recepi(); recepi.Id = id; HtmlWeb web = new HtmlWeb(); HtmlDocument htmldoc = new HtmlDocument(); //REQUIREMENT EXCEPTION HANDLING //check if the page exists >> error 404 try { htmldoc = web.Load(url); } catch (Exception e) { MessageBox.Show(e.Message); return(null); } //checks if the path is viable if (htmldoc.DocumentNode.SelectNodes("//section[contains(@ class,'error-page')]") != null) { return(null); } else { //recipe name check and validation try { var name = htmldoc.DocumentNode.SelectNodes("//div[contains(@class, 'intro')]"); recepi.Name = name[0].InnerText.Trim(); } catch (Exception) { Console.WriteLine("no name"); recepi.Name = id; } //recipe picture check and validation try { var pic = htmldoc.DocumentNode.SelectNodes("//div[contains(@class, 'image-container')]//div//img"); recepi.Pic = pic[0].GetAttributeValue("src", ""); } catch (Exception) { Console.WriteLine("no pic"); recepi.Pic = "Pic unavailable"; } // isolate spicific ingredients validate and fill >> used for diet validation and querry try { var Ingredients = htmldoc.DocumentNode.SelectNodes("//li[contains(@class, 'ingredients-item')]//input").ToList(); foreach (var e in Ingredients) { if (e.GetAttributeValue("value", "").Length >= e.GetAttributeValue("data-ingredient", "").Length) { recepi.addIngreds(e.GetAttributeValue("data-ingredient", "").Trim()); } if (e.GetAttributeValue("value", "").Length < e.GetAttributeValue("data-ingredient", "").Length) { recepi.addIngreds(e.GetAttributeValue("value", "").Trim()); } } } catch (Exception) { Console.WriteLine("no Ingredients"); } // populate the (Full-Ingredients) array >> what will be displayed try { var s = htmldoc.DocumentNode.SelectNodes("//span[contains(@class, 'ingredients-item-name')]").ToList(); foreach (var e in s) { recepi.addFingredient(e.InnerText); } } catch (Exception) { Console.WriteLine("no Ingredients"); } // directions try { var discription = htmldoc.DocumentNode.SelectNodes("//div[contains(@class, 'section-body')]//p").ToList(); foreach (var e in discription) { recepi.adddirections(e.InnerText.Trim()); } } catch (Exception) { Console.WriteLine("no discription"); } return(recepi); } }
//pre database serializing to file was an option public static void saveToFile(Recepi rec) { File.AppendAllText("recepies.json", rec.getjson()); }