// GET: LocalReservation
 public ActionResult LocalReservation(long bookingUnitId)
 {
     if (!Request.IsAuthenticated)
     {
         return(RedirectToAction("Login", "Account"));
     }
     else
     {
         BookingUnitsDisplayRepo budRepo = new BookingUnitsDisplayRepo();
         var model = new LocalReservationViewModel();
         model.BookingUnitId   = bookingUnitId;
         model.BookingUnitName = budRepo.GetBookingUnitNameById(bookingUnitId);
         model.DateFrom        = DateTime.Now;
         model.DateTo          = DateTime.Now;
         return(View(model));
     }
 }
예제 #2
0
        public MyRemoteServices.addLocalReservationRequest GetLocalReservationRequest(LocalReservationViewModel lrVM)
        {
            using (var ctx = new ApplicationDbContext())
            {
                long   bookingUnitId = (long)ctx.BookingUnits.FirstOrDefault(x => x.Id == lrVM.BookingUnitId).MainServerId;
                string dateFrom      = string.Format("{0}-{1}-{2}", lrVM.DateFrom.Year, lrVM.DateFrom.Month, lrVM.DateFrom.Day);
                string dateTo        = string.Format("{0}-{1}-{2}", lrVM.DateTo.Year, lrVM.DateTo.Month, lrVM.DateTo.Day);
                string firstName     = "";
                if (lrVM.ReserveeFirstName != null)
                {
                    firstName = lrVM.ReserveeFirstName;
                }
                string lastName = "";
                if (lrVM.ReserveeLastName != null)
                {
                    lastName = lrVM.ReserveeLastName;
                }

                MyRemoteServices.Reservation resData = new MyRemoteServices.Reservation
                {
                    bookingUnitMainServerId = bookingUnitId,
                    dateFrom          = dateFrom,
                    dateTo            = dateTo,
                    reserveeFirstName = firstName,
                    reserveeLastName  = lastName
                };

                MyRemoteServices.addLocalReservationRequest retObj = new MyRemoteServices.addLocalReservationRequest
                {
                    localReservation = resData
                };

                return(retObj);
            }
        }
        public ActionResult AddLocalReservation(LocalReservationViewModel lrVM)
        {
            if (!Request.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    int  totalDays     = (lrVM.DateTo - lrVM.DateFrom).Days;
                    bool isCorrectDate = true;
                    var  now           = DateTime.Now;
                    var  checkDate     = new DateTime(now.Year, now.Month, now.Day);
                    if (checkDate > lrVM.DateFrom)
                    {
                        isCorrectDate = false;
                    }

                    if (totalDays <= 30 && isCorrectDate)
                    {
                        //send a request on the main server and await for the response
                        DTOHelper dtoHlp = new DTOHelper();
                        MyRemoteServices.AgentEndpointPortClient     aepc        = new MyRemoteServices.AgentEndpointPortClient();
                        MyRemoteServices.addLocalReservationRequest  alrRequest  = dtoHlp.GetLocalReservationRequest(lrVM);
                        MyRemoteServices.addLocalReservationResponse alrResponse = aepc.addLocalReservation(alrRequest);

                        if (alrResponse.responseWrapper.success)
                        {
                            //save localy
                            using (var ctx = new ApplicationDbContext())
                            {
                                BookingUnit          myUnit    = ctx.BookingUnits.FirstOrDefault(x => x.Id == lrVM.BookingUnitId);
                                TotalPriceCalculator totPrCalc = new TotalPriceCalculator();
                                double totalPrice = totPrCalc.CalculateTotalPrice(lrVM.BookingUnitId, lrVM.DateFrom, lrVM.DateTo);

                                Reservation localReservation = new Reservation
                                {
                                    ReservationStatus = ReservationStatus.WAITING,
                                    SubjectName       = lrVM.ReserveeFirstName,
                                    SubjectSurname    = lrVM.ReserveeLastName,
                                    From         = lrVM.DateFrom,
                                    To           = lrVM.DateTo,
                                    BookingUnit  = myUnit,
                                    TotalPrice   = totalPrice,
                                    MainServerId = (long)alrResponse.responseWrapper.responseBody
                                };

                                //add the new item
                                ctx.Reservations.Add(localReservation);

                                //save changes
                                ctx.SaveChanges();
                            }
                        }
                        else
                        {
                            //some error happened, retry
                            TempData["error"] = alrResponse.responseWrapper.message;
                            return(RedirectToAction("LocalReservation", "Agent", new { bookingUnitId = lrVM.BookingUnitId }));
                        }

                        TempData["success"] = "Successfully added a local reservation of the unit: " + lrVM.BookingUnitName;
                        return(RedirectToAction("AgentPage", "Agent"));
                    }
                    else
                    {
                        //max days limit or begin date exception
                        TempData["error"] = "The begin date must be today's date or greater. The maximum number of days for one reservation is 30";
                        return(RedirectToAction("LocalReservation", "Agent", new { bookingUnitId = lrVM.BookingUnitId }));
                    }
                }
                else
                {
                    //invalid VM exception
                    TempData["error"] = "Some form atributes are incorrect";
                    return(RedirectToAction("LocalReservation", "Agent", new { bookingUnitId = lrVM.BookingUnitId }));
                }
            }
        }