Exemple #1
0
        public ActionResult Index(int?pagina, string procura, string regiao, string intervalo)
        {
            var fillStationTimeBreaks = db.FillStationTimeBreaks.Include(f => f.FillStation).Include(f => f.TimeBreak);
            //var po = fillStationTimeBreaks.ToList();
            List <FillStationTimeBreak> po;

            if (User.IsInRole("Admin"))
            {
                po = fillStationTimeBreaks.ToList();
            }
            else
            {
                List <FillStationTimeBreak> selectListItems = new List <FillStationTimeBreak>();

                foreach (var item in fillStationTimeBreaks.ToList())
                {
                    foreach (var fillstat in db.FillStations.ToList())
                    {
                        foreach (var sation in db.Stations.ToList())
                        {
                            //fillstat.Station.OwnerId
                            if (fillstat.StationId == sation.Id && item.FillStationId == fillstat.Id && sation.OwnerId == User.Identity.GetUserId())
                            {
                                selectListItems.Add(item);
                            }
                        }
                    }
                }

                po = selectListItems;
            }


            if (!String.IsNullOrEmpty(procura))
            {
                po = po.Where(f => f.FillStation.Name.Contains(procura)).ToList();
            }

            if (!String.IsNullOrEmpty(regiao) && !regiao.Contains("*"))
            {
                int regionId = Int32.Parse(regiao);
                po = po.Where(f => f.FillStation.Station.RegionId == regionId).ToList();
            }

            if (!String.IsNullOrEmpty(intervalo) && !intervalo.Contains("*"))
            {
                int timeBreakId = Int32.Parse(intervalo);
                po = po.Where(f => f.TimeBreakId == timeBreakId).ToList();
            }

            int pag = (pagina ?? 1);

            var user = User.Identity.GetUserId();

            ViewBag.ListRegions   = ListRegionsFilter.createListItems(db);
            ViewBag.TimeBreakList = ListTimeBreakFilter.createListItems(db);

            return(View(po.ToPagedList(pag, 5)));
        }
        public ActionResult Index(int?pagina, string procura, string regiao, string intervalo)
        {
            if (!User.IsInRole("User"))
            {
                var po = db.Reservations.ToList();

                if (!String.IsNullOrEmpty(procura))
                {
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Name.Contains(procura)).ToList();
                }

                if (!String.IsNullOrEmpty(regiao) && !regiao.Contains("*"))
                {
                    int regionId = Int32.Parse(regiao);
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Station.RegionId == regionId).ToList();
                }

                if (!String.IsNullOrEmpty(intervalo) && !intervalo.Contains("*"))
                {
                    int timeBreakId = Int32.Parse(intervalo);
                    po = po.Where(f => f.FillStationTimeBreak.TimeBreakId == timeBreakId).ToList();
                }

                ViewBag.ListRegions   = ListRegionsFilter.createListItems(db);
                ViewBag.TimeBreakList = ListTimeBreakFilter.createListItems(db);

                int pag = (pagina ?? 1);
                return(View(po.ToPagedList(pag, 5)));
            }
            else
            {
                var po = db.Reservations.ToList().Where(m => m.UserId == User.Identity.GetUserId());

                if (!String.IsNullOrEmpty(procura))
                {
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Name.Contains(procura)).ToList();
                }

                if (!String.IsNullOrEmpty(regiao) && !regiao.Contains("*"))
                {
                    int regionId = Int32.Parse(regiao);
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Station.RegionId == regionId).ToList();
                }

                if (!String.IsNullOrEmpty(intervalo) && !intervalo.Contains("*"))
                {
                    int timeBreakId = Int32.Parse(intervalo);
                    po = po.Where(f => f.FillStationTimeBreak.TimeBreakId == timeBreakId).ToList();
                }

                ViewBag.ListRegions   = ListRegionsFilter.createListItems(db);
                ViewBag.TimeBreakList = ListTimeBreakFilter.createListItems(db);

                int pag = (pagina ?? 1);
                return(View(po.ToPagedList(pag, 5)));
            }
        }
        public ActionResult Create(int?pagina)
        {
            if (User.IsInRole("Admin"))
            {
                return(RedirectToAction("Index"));
            }

            var po = ListReservationViewModel.createListItems(db);

            ViewBag.ListRegions   = ListRegionsFilter.createListItems(db);
            ViewBag.TimeBreakList = ListTimeBreakFilter.createListItems(db);

            int pag = (pagina ?? 1);

            return(View(po.ToPagedList(pag, 5)));
        }
        public ActionResult Create(double?Price,
                                   DateTime?submitdate, int?TimeBreakId, int?pagina, string procura, string regiao, string intervalo)
        {
            int pag = 0;

            ViewBag.ListRegions   = ListRegionsFilter.createListItems(db);
            ViewBag.TimeBreakList = ListTimeBreakFilter.createListItems(db);

            if (!String.IsNullOrEmpty(procura) || !String.IsNullOrEmpty(regiao) || !String.IsNullOrEmpty(intervalo))
            {
                var po = ListReservationViewModel.createListItems(db);

                if (!String.IsNullOrEmpty(procura))
                {
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Name.Contains(procura)).ToList();
                }

                if (!String.IsNullOrEmpty(regiao) && !regiao.Contains("*"))
                {
                    int regionId = Int32.Parse(regiao);
                    po = po.Where(f => f.FillStationTimeBreak.FillStation.Station.RegionId == regionId).ToList();
                }

                if (!String.IsNullOrEmpty(intervalo) && !intervalo.Contains("*"))
                {
                    int timeBreakId = Int32.Parse(intervalo);
                    po = po.Where(f => f.FillStationTimeBreak.TimeBreakId == timeBreakId).ToList();
                }

                pag = (pagina ?? 1);
                return(View(po.ToPagedList(pag, 5)));
            }

            Reservation reservation = new Reservation();

            if (TimeBreakId != null)
            {
                reservation.FillStationTimeBreakId = (int)TimeBreakId;
            }
            else
            {
                return(View(ListReservationViewModel.createListItems(db)));
            }

            int fillStationId = db.FillStationTimeBreaks.Find(TimeBreakId).FillStationId;

            if (Price != null)
            {
                reservation.Price = VerifyPromotion.verifyPrice(db, (DateTime)submitdate, fillStationId);
            }
            else
            {
                return(View(ListReservationViewModel.createListItems(db)));
            }

            if (submitdate != null)
            {
                reservation.Day = (DateTime)submitdate;
            }
            else
            {
                return(View(ListReservationViewModel.createListItems(db)));
            }

            if (User.IsInRole("User"))
            {
                reservation.UserId = User.Identity.GetUserId();
            }

            if (ModelState.IsValid && !ValidateReservation.AlreadyExistsReservation(reservation))
            {
                // ver se o utilizador tem dinheiro suficiente
                var userId = User.Identity.GetUserId();
                foreach (BankInfo item in db.BankInfos.ToList())
                {
                    if (item.UserId == userId)
                    {
                        if (reservation.Price < item.Quant)
                        {
                            item.Quant = item.Quant - reservation.Price;

                            //// atualizar a base de dados
                            db.Reservations.Add(reservation);
                            db.SaveChanges();
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, "Não tem dinheiro suficiente.");
                            pag = (pagina ?? 1);
                            return(View(ListReservationViewModel.createListItems(db).ToPagedList(pag, 5)));
                        }

                        break;
                    }
                }

                //// atualizar a base de dados
                //db.Reservations.Add(reservation);
                //db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "A reserva já existe na base de dados.");
            }



            pag = (pagina ?? 1);
            return(View(ListReservationViewModel.createListItems(db).ToPagedList(pag, 5)));
        }