|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using System.Collections.Generic; |
| 3 | +using DDxApi.Models; |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System.Net.Http; |
| 7 | +using System; |
| 8 | +using System.Net.Http.Json; |
| 9 | +using System.Data; |
| 10 | + |
| 11 | +namespace DDxHub.Controllers |
| 12 | +{ |
| 13 | + public class AnalyzeController : Controller |
| 14 | + { |
| 15 | + // GET: AnalyzeController1 |
| 16 | + public ActionResult Index() |
| 17 | + { |
| 18 | + Startup.itemAdded = false; |
| 19 | + Startup.itemGotten = false; |
| 20 | + |
| 21 | + // Create a unique number as a client's fingerprinting identifier. |
| 22 | + // If you decide to use a constant like itemID = "1", the data |
| 23 | + // entered by your different clients can be intercepted and mixed. |
| 24 | + string itemID = Startup.uniqueMark; |
| 25 | + AddItem(itemID); |
| 26 | + |
| 27 | + // To make async operation as a regular sync - wait for 45 seconds when new data item added to API |
| 28 | + for (var i = 1; i <= 90; i++) |
| 29 | + if (Startup.itemAdded) |
| 30 | + { |
| 31 | + GetItem(itemID); |
| 32 | + break; |
| 33 | + } |
| 34 | + else |
| 35 | + System.Threading.Thread.Sleep(500); |
| 36 | + // Break after 45 seconds attempting to add data item to DiagnosisApi Web API |
| 37 | + if (!Startup.itemAdded) |
| 38 | + { |
| 39 | + ViewBag.Message = "Something went wrong. Please Try Again Later."; |
| 40 | + return View(); |
| 41 | + } |
| 42 | + |
| 43 | + // To make async operation as a regular sync - wait for 45 seconds when new Disorders calculated |
| 44 | + for (var i = 1; i <= 90; i++) |
| 45 | + if (Startup.itemGotten) |
| 46 | + { |
| 47 | + List<Disorder> Diss = new List<Disorder>(); |
| 48 | + foreach (DataRow row in Startup.tblAllDisorders.Rows) |
| 49 | + { |
| 50 | + Disorder dis = new Disorder(); |
| 51 | + dis.Id = Convert.ToInt32(row["id"]); |
| 52 | + dis.Name = row["name"].ToString(); |
| 53 | + Diss.Add(dis); |
| 54 | + } |
| 55 | + if (Diss.Count <= 0) |
| 56 | + ViewBag.Message = "Not enough data entered to make analysis..."; |
| 57 | + return View(Diss); |
| 58 | + } |
| 59 | + else |
| 60 | + System.Threading.Thread.Sleep(500); |
| 61 | + // Break after 45 seconds attempting to get Disorders from DiagnosisApi Web API |
| 62 | + if (!Startup.itemGotten) |
| 63 | + ViewBag.Message = "Something went wrong. Please Try Again Later."; |
| 64 | + |
| 65 | + return View(); |
| 66 | + } |
| 67 | + |
| 68 | + public async void AddItem(string itemID) |
| 69 | + { |
| 70 | + Startup.itemAdded = false; |
| 71 | + var strTests = JsonConvert.SerializeObject(Startup.tests); |
| 72 | + |
| 73 | + var strSymptoms = JsonConvert.SerializeObject(Startup.symptoms); |
| 74 | + |
| 75 | + var item = new DDxItem() |
| 76 | + { |
| 77 | + Id = itemID, |
| 78 | + Tests = strTests, |
| 79 | + Symptoms = strSymptoms |
| 80 | + }; |
| 81 | + |
| 82 | + using (var client = new HttpClient()) |
| 83 | + { |
| 84 | + client.BaseAddress = new Uri(Startup.baseAddress + Startup.AuthenticationID); |
| 85 | + //HTTP POST |
| 86 | + var response = await client.PostAsJsonAsync<DDxItem>("", item); |
| 87 | + if (response.IsSuccessStatusCode) |
| 88 | + Startup.itemAdded = true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public async void GetItem(string itemID) |
| 93 | + { |
| 94 | + Startup.itemGotten = false; |
| 95 | + using (HttpClient client = new HttpClient()) |
| 96 | + { |
| 97 | + // Build Url Base Address with AuthenticationID |
| 98 | + client.BaseAddress = new Uri(Startup.baseAddress + itemID + Startup.AuthenticationID); |
| 99 | + client.DefaultRequestHeaders.Accept.Clear(); |
| 100 | + client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| 101 | + |
| 102 | + //HTTP GET |
| 103 | + HttpResponseMessage response = await client.GetAsync(client.BaseAddress); |
| 104 | + if (response.IsSuccessStatusCode) |
| 105 | + { |
| 106 | + var data = await response.Content.ReadAsStringAsync(); |
| 107 | + Startup.tblAllDisorders = JsonConvert.DeserializeObject<DataTable>(data); |
| 108 | + Startup.itemGotten = true; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // GET: AnalyzeController1/Details/5 |
| 114 | + public ActionResult Details(int id) |
| 115 | + { |
| 116 | + return View(); |
| 117 | + } |
| 118 | + |
| 119 | + // GET: AnalyzeController1/Create |
| 120 | + public ActionResult Create() |
| 121 | + { |
| 122 | + return View(); |
| 123 | + } |
| 124 | + |
| 125 | + // POST: AnalyzeController1/Create |
| 126 | + [HttpPost] |
| 127 | + [ValidateAntiForgeryToken] |
| 128 | + public ActionResult Create(IFormCollection collection) |
| 129 | + { |
| 130 | + try |
| 131 | + { |
| 132 | + return RedirectToAction(nameof(Index)); |
| 133 | + } |
| 134 | + catch |
| 135 | + { |
| 136 | + return View(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // GET: AnalyzeController1/Edit/5 |
| 141 | + public ActionResult Edit(int id) |
| 142 | + { |
| 143 | + return View(); |
| 144 | + } |
| 145 | + |
| 146 | + // POST: AnalyzeController1/Edit/5 |
| 147 | + [HttpPost] |
| 148 | + [ValidateAntiForgeryToken] |
| 149 | + public ActionResult Edit(int id, IFormCollection collection) |
| 150 | + { |
| 151 | + try |
| 152 | + { |
| 153 | + return RedirectToAction(nameof(Index)); |
| 154 | + } |
| 155 | + catch |
| 156 | + { |
| 157 | + return View(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // GET: AnalyzeController1/Delete/5 |
| 162 | + public ActionResult Delete(int id) |
| 163 | + { |
| 164 | + return View(); |
| 165 | + } |
| 166 | + |
| 167 | + // POST: AnalyzeController1/Delete/5 |
| 168 | + [HttpPost] |
| 169 | + [ValidateAntiForgeryToken] |
| 170 | + public ActionResult Delete(int id, IFormCollection collection) |
| 171 | + { |
| 172 | + try |
| 173 | + { |
| 174 | + return RedirectToAction(nameof(Index)); |
| 175 | + } |
| 176 | + catch |
| 177 | + { |
| 178 | + return View(); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments