PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/BookingApp/Controllers/ContactsController.cs

https://bitbucket.org/A00424877/groupmcda5510
C# | 441 lines | 328 code | 47 blank | 66 comment | 32 complexity | d86f65cefdc463a5f748ab052e97dc15 MD5 | raw file
  1. <<<<<<< HEAD
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. using BookingApp;
  12. using BookingApp.Models;
  13. namespace BookingApp.Controllers
  14. {
  15. public class ContactsController : Controller
  16. {
  17. private BookingAppEntityModel db = new BookingAppEntityModel();
  18. private string BASE_URL = "http://localhost:8080/HotelBookingRest/rest/Customers/"; // http://localhost:8080/HotelBookingRest/rest/Customers
  19. private HttpClient client = new HttpClient();
  20. // GET: Contacts
  21. public ActionResult Index()
  22. {
  23. client.BaseAddress = new Uri(BASE_URL);
  24. HttpResponseMessage response = client.GetAsync(BASE_URL).Result;
  25. List<Contact> contacts = new List<Contact>();
  26. if (response.IsSuccessStatusCode)
  27. {
  28. contacts = response.Content.ReadAsAsync<List<Contact>>().Result;
  29. return View(contacts.ToList());
  30. }
  31. return View(contacts.ToList());
  32. }
  33. // GET: Contacts/Details/5
  34. public ActionResult Details(int? id)
  35. {
  36. if (id == null)
  37. {
  38. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  39. }
  40. Contact contact = db.Contacts.Find(id);
  41. if (contact == null)
  42. {
  43. return HttpNotFound();
  44. }
  45. return View(contact);
  46. }
  47. /// <summary>
  48. /// ////////////////////////////////////////////////////////////////////////////////////
  49. /// </summary>
  50. /// <returns></returns>
  51. // GET: Contacts/Create
  52. public ActionResult Create()
  53. {
  54. Contact contact = new Contact();
  55. var countries = GetAllCountries();
  56. contact.CountriesList = GetSelectListItems(countries);
  57. return View(contact);
  58. }
  59. // POST: Contacts/Create
  60. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  61. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  62. [HttpPost]
  63. [ValidateAntiForgeryToken]
  64. public ActionResult Create([Bind(Include = "Id,LastName,FirstName,StreetNumber,City,ProvinceState,Country,PostalCode,PhoneNumber,Email")] Contact contact)
  65. {
  66. if (ModelState.IsValid)
  67. {
  68. db.Contacts.Add(contact);
  69. db.SaveChanges();
  70. //return Redirect("/reservations/Create");
  71. return RedirectToAction("Create", "Reservations", new { contactId = contact.Id });
  72. }
  73. var countries = GetAllCountries();
  74. contact.CountriesList = GetSelectListItems(countries);
  75. return View(contact);
  76. }
  77. // GET: Contacts/Edit/5
  78. public ActionResult Edit(int? id)
  79. {
  80. if (id == null)
  81. {
  82. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  83. }
  84. Contact contact = db.Contacts.Find(id);
  85. if (contact == null)
  86. {
  87. return HttpNotFound();
  88. }
  89. return View(contact);
  90. }
  91. // POST: Contacts/Edit/5
  92. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  93. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  94. [HttpPost]
  95. [ValidateAntiForgeryToken]
  96. public ActionResult Edit([Bind(Include = "Id,LastName,FirstName,StreetNumber,City,ProvinceState,Country,PostalCode,PhoneNumber,Email")] Contact contact)
  97. {
  98. if (ModelState.IsValid)
  99. {
  100. db.Entry(contact).State = EntityState.Modified;
  101. db.SaveChanges();
  102. return RedirectToAction("Index");
  103. }
  104. return View(contact);
  105. }
  106. // GET: Contacts/Delete/5
  107. public ActionResult Delete(int? id)
  108. {
  109. if (id == null)
  110. {
  111. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  112. }
  113. Contact contact = db.Contacts.Find(id);
  114. if (contact == null)
  115. {
  116. return HttpNotFound();
  117. }
  118. return View(contact);
  119. }
  120. // POST: Contacts/Delete/5
  121. [HttpPost, ActionName("Delete")]
  122. [ValidateAntiForgeryToken]
  123. public ActionResult DeleteConfirmed(int id)
  124. {
  125. Contact contact = db.Contacts.Find(id);
  126. db.Contacts.Remove(contact);
  127. db.SaveChanges();
  128. return RedirectToAction("Index");
  129. }
  130. protected override void Dispose(bool disposing)
  131. {
  132. if (disposing)
  133. {
  134. db.Dispose();
  135. }
  136. base.Dispose(disposing);
  137. }
  138. private IEnumerable<string> GetAllCountries()
  139. {
  140. return new List<string>
  141. {
  142. "USA",
  143. "CANADA"
  144. };
  145. }
  146. private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
  147. {
  148. var selectList = new List<SelectListItem>();
  149. foreach (var element in elements)
  150. {
  151. selectList.Add(new SelectListItem
  152. {
  153. Value = element,
  154. Text = element
  155. });
  156. }
  157. return selectList;
  158. }
  159. }
  160. }
  161. =======
  162. using System;
  163. using System.Collections.Generic;
  164. using System.Data;
  165. using System.Data.Entity;
  166. using System.Linq;
  167. using System.Net;
  168. using System.Web;
  169. using System.Web.Mvc;
  170. using BookingApp;
  171. using BookingApp.Models;
  172. using System.Net.Http.Headers;
  173. using System.Threading.Tasks;
  174. using System;
  175. using System.Collections.Generic;
  176. using System.Data;
  177. using System.Data.Entity;
  178. using System.Linq;
  179. using System.Net;
  180. using System.Net.Http;
  181. using System.Net.Http.Headers;
  182. using System.Threading.Tasks;
  183. using System.Web;
  184. using System.Web.Mvc;
  185. using Newtonsoft.Json;
  186. using Newtonsoft.Json.Linq;
  187. namespace BookingApp.Controllers
  188. {
  189. public class ContactsController : Controller
  190. {
  191. private BookingAppEntityModel db = new BookingAppEntityModel();
  192. // GET: Contacts
  193. public async Task<ActionResult> Index()
  194. {
  195. List<Contact> custInfo = new List<Contact>();
  196. using (var client = new HttpClient())
  197. {
  198. //Passing service base url
  199. client.BaseAddress = new Uri("http://localhost:8080/HotelBookingRest/rest/");
  200. client.DefaultRequestHeaders.Clear();
  201. //Define request data format
  202. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  203. //Sending request to find web api REST service resource GetAllEmployees using HttpClient
  204. HttpResponseMessage Res = await client.GetAsync("Customers");
  205. //Checking the response is successful or not which is sent using HttpClient
  206. if (Res.IsSuccessStatusCode)
  207. {
  208. //Storing the response details recieved from web api
  209. var CustResponse = Res.Content.ReadAsStringAsync().Result;
  210. //Deserializing the response recieved from web api and storing into the Employee list
  211. custInfo = JsonConvert.DeserializeObject<List<Contact>>(CustResponse);
  212. System.Diagnostics.Debug.WriteLine(CustResponse);
  213. }
  214. //returning the employee list to view
  215. return View(custInfo);
  216. }
  217. }
  218. /* public ActionResult Index()
  219. {
  220. return View(db.Contacts.ToList());
  221. }
  222. */
  223. // GET: Contacts/Details/5
  224. public ActionResult Details(int? id)
  225. {
  226. if (id == null)
  227. {
  228. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  229. }
  230. Contact contact = db.Contacts.Find(id);
  231. if (contact == null)
  232. {
  233. return HttpNotFound();
  234. }
  235. return View(contact);
  236. }
  237. // GET: Contacts/Create
  238. public ActionResult Create()
  239. {
  240. Contact contact = new Contact();
  241. var countries = GetAllCountries();
  242. contact.CountriesList = GetSelectListItems(countries);
  243. return View(contact);
  244. }
  245. // POST: Contacts/Create
  246. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  247. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  248. [HttpPost]
  249. [ValidateAntiForgeryToken]
  250. public async Task<ActionResult> Create(Contact contact)
  251. {
  252. System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++++++++++++");
  253. System.Diagnostics.Debug.WriteLine(contact.ToString());
  254. try
  255. {
  256. using (var client = new HttpClient())
  257. {
  258. string addcustomerMethod = "Customers/create";
  259. String Baseurl = "http://localhost:8080/HotelBookingRest/rest/";
  260. HttpResponseMessage responseMessage = await client.PostAsJsonAsync(Baseurl + addcustomerMethod, contact);
  261. string strJson = "";
  262. var customerID = "";
  263. if (responseMessage.IsSuccessStatusCode)
  264. {
  265. using (HttpContent content = responseMessage.Content)
  266. {
  267. // ... Read the string.
  268. strJson = content.ReadAsStringAsync().Result;
  269. dynamic jObj = (JObject)JsonConvert.DeserializeObject(strJson);
  270. // System.Diagnostics.Debug.WriteLine(jObj.ToString());
  271. customerID = jObj["id"];
  272. }
  273. //Login loginObj = new Login();
  274. //loginObj.CustomerId = customer.ID;
  275. // Session["custId"] = Convert.ToString(customerID);
  276. /*loginObj.loginId = Request["loginName"].ToString();
  277. loginObj.password = Request["password"].ToString();
  278. db.Logins.Add(loginObj);
  279. db.SaveChanges();*/
  280. System.Diagnostics.Debug.WriteLine("++++++++++++++"+customerID);
  281. return RedirectToAction("Create", "Reservations", new { contactId = customerID });
  282. }
  283. else
  284. {
  285. return View("~/Views/Shared/Error.cshtml");
  286. }
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. return View("~/Views/Shared/Error.cshtml");
  292. }
  293. }
  294. /* public ActionResult Create([Bind(Include = "Id,LastName,FirstName,StreetNumber,City,ProvinceState,Country,PostalCode,PhoneNumber,Email")] Contact contact)
  295. {
  296. if (ModelState.IsValid)
  297. {
  298. db.Contacts.Add(contact);
  299. db.SaveChanges();
  300. //return Redirect("/reservations/Create");
  301. return RedirectToAction("Create", "Reservations", new { contactId = contact.Id });
  302. }
  303. var countries = GetAllCountries();
  304. contact.CountriesList = GetSelectListItems(countries);
  305. return View(contact);
  306. }
  307. */
  308. // GET: Contacts/Edit/5
  309. public ActionResult Edit(int? id)
  310. {
  311. if (id == null)
  312. {
  313. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  314. }
  315. Contact contact = db.Contacts.Find(id);
  316. if (contact == null)
  317. {
  318. return HttpNotFound();
  319. }
  320. return View(contact);
  321. }
  322. // POST: Contacts/Edit/5
  323. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  324. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  325. [HttpPost]
  326. [ValidateAntiForgeryToken]
  327. public ActionResult Edit([Bind(Include = "Id,LastName,FirstName,StreetNumber,City,ProvinceState,Country,PostalCode,PhoneNumber,Email")] Contact contact)
  328. {
  329. if (ModelState.IsValid)
  330. {
  331. db.Entry(contact).State = EntityState.Modified;
  332. db.SaveChanges();
  333. return RedirectToAction("Index");
  334. }
  335. return View(contact);
  336. }
  337. // GET: Contacts/Delete/5
  338. public ActionResult Delete(int? id)
  339. {
  340. if (id == null)
  341. {
  342. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  343. }
  344. Contact contact = db.Contacts.Find(id);
  345. if (contact == null)
  346. {
  347. return HttpNotFound();
  348. }
  349. return View(contact);
  350. }
  351. // POST: Contacts/Delete/5
  352. [HttpPost, ActionName("Delete")]
  353. [ValidateAntiForgeryToken]
  354. public ActionResult DeleteConfirmed(int id)
  355. {
  356. Contact contact = db.Contacts.Find(id);
  357. db.Contacts.Remove(contact);
  358. db.SaveChanges();
  359. return RedirectToAction("Index");
  360. }
  361. protected override void Dispose(bool disposing)
  362. {
  363. if (disposing)
  364. {
  365. db.Dispose();
  366. }
  367. base.Dispose(disposing);
  368. }
  369. private IEnumerable<string> GetAllCountries()
  370. {
  371. return new List<string>
  372. {
  373. "USA",
  374. "CANADA"
  375. };
  376. }
  377. private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
  378. {
  379. var selectList = new List<SelectListItem>();
  380. foreach (var element in elements)
  381. {
  382. selectList.Add(new SelectListItem
  383. {
  384. Value = element,
  385. Text = element
  386. });
  387. }
  388. return selectList;
  389. }
  390. }
  391. }
  392. >>>>>>> d318b9097397298542bc97ad89c156237689f560