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