public Carpool AddMovementToCarpool(Carpool carpool, Movement movement)
 {
     if (carpool != null && movement != null)
     {
         _db.Entry(carpool).State = EntityState.Modified;
         _db.SaveChanges();
     }
   
  
     return null;
 }
 public ActionResult AddMovementToCarpool(Carpool selectedCarpool, Movement newMovement)
 {
     CarpoolService carpoolService = new CarpoolService();
     Carpool newCarpool = carpoolService.AddMovementToCarpool(selectedCarpool, newMovement);
     return View(newCarpool);
 }
        private Movement SaveNewTrip(NewTrip newTrip) {
            var movement = new Movement();
            var db = new AirportCarpoolDbContext();
            db.Entry(movement).State = EntityState.Added;

            var user = GetUserByUserName(db, WebSecurity.CurrentUserName);
            db.Entry(user).State = EntityState.Unchanged;

            movement.User = user;
            Location locationFrom = null;
            if (newTrip.LocationFrom == "Home" && user.Location != null) {
                locationFrom = Location.GetCopy(user.Location);
            } else if (newTrip.LocationFrom == "Schiphol") {
                locationFrom = Location.GetNewSchipholLocation();
            }
            if (locationFrom != null) {
                db.Entry(locationFrom).State = EntityState.Added;
            }
            movement.LocationFrom = locationFrom;

            Location locationTo = null;
            if (newTrip.LocationTo == "Home" && user.Location != null) {
                locationTo = Location.GetCopy(user.Location);
            } else if (newTrip.LocationTo == "Schiphol") {
                locationTo = Location.GetNewSchipholLocation();
            }
            if (locationTo != null) {
                db.Entry(locationTo).State = EntityState.Added;
            }
            movement.LocationTo = locationTo;

            movement.Driver = newTrip.Driver;
            movement.Passenger = newTrip.Passenger;
            movement.Seats = newTrip.Seats;
            movement.Luggage = newTrip.Luggage;
            if (newTrip.Driver) {
                movement.MaxSeats = newTrip.MaxSeats;
                movement.MaxLuggage = newTrip.MaxLuggage;
                movement.MaxKm = newTrip.MaxKm;
            }
            movement.MovementDateTime = newTrip.MovementDate.Add(newTrip.MovementTime.TimeOfDay);
            movement.MovementDateType = newTrip.MovementDateType;

            if (movement.Driver) {
                var carpool = new Carpool();
                carpool.Status = CarpoolStatus.New;
                carpool.MaxKm = newTrip.MaxKm;
                carpool.MaxSeats = newTrip.MaxSeats;
                carpool.MaxLuggage = newTrip.MaxLuggage;
                carpool.Arrival = movement.MovementDateTime;
                db.Entry(carpool).State = EntityState.Added;
                carpool.Movements = new List<Movement>();
                carpool.Movements.Add(movement);
            }

            db.SaveChanges();
            db.Dispose();

            if (newTrip.FlightNumber != "") {
                db = new AirportCarpoolDbContext();
                Flight flight = new Flight {
                    FlightNumber = newTrip.FlightNumber,
                    ArrDep = newTrip.ArrDep,
                    Arrival = newTrip.Arrival,
                    Departure = newTrip.Departure,
                    Date = newTrip.MovementDate,
                    MovementId = movement.MovementId
                };
                db.Entry(flight).State = EntityState.Added;
                db.SaveChanges();
                db.Dispose();
            }

            

            return movement;
        }