public RecipeDetailsForm(recipeClientForm mainWindow, List<RecipesHeaderData> allRecipes, List<CategoryHeaderData> allCategories, ListView.SelectedListViewItemCollection selectedRecipe) { this.allRecipes = allRecipes; this.allCategories = allCategories; this.mainWindow = mainWindow; this.selectedRecipe = selectedRecipe; InitializeComponent(); foreach (ListViewItem recipe in selectedRecipe) { string title = recipe.SubItems[1].Text; string recipeId = recipe.Tag.ToString(); tbTitle.Text = title; tbFilePath.Text = recipe.SubItems[0].Text; tbPrepareTime.Text = recipe.SubItems[3].Text; tbIngredients.Text = recipe.SubItems[2].Text; foreach (var category in allCategories) { cbCategory.Items.Add(category.DisplayName); } currentRecipe = allRecipes.Where(r => r.ID.ToString() == recipeId).First(); var currentCategory = allCategories.Where(c => c.ID == currentRecipe.Category_ID).First(); cbCategory.SelectedItem = currentCategory.DisplayName; tbHowToPrepare.Text = currentRecipe.HowToPrepare; } }
private async void addRecipe() { using (HttpClient client = new HttpClient()) { var category = allCategories.Where(c => c.DisplayName == cbCategory.Text).First(); var friendlyUrl = FriendlyUrlHelper.RemoveDiacritics(tbTitle.Text); RecipesHeaderData recipeHeader = new RecipesHeaderData() { HowToPrepare = tbHowToPrepare.Text, Ingredients = tbIngredients.Text, PrepareTime = tbPrepareTime.Text, Title = tbTitle.Text, Category_ID = category.ID, FriendlyUrl = friendlyUrl, PictureUrl = "default" }; var postResponse = await client.PostAsJsonAsync("https://localhost:44300/api/Recipes", recipeHeader); int recipeId = await postResponse.Content.ReadAsAsync<int>(); string pictureUrl = GetFileName(recipeId.ToString(), tbFilePath.Text); if (pictureUrl != null) { string webImagePath = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Web", "Upload", "Images", pictureUrl); File.Copy(tbFilePath.Text, webImagePath); RecipesHeaderData recipeHeaderUpdate = new RecipesHeaderData() { ID = recipeId, PictureUrl = pictureUrl, HowToPrepare = tbHowToPrepare.Text, Ingredients = tbIngredients.Text, PrepareTime = tbPrepareTime.Text, Title = tbTitle.Text, Category_ID = category.ID, FriendlyUrl = friendlyUrl }; var putResponse = await client.PutAsJsonAsync("https://localhost:44300/api/Recipes", recipeHeaderUpdate); bool returnValue = await putResponse.Content.ReadAsAsync<bool>(); } this.Close(); } }
private async void updateRecipe() { using (HttpClient client = new HttpClient()) { var category = allCategories.Where(c => c.DisplayName == cbCategory.Text).First(); RecipesHeaderData recipeHeader = new RecipesHeaderData() { ID = currentRecipe.ID, HowToPrepare = tbHowToPrepare.Text, Ingredients = tbIngredients.Text, PrepareTime = tbPrepareTime.Text, Title = tbTitle.Text, Category_ID = category.ID, FriendlyUrl = currentRecipe.FriendlyUrl, PictureUrl = currentRecipe.PictureUrl, UserId = currentRecipe.UserId }; var response = await client.PutAsJsonAsync("https://localhost:44300/api/Recipes", recipeHeader); bool returnValue = await response.Content.ReadAsAsync<bool>(); this.Close(); } }