PageRenderTime 33ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

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