Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Firstname, Lastname, Email, Streetadress, City, Country, Ice ,Qtypersons, RoomTypeId, Startdate,Enddate,Eta,Timearrival,Timedeparture,Specialneeds,Extrabed, StaffId")] StaffBookingVM booking)
        {
            if (ModelState.IsValid)
            {
                var customerId = _customerService.CheckIfCustomerExists(booking.Email);
                if (customerId == 0)
                {
                    customerId = _customerService.CreateCustomer(1, booking.Firstname, booking.Lastname, booking.Email, booking.Streetadress, booking.City, booking.Country, booking.Ice, DateTime.Now);
                }
                try
                {
                    var freeRoomId = _roomService.GetIdOfFreeRoomOfType(booking.RoomTypeId, booking.Startdate, booking.Enddate);
                    if (freeRoomId != 0)
                    {
                        var newBooking = new Booking()
                        {
                            Customersid  = customerId,
                            Qtypersons   = booking.Qtypersons,
                            Startdate    = booking.Startdate,
                            Enddate      = booking.Enddate,
                            Eta          = booking.Eta,
                            Specialneeds = booking.Specialneeds,
                            Extrabed     = booking.Extrabed,
                            Staffid      = booking.StaffId
                        };
                        _context.Bookings.Add(newBooking);
                        await _context.SaveChangesAsync();

                        var bookingsroom = new Bookingsroom()
                        {
                            Bookingsid = newBooking.Id,
                            Roomsid    = freeRoomId
                        };
                        _context.Bookingsrooms.Add(bookingsroom);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }
                }
                catch (InvalidOperationException e)
                {
                    ViewBag.ErrorMsg = "No room of this type available for the specific dates you've entered";
                    return(View(booking));
                }
            }
            ViewData["Staffid"]    = new SelectList(_context.staff, "Id", "Firstname");
            ViewData["RoomTypeId"] = new SelectList(_context.Roomtypes, "Id", "Name");
            var bookedRooms = _bookingService.GetBookedDates();

            ViewBag.SingleFullyBookedDates = bookedRooms.Where(x => x.BookedSingleRooms >= _roomService.GetAmountOfRooms(1)).Select(d => d.Date).ToList();
            ViewBag.DoubleFullyBookedDates = bookedRooms.Where(x => x.BookedDoubleRooms >= _roomService.GetAmountOfRooms(2)).Select(d => d.Date).ToList();
            ViewBag.TripleFullyBookedDates = bookedRooms.Where(x => x.BookedTripleRooms >= _roomService.GetAmountOfRooms(3)).Select(d => d.Date).ToList();
            ViewBag.QuadFullyBookedDates   = bookedRooms.Where(x => x.BookedQuadRooms >= _roomService.GetAmountOfRooms(4)).Select(d => d.Date).ToList();

            return(View(booking));
        }
Beispiel #2
0
        public async Task <IActionResult> Book([Bind("Customersid, RoomTypeId, Eta, Qtypersons,Startdate,Enddate,Specialneeds,Extrabed")] BookingVM booking)
        {
            if (ModelState.IsValid)
            {
                var freeRoomId = _roomService.GetIdOfFreeRoomOfType(booking.RoomTypeId, booking.Startdate, booking.Enddate);
                if (freeRoomId != 0)
                {
                    var newBooking = new Booking()
                    {
                        Customersid  = booking.Customersid,
                        Qtypersons   = booking.Qtypersons,
                        Startdate    = booking.Startdate,
                        Enddate      = booking.Enddate,
                        Eta          = booking.Eta,
                        Specialneeds = booking.Specialneeds,
                        Extrabed     = booking.Extrabed
                    };
                    _context.Bookings.Add(newBooking);
                    await _context.SaveChangesAsync();

                    var bookingsroom = new Bookingsroom()
                    {
                        Bookingsid = newBooking.Id,
                        Roomsid    = freeRoomId
                    };
                    _context.Bookingsrooms.Add(bookingsroom);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(BookingSuccess)));
                }
            }
            var userEmail = User.FindFirstValue(ClaimTypes.Email);

            if (userEmail != null)
            {
                var customer = _context.Customers.Where(c => c.Email == userEmail).FirstOrDefault();
                ViewBag.CustomerFirstName = customer.Firstname;
                ViewBag.CustomerLastName  = customer.Lastname;
                ViewBag.CustomerId        = customer.Id;
            }

            ViewData["RoomTypeId"] = new SelectList(_context.Roomtypes, "Id", "Name");
            var bookedRooms = _bookingService.GetBookedDates();

            ViewBag.SingleFullyBookedDates = bookedRooms.Where(x => x.BookedSingleRooms >= _roomService.GetAmountOfRooms(1)).Select(d => d.Date).ToList();
            ViewBag.DoubleFullyBookedDates = bookedRooms.Where(x => x.BookedDoubleRooms >= _roomService.GetAmountOfRooms(2)).Select(d => d.Date).ToList();
            ViewBag.TripleFullyBookedDates = bookedRooms.Where(x => x.BookedTripleRooms >= _roomService.GetAmountOfRooms(3)).Select(d => d.Date).ToList();
            ViewBag.QuadFullyBookedDates   = bookedRooms.Where(x => x.BookedQuadRooms >= _roomService.GetAmountOfRooms(4)).Select(d => d.Date).ToList();
            return(View(booking));
        }