public void Save(TripEntity trip)
 {
     if (trip.Id == 0)
     {
         Add(trip);
     }
     else
     {
         Update(trip);
     }
 }
        private void Add(TripEntity trip)
        {
            var parameters = new[]
            {
                new SqlParameter(SpParams.SaveTrip.UserId, trip.Driver.Id) { Direction = ParameterDirection.Input, DbType = DbType.Int32 },
                new SqlParameter(SpParams.SaveTrip.Seats, trip.Seats) { Direction = ParameterDirection.Input, DbType = DbType.Int32},
                new SqlParameter(SpParams.SaveTrip.Price, trip.Price) { Direction = ParameterDirection.Input, DbType = DbType.Decimal },
                new SqlParameter(SpParams.SaveTrip.StaticMapUrl, trip.StaticMapUrl) { Direction = ParameterDirection.Input, DbType = DbType.String },
                new SqlParameter(SpParams.SaveTrip.StartDateTime, trip.StartDateTime) { Direction = ParameterDirection.Input, DbType = DbType.DateTime },

                new SqlParameter(SpParams.SaveTrip.Manufacturer, trip.Vehicle.Manufacturer) { Direction = ParameterDirection.Input, DbType = DbType.String },
                new SqlParameter(SpParams.SaveTrip.Model, trip.Vehicle.Model) { Direction = ParameterDirection.Input, DbType = DbType.String},
                new SqlParameter(SpParams.SaveTrip.Color, trip.Vehicle.Color) { Direction = ParameterDirection.Input, DbType = DbType.String },
                new SqlParameter(SpParams.SaveTrip.Year, trip.Vehicle.Year) { Direction = ParameterDirection.Input, DbType = DbType.Int32 },
            };

            trip.Id = ExecuteScalar(SpNames.AddTrip, parameters);

            this.AddMapPoints(trip);
        }
        private TripEntity PopulateTripEntity(SqlDataReader dataReader)
        {
            var tripId = dataReader["Id"].FromDb<int>();

            var driver = new UserEntity()
            {
                Id = dataReader["UserId"].FromDb<int>(),
                UserName = dataReader["UserName"].FromDb<string>(),
                Email = dataReader["UserEmail"].FromDb<string>(),
            };

            var vehicle = new VehicleEntity()
            {
                Id = dataReader["VehicleId"].FromDb<int>(),
                Manufacturer = dataReader["VehicleManufacturer"].FromDb<string>(),
                Model = dataReader["VehicleModel"].FromDb<string>(),
                Color = dataReader["VehicleColor"].FromDb<string>(),
                Year = dataReader["VehicleYear"].FromDb<int>()
            };

            var mapPoints = this.GetMapPoints(tripId);

            var trip = new TripEntity()
            {
                Id = tripId,
                Vehicle = vehicle,
                Driver = driver,
                Seats = dataReader["Seats"].FromDb<int>(),
                SeatsTaken = dataReader["SeatsTaken"].FromDb<int>(),
                Price = dataReader["Price"].FromDb<decimal>(),
                StaticMapUrl = dataReader["StaticMapUrl"].FromDb<string>(),
                StartDateTime = dataReader["StartDateTime"].FromDb<DateTime>(),
                Origin = mapPoints.FirstOrDefault(),
                Destination = mapPoints.LastOrDefault(),
                WayPoints = mapPoints.Count > 2 ? mapPoints.GetRange(1, mapPoints.Count - 2) : new List<MapPoint>()
            };

            return trip;
        }
        private void Add(TripEntity trip)
        {
            trip.Id = GenerateUniqueId();

            _trips.Add(trip);
        }
        private void AddMapPoints(TripEntity trip)
        {
            int sequence = 0;
            this.AddMapPoint(trip.Id, trip.Origin, sequence++);

            foreach (var point in trip.WayPoints)
            {
                this.AddMapPoint(trip.Id, point, sequence++);
            }

            this.AddMapPoint(trip.Id, trip.Destination, sequence);
        }
 private void SendNotificationToTripPassenger(TripEntity trip, UserEntity passenger)
 {
     _emailService.Send(new EmailMessage()
     {
         Destination = passenger.UserName,
         Subject = "New trip",
         Message =
             string.Format("You was successfully assigned to trip from {0} to {1} ({2}) with Driver: {3}.\n" +
                           " Contact information:\n {4}.\n Price: {5}.\n Car:\n {6} ",
                 trip.Origin.FormattedLongAddress,
                 trip.Destination.FormattedLongAddress,
                 trip.StartDateTime,
                 trip.Driver.Email,
                 trip.Driver.Profile,
                 trip.Price,
                 trip.Vehicle)
     });
 }
        private void Update(TripEntity trip)
        {
            var indexToUpdate = _trips.FindIndex(x => x.Id == trip.Id);

            _trips[indexToUpdate] = trip;
        }
 private void SendNotificationToTripDriver(TripEntity trip, UserEntity passenger)
 {
     _emailService.Send(new EmailMessage()
     {
         Destination = trip.Driver.UserName,
         Subject = "New passenger",
         Message = string.Format("Your trip from {0} to {1} ({2}) has new passenger: {3}. Contact information:\n {4}.\nPrice: {5}.\n Your car:\n {6}",
        trip.Origin.FormattedLongAddress,
        trip.Destination.FormattedLongAddress,
        trip.StartDateTime,
        passenger.Email,
        passenger.Profile,
        trip.Price,
        trip.Vehicle)
     });
 }
 private void PopulateTripMapPoints(TripEntity trip)
 {
     var mapPoints = trip.MapPoints.OrderBy(mp => mp.Sequence).ToList();
     trip.Origin = mapPoints.FirstOrDefault();
     trip.Destination = mapPoints.LastOrDefault();
     trip.WayPoints = mapPoints.Count > 2
         ? mapPoints.Skip(1).Take(mapPoints.Count - 2).ToList()
         : new List<MapPoint>();
 }
 private void PopulateUserProfile(TripEntity trip)
 {
     trip.Driver.Profile = trip.Driver.UserProfiles.FirstOrDefault();
 }
        private void Update(TripEntity trip)
        {
            var tripToUpdate = this.GetById(trip.Id);

            if (tripToUpdate != null)
            {
                tripToUpdate.DriverId = trip.DriverId;
                tripToUpdate.Price = trip.Price;
                tripToUpdate.Seats = trip.Seats;
                tripToUpdate.StaticMapUrl = trip.StaticMapUrl;
                tripToUpdate.StartDateTime = trip.StartDateTime;

                if (tripToUpdate.Vehicle != null)
                {
                    tripToUpdate.Vehicle.Color = trip.Vehicle.Color;
                    tripToUpdate.Vehicle.Manufacturer = trip.Vehicle.Manufacturer;
                    tripToUpdate.Vehicle.Model = trip.Vehicle.Model;
                    tripToUpdate.Vehicle.Year = trip.Vehicle.Year;
                }

                CatchMeContext.MapPoints.RemoveRange(tripToUpdate.MapPoints);

                CatchMeContext.SaveChanges();

                AddMapPoints(trip);
            }
        }
        private void Add(TripEntity trip)
        {
            CatchMeContext.Vehicles.Add(trip.Vehicle);
            CatchMeContext.Trips.Add(trip);

            CatchMeContext.SaveChanges();

            AddMapPoints(trip);
        }