Example #1
0
        public bool UpdateBooking(BookingEdit model)
        {
            try
            {
                using (var ctx = new ApplicationDbContext())
                {
                    var bookingEntity = ctx.Bookings.Where(b => b.Id == model.Id)
                                        .FirstOrDefault();
                    if (bookingEntity == null)
                    {
                        return(false);
                    }

                    bookingEntity.Id        = model.Id;
                    bookingEntity.RenterId  = model.RenterId;
                    bookingEntity.SpaceId   = model.SpaceId;
                    bookingEntity.StartDate = model.StartDate;
                    bookingEntity.EndDate   = model.EndDate;

                    return(ctx.SaveChanges() == 1);
                }
            }
            catch (Exception e)
            {
                SentrySdk.CaptureException(e);
                return(false);
            }
        }
        public bool IsUpdatedBookingValid(BookingEdit updatedBooking, out List <string> validationMessages)
        {
            bool result = true;

            validationMessages = new List <string>();
            if (GetEmployeeById(updatedBooking.EmployeeId) == null)
            {
                result = false;
                validationMessages.Add("Employee ID " + updatedBooking.EmployeeId + "Doesn't exist!");
            }
            if (result)
            {
                List <Booking> allBookings = GetAllBookingsForEmployeeId(updatedBooking.EmployeeId);
                List <Booking> Conflicts   = allBookings.Where(b => b.booking_id != updatedBooking.BookingId && ((updatedBooking.StartDate > b.start_date && updatedBooking.StartDate < b.end_date) || (updatedBooking.StartDate <b.start_date && updatedBooking.EndDate> b.start_date))).ToList();
                result = Conflicts.Count() == 0;
                if (!result)
                {
                    validationMessages.Add("Booking requested starting on " + updatedBooking.StartDate.ToString("MM/dd/yyyy") +
                                           " and ending on " + updatedBooking.EndDate.ToString("MM/dd/yyyy") + " has conflict with the following booking(s)!");
                    foreach (var conflict in Conflicts)
                    {
                        validationMessages.Add("Booking ID: " + conflict.booking_id + " starting on " + conflict.start_date.ToString("MM/dd/yyyy") +
                                               " ending on " + conflict.end_date.ToString("MM/dd/yyyy"));
                    }
                }
            }


            return(result);
        }
        public bool UpdateBooking(BookingEdit updatedBooking, out List <string> validationMessages)
        {
            bool result = false;

            validationMessages = new List <string>();
            if (updatedBooking != null)
            {
                if (IsUpdatedBookingValid(updatedBooking, out validationMessages))
                {
                    try
                    {
                        Booking bookingToReplace = _db.Bookings.Where(b => b.booking_id == updatedBooking.BookingId).SingleOrDefault();
                        bookingToReplace.booking_type_code = updatedBooking.BookingType;
                        bookingToReplace.comment           = updatedBooking.Comment;
                        bookingToReplace.start_date        = updatedBooking.StartDate;
                        bookingToReplace.end_date          = updatedBooking.EndDate;

                        _db.SaveChanges();

                        result = true;
                    }
                    catch (Exception ex)
                    {
                        result = false;
                        LogWriter.WriteLog(ex);
                    }
                }
                else
                {
                    result = false;
                }
            }
            return(result);
        }
        public BookingEdit GetBookingDetails(BookingDetailsEditRequest bookingDetailsEditRequest)
        {
            using (StageBitzDB dataContext = new StageBitzDB())
            {
                InventoryBL inventoryBL = new InventoryBL(dataContext);
                BookingEdit bookingEdit = inventoryBL.GetBookingDetails(bookingDetailsEditRequest);

                return(bookingEdit);
            }
        }
        public bool UpdateBooking(BookingEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Bookings
                    .Single(e => e.BookingId == model.BookingId);

                entity.DestId  = model.DestId;
                entity.TransId = model.TransId;

                return(ctx.SaveChanges() == 1);
            }
        }
        public BookingEditForm RepopulateEditForm(BookingEdit form, List <string> validationMessages)
        {
            BookingEditForm repopulatedForm = CreateBookingEditForm(form.EmployeeId);

            repopulatedForm.StartDate          = form.StartDate;
            repopulatedForm.EndDate            = form.EndDate;
            repopulatedForm.Comment            = form.Comment;
            repopulatedForm.ValidationMessages = validationMessages;
            repopulatedForm.EmployeeId         = form.EmployeeId;
            repopulatedForm.EmployeeName       = form.EmployeeName;
            repopulatedForm.EmployeeNumber     = form.EmployeeNumber;
            repopulatedForm.Comment            = form.Comment;
            repopulatedForm.BookingType        = new SelectList(GetAllBookingTypes(), "booking_type_code", "booking_type_name");
            return(repopulatedForm);
        }
        public IHttpActionResult Put(BookingEdit booking)
        {
            booking.UserId = User.Identity.GetUserId();
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateBookingService();

            if (!service.UpdateBooking(booking))
            {
                return(InternalServerError());
            }
            return(Ok());
        }
Example #8
0
        public IHttpActionResult Put([FromBody] BookingEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var item = _service.GetBookingById(model.Id);

            if (item == null)
            {
                return(NotFound());
            }
            if (_service.UpdateBooking(model))
            {
                return(Ok());
            }
            return(InternalServerError());
        }
        public ActionResult Edit(BookingEdit booking, string bookingType)
        {
            booking.DateCreated = DateTime.Now;
            booking.CreatedBy   = User.Identity.Name.ToString();
            booking.StartDate   = booking.StartDate.AddHours(double.Parse(booking.StartTime));
            booking.EndDate     = booking.EndDate.AddHours(double.Parse(booking.EndTime));
            booking.BookingType = bookingType;



            if (ModelState.IsValid)
            {
                bool conflict = false;

                foreach (var id in booking.RoomIds)
                {
                    if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id) && b.BookingId != booking.BookingId))
                    {
                        conflict      = true;
                        ViewBag.Error = "This room and time have been reserved.";
                    }
                }
                if (!conflict)
                {
                    if (booking.EndDate < booking.StartDate)
                    {
                        ViewBag.Error = "The End Date Can't Be Before the Start Date.";
                    }
                    else
                    {
                        foreach (var id in booking.RoomIds)
                        {
                            db.Entry(booking.GetBooking(id)).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        return(RedirectToAction("Details", new { id = booking.BookingId }));
                    }
                }
            }
            var mgmt = new BookingMgmt(bookingRepository);

            return(View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName)));
        }
 public ActionResult Edit(BookingEdit form)
 {
     try
     {
         if (form != null)
         {
             List <string> validationMessages;
             if (_manager.UpdateBooking(form, out validationMessages))
             {
                 return(RedirectToAction("BookingsTable"));
             }
             else
             {
                 // Re-populate booking form with validation message
                 return(View(_manager.RepopulateEditForm(form, validationMessages)));
             }
         }
         return(RedirectToAction("BookingsTable"));
     }
     catch
     {
         return(RedirectToAction("BookingsTable"));
     }
 }
        public ActionResult Create(BookingEdit booking, string bookingType, string viewName, string viewRoomName, string option, string monthlyoption, int increment)
        {
            DateTime resetDate = booking.StartDate;

            booking.RepeadEnd = booking.RepeadEnd.AddDays(1).AddSeconds(-1);

            booking.DateCreated = DateTime.Now;
            booking.CreatedBy   = User.Identity.Name.ToString();
            booking.StartDate   = booking.StartDate.AddHours(double.Parse(booking.StartTime));
            booking.EndDate     = booking.EndDate.AddHours(double.Parse(booking.EndTime));
            booking.BookingType = bookingType;

            var mgmt = new BookingMgmt(bookingRepository);

            switch (option)
            {
            case "Daily":
                potentialBookings = mgmt.DailyRecurring(booking);
                break;

            case "Weekly":
                potentialBookings = mgmt.WeeklyRecurring(booking, increment);
                break;

            case "Monthly":
                if (monthlyoption == "Day")
                {
                    potentialBookings = mgmt.MonthlyByDay(booking);
                }
                else if (monthlyoption == "Increment")
                {
                    potentialBookings = mgmt.MonthlyIncrement(booking);
                }
                break;

            default:     // code for non recurring
                if (ModelState.IsValid)
                {
                    bool conflict = false;

                    foreach (var id in booking.RoomIds)
                    {
                        if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id)))
                        {
                            conflict      = true;
                            ViewBag.Error = "This room and time have been reserved.";
                        }
                    }

                    if (!conflict)
                    {
                        if (booking.EndDate < booking.StartDate)
                        {
                            ViewBag.Error = "The End Date Can't Be Before the Start Date.";
                        }
                        else
                        {
                            foreach (var id in booking.RoomIds)
                            {
                                bookingRepository.InsertBooking(booking.GetBooking(id));
                                bookingRepository.Save();
                            }

                            if (viewName == "/")
                            {
                                return(RedirectToAction("DailyBookings", new { date = booking.StartDate.ToString("yyyy-MM-dd") }));
                            }
                            else
                            {
                                return(RedirectToAction(viewName, new { date = booking.StartDate.AddDays(-(((int)booking.StartDate.DayOfWeek) + 6) % 7), roomName = viewRoomName }));
                            }
                        }
                    }
                }
                return(View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName)));
                //break;
            }

            List <Booking> failedBookings = new List <Booking>();

            foreach (var date in potentialBookings)
            {
                booking.StartDate = date.AddHours(double.Parse(booking.StartTime));
                booking.EndDate   = date.AddHours(double.Parse(booking.EndTime));

                if (ModelState.IsValid)
                {
                    foreach (var id in booking.RoomIds)
                    {
                        if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id)))
                        {
                            failedBookings.Add(booking.GetBooking(id));
                            //ViewBag.Error = "This room and time have been reserved.";
                            //potentialBookings.Remove(date);
                        }
                        else
                        {
                            bookingRepository.InsertBooking(booking.GetBooking(id));
                            bookingRepository.Save();
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();

            if (failedBookings.Count >= 1)
            {
                sb.Append("The following were not booked due to confilcts: \n\n");

                foreach (var item in failedBookings)
                {
                    string roomName = db.Rooms.
                                      Where(r => (r.RoomId.Equals(item.RoomId))).
                                      Select(r => r.RoomName).FirstOrDefault();

                    sb.Append(item.StartTime + " - " + item.EndTime.TimeOfDay + " " + roomName + "\n");
                }
            }
            TempData["ErrorList"] = sb.ToString();

            if (viewName == "/")
            {
                return(RedirectToAction("DailyBookings", new { date = resetDate.ToString("yyyy-MM-dd") }));
            }
            else
            {
                return(RedirectToAction(viewName, new { date = resetDate.AddDays(-(((int)resetDate.DayOfWeek) + 6) % 7), roomName = viewRoomName }));
            }

            //var mgmt = new BookingMgmt(bookingRepository);
            //return View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName));
        }