Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Web;
  8. using System.Web.Http;
  9. using System.Web.Http.Results;
  10. using System.Web.UI;
  11. using System.Web.UI.WebControls;
  12. using MySwoleMate300WebApiFinal.Controllers;
  13. using MySwoleMate300WebApiFinal.Models;
  14. using MySwoleMate300WebApiFinal.Models.Meal_List;
  15. using Newtonsoft.Json;
  16.  
  17. namespace MySwoleMate300WebApiFinal
  18. {
  19. public partial class AddMeal : System.Web.UI.Page
  20. {
  21. protected string jsonFoodString { get; set; }
  22.  
  23. protected void Page_Load(object sender, EventArgs e)
  24. {
  25. //Check on inital Get reueset when page_loads
  26. if (Request.HttpMethod == "GET")
  27. {
  28. BindData();
  29. }
  30. //For checking for POST requests (Useful for the addmeal.js Ajax request
  31. else if (Request.HttpMethod == "POST")
  32. {
  33. MealFoodViewModel mealFood = JsonConvert.DeserializeObject<MealFoodViewModel>(Request.Form["data"]);
  34. if (mealFood.FoodID.Count > 0)
  35. {
  36. AddMealButton_Click(mealFood);
  37. }
  38. else
  39. {
  40. ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
  41. }
  42. }
  43. }
  44.  
  45. private HttpClient CreateClient()
  46. {
  47. HttpClient client = new HttpClient();
  48. client.BaseAddress = new Uri("http://localhost:63591/");
  49. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  50. return client;
  51. }
  52.  
  53. protected void AddMealButton_Click(MealFoodViewModel addmealFormHttpRequest)
  54. {
  55. int mealID = 0;
  56. string addmealFormHttpRequestString = JsonConvert.SerializeObject(addmealFormHttpRequest);
  57.  
  58. using (HttpClient client = CreateClient())
  59. {
  60. HttpResponseMessage response = client.PostAsJsonAsync("api/Meals", addmealFormHttpRequestString).Result;
  61.  
  62. if (response.IsSuccessStatusCode)
  63. {
  64. string foodstring = response.Content.ReadAsStringAsync().Result;
  65. Meal mealView = JsonConvert.DeserializeObject<Meal>(foodstring); ;
  66. mealID = mealView.MealID;
  67. }
  68. else
  69. {
  70. ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
  71. }
  72. }
  73.  
  74. using (HttpClient client = CreateClient())
  75. {
  76. HttpResponseMessage response = client.PostAsJsonAsync("api/MealFoods/" + mealID, addmealFormHttpRequestString).Result;
  77.  
  78. if (response.IsSuccessStatusCode)
  79. {
  80. if (Response.IsClientConnected)
  81. {
  82. Response.Redirect("~/mealplanner.aspx", false);
  83. }
  84. else
  85. {
  86. Response.End();
  87. ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
  88. }
  89. }
  90. else
  91. {
  92. ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
  93. }
  94. }
  95. }
  96.  
  97. protected void BindData()
  98. {
  99. var client = CreateClient();
  100. HttpResponseMessage response = client.GetAsync("api/MealCategories/").Result;
  101.  
  102. if (response.IsSuccessStatusCode)
  103. {
  104. string foodstring = response.Content.ReadAsStringAsync().Result;
  105. List<CategoriesView> categories = JsonConvert.DeserializeObject<List<CategoriesView>>(foodstring);
  106. Dictionary<int, string> categoryDictionary = categories.ToDictionary(x => x.MealCategoryID, x => x.MealCategoryName);
  107. SortedDictionary<int, string> sortedMealDictionary = new SortedDictionary<int, string>(categoryDictionary);
  108. AddCategoryName.DataSource = sortedMealDictionary;
  109. AddCategoryName.DataTextField = "Value";
  110. AddCategoryName.DataValueField = "Key";
  111. AddCategoryName.DataBind();
  112.  
  113. ListItem item = new ListItem("--Select--", "0");
  114. AddCategoryName.Items.Insert(0, item);
  115. }
  116. else
  117. {
  118. ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
  119. }
  120.  
  121. ToolTips();
  122. }
  123.  
  124. protected void ToolTips()
  125. {
  126. AddMealName.ToolTip = "<span style ='color:red'>*</span> Food Name must be between 1 and 30 characters in length <br/>" +
  127. "<span style ='color:red'>*</span> Food Name must not contain any special characters";
  128.  
  129. AddCategoryName.ToolTip = "<span style ='color:red'>*</span> Select a meal for your training";
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement