public async Task <IActionResult> Create(RentFormViewModel obj)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                var account = await _accountService.GetAccountByUser(user);

                var car = await _carService.FindByIdAsync(obj.Rent.CarId);

                var value = car.DailyPrice * obj.Rent.End.Subtract(obj.Rent.Start).Days;
                if (account.Balance < value)
                {
                    TempData["erro"] = "Saldo insuficiente para alugar o veículo.";
                    return(RedirectToAction("Index", "Cars"));
                }

                account.Balance -= obj.Rent.Value;
                await _accountService.UpdateAsync(account);

                TempData["confirm"] = obj.Car.Brand + " - " + obj.Car.Model + " foi alugado com sucesso.";
                obj.Rent.UserId     = user.Id;
                await _rentService.InsertAsync(obj.Rent);

                list = await _rentService.FindAllAsync();

                _cache.Set("rent", list, cacheOptions);

                return(RedirectToAction("Index", "Accounts"));
            }

            TempData["erro"] = "Erro ao alugar.";
            return(RedirectToAction("Index", "Cars"));
        }
예제 #2
0
        public ActionResult Save(Rent rent) // recebemos um rente
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new RentFormViewModel
                {
                    Rent = rent
                };

                return(View("RentForm", viewModel));
            }

            if (rent.Id == 0)
            {
                // armazena o rente em memória
                _context.Rents.Add(rent);
            }
            else
            {
                var rentInDb = _context.Rents.Single(c => c.Id == rent.Id);

                rentInDb.RentDate = rent.RentDate;
                rentInDb.Book     = rent.Book;
                rentInDb.Client   = rent.Client;
            }

            // faz a persistência
            _context.SaveChanges();
            // Voltamos para a lista de rentes
            return(RedirectToAction("Index"));
        }
예제 #3
0
        public ActionResult New()
        {
            var rentmodel = new RentFormViewModel
            {
                Rent = new Rent()
            };

            return(View("RentForm", rentmodel));
        }
예제 #4
0
        public ActionResult Rent()
        {
            var viewModel = new RentFormViewModel
            {
                Shops = _Context.ShopRequired.ToList(),
                Units = _Context.UnitsRequired.ToList()
            };

            return(View(viewModel));
        }
예제 #5
0
        public ActionResult Index()
        {
            var model = new RentFormViewModel(CurrentPage)
            {
                CurrentPage = CurrentPage
            };

            var umbCategories = CoreHelpers.CurrentSite().GetPropertyValue <string>("carCategories");

            model.AllCarCategories = CoreHelpers.TextareaToSelectListItems(umbCategories);

            return(CurrentTemplate(model));
        }
        public async Task <IActionResult> Create(int id)
        {
            var car = await _carService.FindByIdAsync(id);

            var rent = new Rent {
                CarId = id
            };
            var viewModel = new RentFormViewModel {
                Car = car, Rent = rent
            };

            return(View(viewModel));
        }
예제 #7
0
        public ActionResult Edit(int id)
        {
            var rentInDb = _context.Rents.SingleOrDefault(c => c.Id == id);

            if (rentInDb == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new RentFormViewModel
            {
                Rent = rentInDb
            };

            return(View("rentForm", viewModel));
        }
예제 #8
0
        public ActionResult Rent(int id)
        {
            var car = _context.Cars.SingleOrDefault(c => c.Id == id);

            if (car == null)
            {
                return(HttpNotFound());
            }

            var rent      = new Rent();
            var viewModel = new RentFormViewModel()
            {
                Car        = car,
                Customer   = rent.Customer,
                Username   = User.Identity.Name,
                RentedDate = DateTime.Now,
                CarId      = car.Id
            };


            return(View(viewModel));
        }
예제 #9
0
        public ActionResult Rent(RentFormViewModel ViewModel)
        {
            var rentalId = User.Identity.GetUserId();
            var shops    = _Context.ShopRequired.Single(s => s.Id == ViewModel.ShopRequired);
            var units    = _Context.UnitsRequired.Single(u => u.Id == ViewModel.UnitsRequired);
            var rental   = _Context.Users.Single(u => u.Id == rentalId);

            var rent = new Details
            {
                Rental        = rental,
                Company       = ViewModel.Company,
                Contact       = ViewModel.Contact,
                Email         = ViewModel.Email,
                ShopRequired  = shops,
                UnitsRequired = units
            };

            _Context.Details.Add(rent);
            _Context.SaveChanges();



            return(RedirectToAction("Final", "Submit"));
        }