public async Task <IActionResult> Create(BookingModel booking) { try { List <CategoryModel> categoryList = await ApiConnection.GetCategoryList(); List <RoomModel> roomList = await ApiConnection.GetRoomList(); List <BookingModel> bookingList = await ApiConnection.GetBookingList(); List <RoomModel> qualifiedrooms = new List <RoomModel>(); List <RoomModel> corcatroom = new List <RoomModel>(); int bookingstart = int.Parse(DateTime.Parse(booking.StartDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int bookingend = int.Parse(DateTime.Parse(booking.EndDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int dateToday = int.Parse(DateTime.Parse(DateTime.Today.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); if (bookingstart < bookingend && bookingstart >= dateToday) //kollar så bokningens startdatum inte är före dagens datum. { corcatroom.AddRange(from item in roomList where item.CategoryId == booking.CategoryId select item); List <BookingModel> corcatbooking = new List <BookingModel>(); BookingHandler.RoomAvailableCheckV2(bookingList, corcatroom, bookingstart, bookingend); if (corcatroom.Count > 0) //kollar ifall det finns tillgängliga rum { var room = corcatroom.First(); BookingModel finalBooking = BookingHandler.SetFinalBooking(booking, room); var postTask = ApiConnection.ApiClient.PostAsJsonAsync <BookingModel>("BookingModels", finalBooking); postTask.Wait(); var result = postTask.Result; return(RedirectToAction("Confirmation", "Booking")); } else { ViewData["norooms"] = "Det finns inga lediga rum av din preferenser"; await ViewbagCategory(); return(View(new BookingModel())); } } else { ViewData["wrongtime"] = "Vänligen fyll i en korrekt tid. Slutdatumet måste vara senare än startdatumet."; await ViewbagCategory(); return(View(new BookingModel())); } } catch (Exception ex) { _logger.LogWarning("Could not create booking: HttpPost", ex); return(View()); } }
public async Task <IActionResult> Edit(int id, int GuestId, [Bind("Id,CategoryId,StartDate,EndDate,GuestId")] BookingModel booking) { try { //först måste vi ta bort aktuell bokning från den lokala bokningslistan //anropa metod för att kolla om bokningen är möjlig List <RoomModel> roomList = await ApiConnection.GetRoomList(); List <BookingModel> bookingList = await ApiConnection.GetBookingList(); foreach (var item in bookingList) { if (booking.Id == item.Id) { bookingList.Remove(item); break; } } int bookingstart = int.Parse(DateTime.Parse(booking.StartDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int bookingend = int.Parse(DateTime.Parse(booking.EndDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int dateToday = int.Parse(DateTime.Parse(DateTime.Today.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); List <RoomModel> corcatroom = new List <RoomModel>(); if (bookingstart < bookingend && bookingstart >= dateToday) //kollar så bokningens start datum inte är efter slutdatum { corcatroom.AddRange(from item in roomList where item.CategoryId == booking.CategoryId select item); //--------------------------------------------------------------------------------------------------------------------------------------------- BookingHandler.RoomAvailableCheckV2(bookingList, corcatroom, bookingstart, bookingend); RoomModel Besterummet = new RoomModel(); if (corcatroom.Count() > 0) { Besterummet = corcatroom.First(); BookingModel finalBooking = new BookingModel(); finalBooking.Id = booking.Id; finalBooking.StartDate = booking.StartDate; finalBooking.EndDate = booking.EndDate; finalBooking.RoomId = Besterummet.Id; finalBooking.GuestId = GuestId; HttpResponseMessage response = ApiConnection.ApiClient.PutAsJsonAsync("BookingModels/" + finalBooking.Id, finalBooking).Result; return(RedirectToAction(nameof(Index))); } else { ViewData["norooms"] = "Det finns inga lediga rum av din preferenser"; await ViewbagCategory(); return(View()); } } else { ViewData["wrongtime"] = "Denna ändring är inte möjlig"; await ViewbagCategory(); return(RedirectToAction("Privacy", "Home")); } } catch (Exception ex) { _logger.LogWarning("Could not edit booking: HttpPost", ex); throw; } }
public async Task <IActionResult> Create(int id, [Bind("CategoryId, StartDate, EndDate, GuestId")] BookingModel booking) { try { var str = HttpContext.Session.GetString("GuestSession"); var obj = JsonConvert.DeserializeObject <GuestModel>(str); List <RoomModel> roomList = await ApiConnection.GetRoomList(); List <BookingModel> bookingList = await ApiConnection.GetBookingList(); List <CategoryModel> categoryList = await ApiConnection.GetCategoryList(); List <RoomModel> qualifiedrooms = new List <RoomModel>(); List <RoomModel> corcatroom = new List <RoomModel>(); int bookingstart = int.Parse(DateTime.Parse(booking.StartDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int bookingend = int.Parse(DateTime.Parse(booking.EndDate.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar datetime till int int dateToday = int.Parse(DateTime.Parse(DateTime.Today.ToString()).ToString().Remove(10, 9).Remove(4, 1).Remove(6, 1)); //parsar dagens datum till int if (bookingstart < bookingend && bookingstart >= dateToday) //kollar så bokningens start datum inte är efter slutdatum { corcatroom.AddRange(from item in roomList where item.CategoryId == booking.CategoryId select item); List <BookingModel> corcatbooking = new List <BookingModel>(); BookingHandler.RoomAvailableCheckV2(bookingList, corcatroom, bookingstart, bookingend); if (corcatroom.Count > 0) //kollar ifall det finns tillgängliga rum { var room = corcatroom.First(); BookingModel finalBooking = BookingHandler.SetFinalBooking(booking, room, obj); var postTask = ApiConnection.ApiClient.PostAsJsonAsync <BookingModel>("BookingModels", finalBooking); postTask.Wait(); var result = postTask.Result; return(RedirectToAction("Confirmation", "Booking")); } else { ViewBag.GuestBag = obj.Firstname + " " + obj.Lastname; ViewData["norooms"] = "Det finns inga lediga rum av din preferenser"; foreach (var item in categoryList) { item.Path = "http://193.10.202.81/Uploads/" + item.Image; } ViewData["Price"] = categoryList; //för att fixa viewdata await ViewbagCategory(); return(View(new BookingModel())); } } else { if (bookingstart < dateToday) { ViewData["wrongtime"] = "Om du inte har en tidsmaskin, kan du endast boka dagens datum och framåt."; foreach (var item in categoryList) { item.Path = "http://193.10.202.81/Uploads/" + item.Image; } ViewData["Price"] = categoryList; //för att fixa viewdata } else { ViewData["wrongtime"] = "Vänligen fyll i en korrekt tid. Slutdatumet måste vara senare än startdatumet."; foreach (var item in categoryList) { item.Path = "http://193.10.202.81/Uploads/" + item.Image; } ViewData["Price"] = categoryList; //för att fixa viewdata } ViewBag.GuestBag = obj.Firstname + " " + obj.Lastname; await ViewbagCategory(); return(View(new BookingModel())); } } catch (Exception ex) { _logger.LogWarning("Couldn't book a room."); System.Diagnostics.Debug.WriteLine(ex.Message); return(View()); } }