Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Web;
- using System.Web.Http;
- using System.Web.Http.Results;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using MySwoleMate300WebApiFinal.Controllers;
- using MySwoleMate300WebApiFinal.Models;
- using MySwoleMate300WebApiFinal.Models.Meal_List;
- using Newtonsoft.Json;
- namespace MySwoleMate300WebApiFinal
- {
- public partial class AddMeal : System.Web.UI.Page
- {
- protected string jsonFoodString { get; set; }
- protected void Page_Load(object sender, EventArgs e)
- {
- //Check on inital Get reueset when page_loads
- if (Request.HttpMethod == "GET")
- {
- BindData();
- }
- //For checking for POST requests (Useful for the addmeal.js Ajax request
- else if (Request.HttpMethod == "POST")
- {
- MealFoodViewModel mealFood = JsonConvert.DeserializeObject<MealFoodViewModel>(Request.Form["data"]);
- if (mealFood.FoodID.Count > 0)
- {
- AddMealButton_Click(mealFood);
- }
- else
- {
- ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
- }
- }
- }
- private HttpClient CreateClient()
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:63591/");
- client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
- return client;
- }
- protected void AddMealButton_Click(MealFoodViewModel addmealFormHttpRequest)
- {
- int mealID = 0;
- string addmealFormHttpRequestString = JsonConvert.SerializeObject(addmealFormHttpRequest);
- using (HttpClient client = CreateClient())
- {
- HttpResponseMessage response = client.PostAsJsonAsync("api/Meals", addmealFormHttpRequestString).Result;
- if (response.IsSuccessStatusCode)
- {
- string foodstring = response.Content.ReadAsStringAsync().Result;
- Meal mealView = JsonConvert.DeserializeObject<Meal>(foodstring); ;
- mealID = mealView.MealID;
- }
- else
- {
- ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
- }
- }
- using (HttpClient client = CreateClient())
- {
- HttpResponseMessage response = client.PostAsJsonAsync("api/MealFoods/" + mealID, addmealFormHttpRequestString).Result;
- if (response.IsSuccessStatusCode)
- {
- if (Response.IsClientConnected)
- {
- Response.Redirect("~/mealplanner.aspx", false);
- }
- else
- {
- Response.End();
- ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
- }
- }
- else
- {
- ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
- }
- }
- }
- protected void BindData()
- {
- var client = CreateClient();
- HttpResponseMessage response = client.GetAsync("api/MealCategories/").Result;
- if (response.IsSuccessStatusCode)
- {
- string foodstring = response.Content.ReadAsStringAsync().Result;
- List<CategoriesView> categories = JsonConvert.DeserializeObject<List<CategoriesView>>(foodstring);
- Dictionary<int, string> categoryDictionary = categories.ToDictionary(x => x.MealCategoryID, x => x.MealCategoryName);
- SortedDictionary<int, string> sortedMealDictionary = new SortedDictionary<int, string>(categoryDictionary);
- AddCategoryName.DataSource = sortedMealDictionary;
- AddCategoryName.DataTextField = "Value";
- AddCategoryName.DataValueField = "Key";
- AddCategoryName.DataBind();
- ListItem item = new ListItem("--Select--", "0");
- AddCategoryName.Items.Insert(0, item);
- }
- else
- {
- ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
- }
- ToolTips();
- }
- protected void ToolTips()
- {
- AddMealName.ToolTip = "<span style ='color:red'>*</span> Food Name must be between 1 and 30 characters in length <br/>" +
- "<span style ='color:red'>*</span> Food Name must not contain any special characters";
- AddCategoryName.ToolTip = "<span style ='color:red'>*</span> Select a meal for your training";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement