Пример #1
0
        public async Task <IActionResult> Book(int id, RentalVM vm)
        {
            //Check to see if Rental.Id == UserRentalId?

            var Users = await _context.Users.ToListAsync();

            //User users = await _context.User.FirstOrDefaultAsync(u => u.Id == id);
            string errormessage = "End Date must be before Start Date";

            UserRentals userRentals = new UserRentals()
            {
                rentalId  = id,
                userId    = vm.userRentals.userId,
                startDate = vm.userRentals.startDate.Date,
                endDate   = vm.userRentals.endDate.Date
            };

            if (userRentals.startDate.Date < userRentals.endDate.Date && userRentals.startDate.Date > DateTime.Now)
            {
                _context.Add(userRentals);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(NotFound(errormessage));
            }

            //return RedirectToAction(nameof(Index));
        }
        public IHttpActionResult CreateNewRentals(UserRentalsDto userRentalsDto)
        {
            //what we have to do, check if rentals exists in the db, if exists with the same movie and username, deny rentals else accept
            var user = _context.Users.SingleOrDefault(u => u.UserName == User.Identity.Name);

            if (user == null)
            {
                return(Content(HttpStatusCode.BadRequest, Resources.Controller_Movies_Form.access_denied));
            }

            var movie = _context.Movies.SingleOrDefault(m => userRentalsDto.MovieId == m.Id);

            //if (movie.NumberAvailable == 0)
            //    return Content(HttpStatusCode.BadRequest, "Movie is not available at");

            //movie.NumberAvailable--;

            //check if user and movie exists in user rentals already
            var rentalExists = _context.UserRentals.SingleOrDefault(r => r.Users.Id == user.Id && r.Movie.Id == userRentalsDto.MovieId);

            //if rentals exists is not  equal to null
            if (rentalExists != null)
            {
                return(Content(HttpStatusCode.BadRequest, Resources.Controller_Movies_Form.already_in_rentals));
            }

            var rental = new UserRentals
            {
                DateRented   = DateTime.Now,
                DateReturned = DateTime.Now.AddDays(30),
                Movie        = movie,
                Users        = user
            };

            _context.UserRentals.Add(rental);

            _context.SaveChanges();


            /*
             *  steps
             *  user clicks on rent
             *  js gets user id and movie id
             *  checks rentals table if user id and movie id exists
             *  if exists throw error, only one is acceptable
             *  else check number is available if > 0 continue else throw error not enough
             *  add to userrentals table
             */
            return(Ok());
        }