public IHttpActionResult Save(RentalDto newRental) { var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where( m => newRental.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; var rental = new Rental //Create new record in Rentals for each movie being rented in this execution { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult Create(RentalDto rentalDto) { var movieInDb = _context.MoviesModel.Where(m => rentalDto.MoviesId.Contains(m.Id)).ToList(); var customerInDb = _context.Customers.Single(c => c.Id == rentalDto.CustomerId); foreach (var movie in movieInDb) { if (movie.NumberAvailable == 0) { return(BadRequest("There is no copy of this movie available.")); } var newRental = new Rental { Customer = customerInDb, Movie = movie, DateRented = DateTime.Now }; _context.Rental.Add(newRental); movie.NumberAvailable--; } _context.SaveChanges(); return(Ok()); }
public SimpleResponse <RentalDto> Create(RentalDto dto) { var response = new SimpleResponse <RentalDto>(); try { var model = SimpleMapper.Map <RentalDto, RentalViewModel>(dto); var resp = iRentalBusiness.Create(model); response = new SimpleResponse <RentalDto>() { ResponseCode = resp.ResponseCode, ResponseMessage = resp.ResponseMessage, RCode = resp.RCode }; response.Data = SimpleMapper.Map <RentalViewModel, RentalDto>(resp.Data); } catch (Exception ex) { response.ResponseCode = BusinessResponseValues.InternalError; response.ResponseMessage = "Okuma iþleminde hata oluþtu."; SimpleFileLogger.Instance.Error(ex); } return(response); }
public IActionResult Create([FromBody] RentalDto rental) { //NO AUTH try { context.Rentals.Add((Rental)rental); context.SaveChanges(); return(Ok(200)); } catch (Exception) // Find what is Error 403 in exception form { return(StatusCode(403)); } //catch (Exception) //{ // return StatusCode(401); //} //catch (Exception) //{ // return StatusCode(404); //} //catch (Exception) //{ // return StatusCode(406); //} }
public async Task DeleteRental(RentalDto rental) { var entityRental = await _unitOfWork.Rentals.GetByIdAsync(rental.Id); _unitOfWork.Rentals.Remove(entityRental); await _unitOfWork.CommitAsync(); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { var movie = _context.Movies.Single(m => m.Id == rentalDto.MovieId); if (movie == null) { return(BadRequest("Invalid MovieId.")); } if (movie.NumberAvailable < 1) { return(BadRequest("Movie is not currently available.")); } var customer = _context.Customers.SingleOrDefault(c => c.Id == rentalDto.CustomerId); if (customer == null) { return(BadRequest("Invalid CustomerId.")); } movie.NumberAvailable--; var rental = Mapper.Map <RentalDto, Rental>(rentalDto); rental.Customer = customer; rental.Movie = movie; _context.Rentals.Add(rental); _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto newRental) { var customer = _context.Customers.Single( c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where( m => newRental.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.AvailableAmount == 0) { return(BadRequest("Movie is not available.")); } movie.AvailableAmount--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now, MovieId = movie.Id, CustomerId = customer.Id, BeenReturned = newRental.BeenReturned }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { if (rentalDto.MovieIds.Count == 0) { return(BadRequest("No MovieIds have been given.")); } var customer = _context.Customers.Single(c => c.Id == rentalDto.CustomerId); var movies = _context.Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); foreach (Movie movie in movies) { if (movie.CopiesAvailable == 0) { return(BadRequest("No copies available")); } movie.CopiesAvailable--; var newRental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(newRental); } _context.SaveChanges(); return(Ok(rentalDto)); }
public IHttpActionResult NewRentals(RentalDto rentalDto) { var customer = _context.Customer.SingleOrDefault(c => c.Id == rentalDto.CustomerId); var movies = _context.Movie.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.StockAvailabilty == 0) { return(BadRequest("Movie is out of stock")); } movie.StockAvailabilty--; var rental = new Rental() { Customer = customer, CustomerId = customer.Id, Movie = movie, MovieId = movie.Id, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { bool allMoviesAvailable = true; var customer = _context.Customers.Single(c => c.Id == rentalDto.CustomerId); var movies = _context.Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable != 0) { var rental = new Rental(customer, movie); _context.Rentals.Add(rental); } else { allMoviesAvailable = false; } } _context.SaveChanges(); if (allMoviesAvailable) { return(Ok()); } return(BadRequest("One or more movies were not available")); }
public IHttpActionResult CreateRentals(RentalDto rentalDto) { var dbCustomer = _context.Customers.Single(c => c.Id == rentalDto.CustomerId); var dbBooks = _context.Books.Where(b => rentalDto.BookIds.Contains(b.Id)).ToList(); foreach (var book in dbBooks) { if (book.NumberAvailable == 0) { return(BadRequest("Book is out of stock.")); } book.NumberAvailable--; var rental = new Rental { Customer = dbCustomer, Book = book, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> Create(RentalDto rentalDto) { var customer = await _context.Customers.SingleOrDefaultAsync(c => c.Id == rentalDto.Customer); var movies = await _context.Movies.Where(m => rentalDto.Movie.Contains(m.Id)).ToListAsync(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(NotFound()); } movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Add(rental); } await _context.SaveChangesAsync(); return(View("Index")); }
public IHttpActionResult CreateRental(RentalDto dto) { var customer = _db.Customers.Single( c => c.Id == dto.CustomerId); // SELECT * FROM Movies IN (1,2,3,4) var movies = _db.Movies.Where( m => dto.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.AmountAvailable == 0) { return(BadRequest("Amount of available movies is 0!")); } movie.AmountAvailable--; var rental = new Rental { Customer = customer, Movie = movie, RentedDate = DateTime.Now }; _db.Rentals.Add(rental); } _db.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { var customer = Customers.Single(c => c.Id == rentalDto.CustomerId); var movies = Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available")); } var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; movie.NumberInStock--; _context.Rentals.Add(rental); } if (!ModelState.IsValid) { return(BadRequest()); } SaveChanges(); return(Ok()); }
public IHttpActionResult NewRental(RentalDto newRental) { if (!ModelState.IsValid) { return(BadRequest()); } var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList(); var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); foreach (var movie in movies) { if (movie.NumberAvailable > 0) { movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } else { return(BadRequest("Movie is not available.")); } } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(RentalDto newRental) { var customer1 = _context.Customers.SingleOrDefault( c => c.id == newRental.customerId); var movies = _context.Products.SingleOrDefault( m => m.Id == newRental.productId); if (movies.Stock == 0) { return(BadRequest("Movie is not Available")); } movies.NumberAvailable--; movies.Stock--; var rental = new Rental { customer = customer1, product = movies, DateRented = DateTime.Now }; _context.Rentals.Add(rental); _context.SaveChanges(); return(Ok()); }
public async Task <ActionResult> Post(RentalModel model) { if (!ModelState.IsValid) { return(this.BadRequest(this.GetErrorJsonFromModelState())); } var dto = new RentalDto { BookId = model.BookId, CustomerId = model.CustomerId, RentDate = DateTime.UtcNow, ReturnDate = model.ReturnDate, }; this.PopulateAuditFieldsOnCreate(dto); var response = await this._rentalService.InsertAsync(new GenericRequest <RentalDto> { Data = dto }); if (response.IsError()) { return(this.BadRequest(this.GetErrorJson(response))); } return(this.Ok(this.GetSuccessJson(response, response.Data))); }
public IHttpActionResult CreateRentals(RentalDto rental) { var customer = _context.Customers.Single( c => c.Id == rental.CustomerId); var movies = _context.Movies.Where( m => rental.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available")); } movie.NumberAvailable--; var newRental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(newRental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { Customer CustomerInDB = _context.Customers.Single( c => c.Id == rentalDto.CustomerId); var movies = _context.Movies.Where( m => rentalDto.MovieIds.Contains(m.Id)); DateTime DateRented = DateTime.Now; foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; var Rental = new Rental { Customer = CustomerInDB, DateRented = DateRented, Movie = movie }; _context.Rentals.Add(Rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto newRental) { //if(newRental.MovieIds.Count == 0) // return BadRequest("No Movie Ids have been given."); //var customer = context.Customers.SingleOrDefault(c => c.Id == newRental.CustomerId); var customer = context.Customers.Single(c => c.Id == newRental.CustomerId); //if(customer == null) // return BadRequest("CustomerId is not valid."); var movies = context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList(); //if(movies.Count != newRental.MovieIds.Count) // return BadRequest("One or more MovieIds are invalid."); foreach (var movie in movies) { if (movie.Available == 0) { return(BadRequest("Movie is not available.")); } Rental rental = new Rental(customer, movie); context.Rentals.Add(rental); } context.SaveChanges(); return(Ok()); }
public IHttpActionResult Save(RentalDto rentalDto) { var customer = _context.Customers.Single(c => c.Id == rentalDto.CustomerId); var Movies = _context.Movies.Where( m => rentalDto.MovieIds.Contains(m.Id)).ToList(); foreach (var item in Movies) { if (item.NoInAvailable == 0) { return(BadRequest("Movie is not Available")); } item.NoInAvailable = item.NoInAvailable - 1; var rental = new Rental { Customer = customer, Movie = item, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public async Task Add_WhenAddRental_ShouldSaveTheRental() { var clientId = 1; var vehicleId = 1; _dbContext.Vehicles.Add(new Vehicle { Description = "Ford Focus", PricePerDay = 10 }); _dbContext.SaveChanges(); var rental = new RentalDto { ClientId = clientId, VehicleId = vehicleId, StartDate = new DateTime(2021, 4, 1), EndDate = new DateTime(2021, 4, 10) }; await _sut.AddNewRental(rental); _dbContext.Rentals.Count().Should().Be(1); var rentalAdded = await _dbContext.Rentals.FirstOrDefaultAsync(); rentalAdded.Id.Should().Be(1); rentalAdded.ClientId.Should().Be(clientId); rentalAdded.VehicleId.Should().Be(vehicleId); rentalAdded.StartDate.Should().Be(new DateTime(2021, 4, 1)); rentalAdded.EndDate.Should().Be(new DateTime(2021, 4, 10)); rentalAdded.Price.Should().Be(90); rentalAdded.Canceled.Should().BeFalse(); }
public async Task <IActionResult> PutRental(int id, RentalDto rentalDto) { if (id != rentalDto.Id) { return(BadRequest()); } try { var rental = _mapper.Map <Rental>(rentalDto); await _repository.Update(rental); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PostNewRental(RentalDto RentalDto) { //if (RentalDto.ActivityIds.Count == 0) // return BadRequest("there no activity available"); var subscriber = _db.Subscribers .SingleOrDefault(c => c.Id == RentalDto.SubscriberId); if (subscriber == null) { return(NotFound()); } var activities = _db.Activiies .Where(m => RentalDto.ActivityIds.Contains(m.Id)).ToList(); //if (activities.Count != RentalDto.ActivityIds.Count) // return BadRequest("one or more of activities not loaded "); foreach (var activity in activities) { var rental = new Rental { Subscriber = subscriber, Activity = activity, DateRented = DateTime.Now }; _db.Rentals.Add(rental); } _db.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(RentalDto rentalDto) { Customer customer = _context.Customers.FirstOrDefault(c => c.Id == rentalDto.CustomerId); if (customer == null) { return(BadRequest("Customer Not Found")); } foreach (int id in rentalDto.MovieIds) { Movie movie = _context.Movies.FirstOrDefault(m => m.Id == id && m.NumberAvailable > 0); if (movie != null) { movie.NumberAvailable -= 1; _context.Rentals.Add(new Rental { Customer = customer, DateRented = DateTime.Now, Movie = movie }); _context.SaveChanges(); } } return(Ok()); }
public IHttpActionResult CreateNewRentals(RentalDto NewRentaldto) { var customer = db.Customers.SingleOrDefault(c => c.id == NewRentaldto.CustomerId); if (customer == null) { return(NotFound()); } var movies = db.Movies.Where(m => NewRentaldto.MoviesId.Contains(m.id)).ToList(); var newRentals = new List <Rental>(); foreach (var movie in movies) { var movieAvailability = db.Movies.SingleOrDefault(m => m.id == movie.id); if (movieAvailability.NumberAvailable != 0) { movieAvailability.NumberAvailable = movieAvailability.NumberAvailable - 1; db.Rentals.Add(new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }); db.SaveChanges(); } } return(Ok(new { message = "success" })); }
public IHttpActionResult CreateRental(RentalDto rental) { var customer = _context.Customers.SingleOrDefault(c => c.Id == rental.CustomerId); //customer Id not valid if (customer == null) { return(BadRequest("Customer Id is not valid.")); } //No movies Ids if (rental.MovieIds.Count == 0) { return(BadRequest("No Movies Ids have been given.")); } //Customer is delinquent if (customer.Delinquent) { return(BadRequest("Customer is delinquent.")); } //Customer trying to rent more than 5 movies if (rental.MovieIds.Count > 5) { return(BadRequest("You can't rent more than 5 movies at one time.")); } var movies = _context.Movies.Where( m => rental.MovieIds.Contains(m.Id)).ToList(); //One or more MovieIds are invalid. if (movies.Count != rental.MovieIds.Count) { return(BadRequest("One or more MovieIds are invalid.")); } foreach (var movie in movies) { //Movie is not available. if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; var newRental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(newRental); } _context.SaveChanges(); //we didn't use created because we have multiple resources return(Ok()); }
public void DeleteSingleRental(RentalDto rental) { var rentalForDeletion = _context.Rentals.Find(rental.Id); _context.Rentals.Remove(rentalForDeletion); _context.SaveChanges(); }
public RentalFormViewModel(RentalDto rental) { Id = rental.Id; CustomerId = rental.CustomerId; CarId = rental.CarId; DateRented = rental.DateRented; DateReturned = rental.DateReturned; }
// POST api/AddRental public IHttpActionResult AddRental(RentalDto rentalDto) { if (!ModelState.IsValid) { return(BadRequest("Please check input parameters")); } if (rentalDto.Movies.Count == 0) { return(BadRequest("Please provide movies")); } var customer = _dbContext.Customer.SingleOrDefault(c => c.Id == rentalDto.CustomerId); if (customer == null) { return(BadRequest("Invalid customer")); } var movies = _dbContext.Movie.Where(m => rentalDto.Movies.Contains(m.Id)).ToList(); if (movies == null) { return(BadRequest("Invalid movies")); } if (movies.Count != rentalDto.Movies.Count) { return(BadRequest("One or more movies not found")); } foreach (var movieId in rentalDto.Movies) { var currentMovie = movies.Single(m => m.Id == movieId); if (currentMovie.NumberAvailable == 0) { return(BadRequest("One or more movies not available")); } else { Rental rentalData = new Rental { CustomerId = rentalDto.CustomerId, MovieId = movieId, RentalDate = DateTime.Now, }; _dbContext.Rental.Add(rentalData); currentMovie.NumberAvailable--; } } _dbContext.SaveChanges(); return(Ok()); }