public IHttpActionResult UpdateRental(int id, ToolRentalDto toolRentalDto) { if (!ModelState.IsValid) { return(BadRequest()); } var rentalInDb = _context.Rentals.Include(r => r.Tool).SingleOrDefault(r => r.Id == id); if (rentalInDb == null) { return(NotFound()); } rentalInDb.DateReturned = DateTime.Now; if (rentalInDb.Tool.NumberAvailable < rentalInDb.Tool.NumberInStock && rentalInDb.IsRentalActive == true) { rentalInDb.Tool.NumberAvailable++; rentalInDb.IsRentalActive = false; } Mapper.Map(toolRentalDto, rentalInDb); _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewToolRental(ToolRentalDto toolRental) { var customer = _context.Customers.Single( c => c.Id == toolRental.CustomerId); var tools = _context.Tools.Where( t => toolRental.ToolIds.Contains(t.Id)).ToList(); foreach (var tool in tools) { if (tool.NumberAvailable == 0) { return(BadRequest("Tool isn't available for rent.")); } tool.NumberAvailable--; var rental = new Rental { Customer = customer, Tool = tool, DateRented = DateTime.Now, IsRentalActive = true }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }