public IHttpActionResult Get(int id) { var customerRentals = new CustomerRentalsDto() { CustomerId = id, RentedMovies = _context.Rentals .Where(r => r.DateReturned == null) .Where(r => r.CustomerId == id) .Include(r => r.Movie) .Select(r => new RentedMovieDto() { MovieId = r.Movie.Id, MovieName = r.Movie.Name, DateRented = r.DateRented }).ToList() }; return(Ok(customerRentals)); }
// GET api/<controller>/5 public IHttpActionResult Get(int id) { // id is the customer id var CustomerInDb = _context.Customers.SingleOrDefault(c => c.Id == id); if (CustomerInDb == null) { return(NotFound()); // throw new HttpResponseException(HttpStatusCode.NotFound); } // WeakReference will prepare CustomerRentalsDto to return var Rentals = _context.Rentals.Include(c => c.Movie).Where(r => r.Customer.Id == id).ToList(); CustomerRentalsDto custRents = new CustomerRentalsDto() { Customer = Mapper.Map <Customer, CustomerDto>(CustomerInDb), Rentals = Rentals }; return(Ok(custRents)); }