PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Cinemas/Cinemas/Controllers/BookingsController.cs

https://bitbucket.org/A00425229/project-2
C# | 236 lines | 143 code | 19 blank | 74 comment | 12 complexity | 7dac4fe2a16d91d11353013507fb7ccb MD5 | raw file
  1. /*using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Cinemas.Models;*/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Data.Entity;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Net.Http;
  17. using System.Net.Http.Headers;
  18. using System.Threading.Tasks;
  19. using System.Web;
  20. using System.Web.Mvc;
  21. using Cinemas.Models;
  22. using Newtonsoft.Json;
  23. using Newtonsoft.Json.Linq;
  24. namespace Cinemas.Controllers
  25. {
  26. public class BookingsController : Controller
  27. {
  28. private LuxeCinemasEntities db = new LuxeCinemasEntities();
  29. // GET: Bookings
  30. public async Task<ActionResult> Index()
  31. {
  32. List<Booking> BookingInfo = new List<Booking>();
  33. using (var client = new HttpClient())
  34. {
  35. //Passing service base url
  36. client.BaseAddress = new Uri("http://localhost:8080/MovieBookingRest/rest/");
  37. client.DefaultRequestHeaders.Clear();
  38. //Define request data format
  39. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  40. //Sending request to find web api REST service resource GetAllEmployees using HttpClient
  41. HttpResponseMessage Res = await client.GetAsync("Bookings");
  42. //Checking the response is successful or not which is sent using HttpClient
  43. if (Res.IsSuccessStatusCode)
  44. {
  45. //Storing the response details recieved from web api
  46. var BookingResponse = Res.Content.ReadAsStringAsync().Result;
  47. //Deserializing the response recieved from web api and storing into the Employee list
  48. BookingInfo = JsonConvert.DeserializeObject<List<Booking>>(BookingResponse);
  49. }
  50. //returning the employee list to view
  51. return View(BookingInfo);
  52. }
  53. }
  54. /*public ActionResult Index()
  55. {
  56. var bookings = db.Bookings.Include(b => b.Customer);
  57. return View(bookings.ToList());
  58. }*/
  59. // GET: Bookings/Details/5
  60. //public ActionResult Details(int? id)
  61. //{
  62. // if (id == null)
  63. // {
  64. // return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  65. // }
  66. // Booking booking = db.Bookings.Find(id);
  67. // if (booking == null)
  68. // {
  69. // return HttpNotFound();
  70. // }
  71. // return View(booking);
  72. //}
  73. // GET: Bookings/Details/5
  74. public ActionResult Details()
  75. {
  76. try
  77. {
  78. return View();
  79. }
  80. catch (Exception ex)
  81. {
  82. return View("~/Views/Shared/Error.cshtml");
  83. }
  84. }
  85. // GET: Bookings/Create
  86. public ActionResult Create()
  87. {
  88. ViewBag.CustomerId = new SelectList(db.Customers, "ID", "firstName");
  89. return View();
  90. }
  91. // POST: Bookings/Create
  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 async Task<ActionResult> Create(Booking booking)
  97. {
  98. try
  99. {
  100. using (var client = new HttpClient())
  101. {
  102. Booking book = new Booking();
  103. book.CustomerId = Convert.ToInt32(Session["CustId"]);
  104. book.Movie = booking.Movie;
  105. book.Tickets = booking.Tickets;
  106. book.Date = Convert.ToString(booking.Date);
  107. book.Time = booking.Time;
  108. string addcustomerMethod = "Bookings";
  109. String Baseurl = "http://localhost:8080/MovieBookingRest/rest/";
  110. HttpResponseMessage responseMessage = await client.PostAsJsonAsync(Baseurl + addcustomerMethod, book);
  111. if (responseMessage.IsSuccessStatusCode)
  112. {
  113. return RedirectToAction("Details", "Bookings");
  114. }
  115. else
  116. {
  117. return View("~/Views/Shared/Error.cshtml");
  118. }
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. return View("~/Views/Shared/Error.cshtml");
  124. }
  125. }
  126. /*public ActionResult Create([Bind(Include = "ID,CustomerId,Movie,Tickets,Date,Time")] Booking booking)
  127. {
  128. try
  129. {
  130. if (ModelState.IsValid)
  131. {
  132. Booking book = new Booking();
  133. book.CustomerId = Convert.ToInt32(Session["CustId"]);
  134. book.Movie = booking.Movie;
  135. book.Tickets = booking.Tickets;
  136. book.Date = Convert.ToString(booking.Date);
  137. book.Time = booking.Time;
  138. db.Bookings.Add(book);
  139. db.SaveChanges();
  140. return RedirectToAction("Details", "Bookings");
  141. }
  142. ViewBag.CustomerId = new SelectList(db.Customers, "ID", "firstName", booking.CustomerId);
  143. return View(booking);
  144. }
  145. catch(Exception ex)
  146. {
  147. return View("~/Views/Shared/Error.cshtml");
  148. }
  149. }
  150. */
  151. // GET: Bookings/Edit/5
  152. public ActionResult Edit(int? id)
  153. {
  154. if (id == null)
  155. {
  156. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  157. }
  158. Booking booking = db.Bookings.Find(id);
  159. if (booking == null)
  160. {
  161. return HttpNotFound();
  162. }
  163. ViewBag.CustomerId = new SelectList(db.Customers, "ID", "firstName", booking.CustomerId);
  164. return View(booking);
  165. }
  166. // POST: Bookings/Edit/5
  167. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  168. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  169. [HttpPost]
  170. [ValidateAntiForgeryToken]
  171. public ActionResult Edit([Bind(Include = "ID,CustomerId,Movie,Tickets,Date,Time")] Booking booking)
  172. {
  173. if (ModelState.IsValid)
  174. {
  175. db.Entry(booking).State = EntityState.Modified;
  176. db.SaveChanges();
  177. return RedirectToAction("Index");
  178. }
  179. ViewBag.CustomerId = new SelectList(db.Customers, "ID", "firstName", booking.CustomerId);
  180. return View(booking);
  181. }
  182. // GET: Bookings/Delete/5
  183. public ActionResult Delete(int? id)
  184. {
  185. if (id == null)
  186. {
  187. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  188. }
  189. Booking booking = db.Bookings.Find(id);
  190. if (booking == null)
  191. {
  192. return HttpNotFound();
  193. }
  194. return View(booking);
  195. }
  196. // POST: Bookings/Delete/5
  197. [HttpPost, ActionName("Delete")]
  198. [ValidateAntiForgeryToken]
  199. public ActionResult DeleteConfirmed(int id)
  200. {
  201. Booking booking = db.Bookings.Find(id);
  202. db.Bookings.Remove(booking);
  203. db.SaveChanges();
  204. return RedirectToAction("Index");
  205. }
  206. protected override void Dispose(bool disposing)
  207. {
  208. if (disposing)
  209. {
  210. db.Dispose();
  211. }
  212. base.Dispose(disposing);
  213. }
  214. }
  215. }