/// <summary> /// Delete booking from db. /// </summary> /// <param name="bookingId">Id of booking to delete.</param> public void DeleteBooking(int bookingId) { Booking b = repos.FindBy <Booking>(book => book.Id == bookingId).FirstOrDefault(); if (null != b && b.CarInGarage) { try { repos.Remove(b); repos.SaveChanges(); } catch (DbUpdateException ex) { DbUpdateFault faulty = new DbUpdateFault(); faulty.Message = ex.Message; faulty.Description = "Something went wrong when trying to delete booking from db."; throw new FaultException <DbUpdateFault>(faulty); } } else { InvalidOperationFault fault = new InvalidOperationFault(); fault.Message = "Error when trying to delete booking."; fault.Description = "Booking with that id could not be deleted from database, please review input. Maybe" + "the booking was not found in system or the rented car has not been returned by customer."; throw new FaultException <InvalidOperationFault>(fault); } }
/// <summary> /// Add customer to db. /// </summary> /// <param name="customer">Customer object containing data to be added to db.</param> /// <returns>An int representing Id of added customer.</returns> public int AddCustomer(Customer customer) { if (String.IsNullOrEmpty(customer.FirstName) || String.IsNullOrEmpty(customer.LastName) || String.IsNullOrEmpty(customer.Email)) { RequiredInputOmittedFault fault = new RequiredInputOmittedFault(); fault.Message = "Error when trying to add customer!"; fault.Description = "One or more of the fields: firstname, lastname, email were left empty.Can't save customer, please check your input."; throw new FaultException <RequiredInputOmittedFault>(fault); } else { try { repos.Add(customer); repos.SaveChanges(); return(customer.Id); } catch (DbUpdateException ex) { DbUpdateFault fault = new DbUpdateFault(); fault.Message = ex.Message; fault.Description = "Could not save customer to database since the email address was already registered in system, please check that input was correct."; throw new FaultException <DbUpdateFault>(fault); } } }
/// <summary> /// Delete car from database, set property "Deleted" to true and property /// "Available" to false. If already listed as deleted: throw exception. /// </summary> /// <param name="carId">Id of car to delete.</param> public void DeleteCar(int carId) { Car c = repos.FindBy <Car>(car => car.Id == carId).FirstOrDefault(); List <Booking> b = GetBookings() .Where(book => book.CarId == carId).ToList(); foreach (Booking book in b) { if (book.EndTime >= DateTime.Today) { DbUpdateFault fault = new DbUpdateFault(); fault.Message = "Error when trying to delete car."; fault.Description = "Car has active bookings, can't be deleted from system at the moment."; throw new FaultException <DbUpdateFault>(fault); } } if (c.Deleted) { throw new FaultException("Car already deleted in system!"); } else { c.Deleted = true; c.AvailableForBooking = false; repos.Edit(c); repos.SaveChanges(); } }