public IActionResult AddNewTable(ReservationDeteailsViewModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             //getting list of tables that are booked in the same period that the current reservation
             List <Table_Reservation> Unavailabe_Tables = _services.GetListTable(model.Date, model.StartTime);
             foreach (var table in Unavailabe_Tables)
             {
                 //check if the choosen table is not in the list of Unavailabe_Tables
                 if (table.TableID == model.TableID)
                 {
                     model.Errormessage = "Sorry this table is already booked during that period";
                     //(if it is) return detailspage with an error message
                     return(RedirectToAction("Detail", new { id = model.ReservationID, errormessage = model.Errormessage }));
                 }
             }
             model.Errormessage = "The table was added";
             //(if NOT) set new table to the resrvation
             _services.AddTable(model.ReservationID, model.TableID);
         }
         catch
         {
             model.Errormessage = "You havent choosen any table";
             //if an error happens return, no tables were selected
             return(RedirectToAction("Detail", new { id = model.ReservationID, errormessage = model.Errormessage }));
         }
     }
     //if modelstate is not valid return detail page
     return(RedirectToAction("Detail", new { id = model.ReservationID }));
 }
 public IActionResult RemoveTable(ReservationDeteailsViewModel model)
 {
     if (ModelState.IsValid)
     {
         //check if a table were choose and if the reservation has any table
         if (model.TableID != 0 && model.NumberOfTables > 0)
         {
             try
             {
                 _services.RemoveTable(model.ReservationID, model.TableID);
                 _services.UpdateReservation(model);
                 model.Errormessage = "The table was Removed";
             }
             catch (Exception)
             {
                 model.Errormessage = "You cannot delete a table that has not been assigned";
                 return(RedirectToAction("Detail", new { id = model.ReservationID, errormessage = model.Errormessage }));
             }
         }
         //(if NOT) return details page with an error message
         model.Errormessage = "There have been no tables selected.";
         return(RedirectToAction("Detail", new { id = model.ReservationID, errormessage = model.Errormessage }));
     }
     return(RedirectToAction("Detail", new { id = model.ReservationID }));
 }
 public IActionResult Detail(ReservationDeteailsViewModel m)
 {
     if (ModelState.IsValid)
     {
         // getting the New sitting ID based on its name
         Sitting s = _services.GetSittingByName(m.SittingName);
         // setting it to the model
         m.SittingID = s.SittingID;
         // using the model data to update the DB
         _services.UpdateReservation(m);
         // return to the Staf Page (Reservations list)
         return(RedirectToAction("Index"));
     }
     // stay in the details page if modelState is not valid
     return(View(m.ReservationID));
 }
        public IActionResult Detail(int id, string errormessage)
        {
            //get the reservation data from the database
            Reservation r = _services.GetReservationById(id);
            //get the Customer Data from the database
            Customer c = _services.GetCustomerById(r.CustomerID);
            //get the Sitting Data from the database
            Sitting s = _services.GetSittingById(r.SittingID);
            //get the Tables List of this reservation from the database
            List <Table_Reservation> tds = _services.GetTRList_byReservationID(r.ReservationID);
            //get the Area Name from the database
            string MyArea = _services.GetAreaByID(id);

            ReservationDeteailsViewModel model = new ReservationDeteailsViewModel()
            {
                //Sitting
                SittingID   = s.SittingID,
                SittingName = s.SittingName,

                //Reservation
                ReservationID          = r.ReservationID,
                NumberOfGuests         = r.NumberOfGuests,
                AdditionalRequirements = r.AdditionalRequirements,
                Date      = r.Date,
                StartTime = r.StartTime,
                EndTime   = r.EndTime,
                Status    = r.Status,
                Source    = r.Source,

                //Customer
                CustomerId  = c.CustomerID,
                FirstName   = c.FirstName,
                LastName    = c.LastName,
                Email       = c.Email,
                PhoneNumber = c.PhoneNumber,

                //tables
                NumberOfTables = tds.Count,
                MyTR           = tds,
                Area           = MyArea
            };

            //setting new error message
            model.Errormessage = errormessage;
            return(View(model));
        }
        //----------------------------------------------------------------------------- UPDATE Section:

        public void UpdateReservation(ReservationDeteailsViewModel command)              // Update reservations Details
        {
            Reservation r = GetReservationById(command.ReservationID);

            //reservation
            r.NumberOfGuests         = command.NumberOfGuests;
            r.AdditionalRequirements = command.AdditionalRequirements;
            r.Status     = command.Status;
            r.Source     = command.Source;
            r.Date       = command.Date;
            r.StartTime  = command.StartTime;
            r.EndTime    = command.EndTime;
            r.CustomerID = command.CustomerId;
            r.SittingID  = command.SittingID;

            //update Database
            _context.SaveChanges();
        }