public async Task <IActionResult> GetMovie(int id) { var movie = await _repository.GetMovieAsync(id); if (movie == null) { return(NotFound()); } var movieToReturn = _mapper.Map <MovieDto>(movie); return(Ok(movieToReturn)); }
public async Task <IActionResult> AddRental([FromBody] RentalForCreationDto rentalForCreationDto) { if (rentalForCreationDto == null) { return(BadRequest()); } var customer = await _repository.GetCustomerAsync(rentalForCreationDto.CustomerId); if (customer == null) { return(NotFound()); } var movie = await _repository.GetMovieAsync(rentalForCreationDto.MovieId); if (movie == null) { return(NotFound()); } if (movie.NumberInStock == 0) { return(BadRequest("Movie out of stock")); } var rental = _mapper.Map <Rental>(rentalForCreationDto); rental.RentalFee = movie.DailyRentalRate; _repository.AddRental(rental); movie.NumberInStock--; await _unitOfWork.CompleteAsync(); var rentalToReturn = _mapper.Map <RentalDto>(rental); return(CreatedAtRoute("GetRental", new { id = rentalToReturn.Id }, rentalToReturn)); }