public async Task <IActionResult> EndRental(int id, Rental rental) { if (id != rental.RentalId) { return(BadRequest()); } _context.Entry(rental).State = EntityState.Modified; try { rental.RentalEnd = DateTime.Now; rental.TotalCost = CalcTotalCost(rental); await _context.SaveChangesAsync(); return(rental); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Rental> > EndRental(int id) { var rental = await _context.Rentals.FindAsync(id); if (rental == null) { return(NotFound()); } if (rental.Ended) { return(BadRequest("Rental is already ended")); } rental.RentalEnd = System.DateTime.Now; rental.Ended = true; rental.Bike.IsRented = false; rental.Customer.HasActiveRental = false; rental.TotalCost = _calculator.CalculateCosts(rental.RentalBegin, rental.RentalEnd, rental.Bike.RentalPriceFirstHour, rental.Bike.RentalPricePerAdditionalHour); _context.Entry(rental).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.ID) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutBike(int id, Bike bike) { if (id != bike.BikeId) { return(BadRequest()); } _context.Entry(bike).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BikeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private async Task <IActionResult> PutRental(int id, Rental rental) { if (id != rental.RentalId) { return(BadRequest()); } _context.Entry(rental).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Rental> > EndRental(int id) { var rental = await _context.Rentals.FindAsync(id); if (rental == null) { return(NotFound()); } if (rental.RentalEnd != DateTime.MaxValue) { return(BadRequest("The Rental already ended")); } rental.RentalEnd = DateTime.Now; rental.Bike.IsRented = false; rental.RentalCost = calculation.CalculateCost(rental.RentalBegin, rental.RentalEnd, rental.Bike.RentalPriceFirstHour, rental.Bike.RentalPriceAdditionalHour); _context.Entry(rental).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public T Update(T item) { _context.Entry(item).State = EntityState.Modified; Save(); return(item); }