public RideViewModel GetEmptyVM() { var dbModel = GetEntity(); var dbCars = dbContext.Set <Carpool>().AsNoTracking().ToList(); var selectCars = dbCars.Select(x => { return(new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); }).ToList(); var dbEmployees = dbContext.Set <Employee>().AsNoTracking().ToList(); var selectEmployees = dbEmployees.Select(x => { return(new SelectListItem { Text = x.EmployeeName, Value = x.Id.ToString() }); }).ToList(); return(MapRide(dbModel, selectCars, selectEmployees)); }
public virtual List <TModel> Get(TSearch search) { var list = _context.Set <TDatabase>().ToList(); return(_mapper.Map <List <TModel> >(list)); }
public void AddTrip(NewPostingViewModel viewModel, User user) { using (Context) { Location to = new Location(); to.StreetAddress = viewModel.StreetAddressDestination; to.City = viewModel.CityDestination; to.State = viewModel.StateDestination; to.ZipCode = viewModel.ZipDestination; Location from = new Location(); from.StreetAddress = viewModel.StreetAddressOrigin; from.City = viewModel.CityOrigin; from.State = viewModel.StateOrigin; from.ZipCode = viewModel.ZipOrigin; Trip trip = new Trip(); trip.To = to; trip.From = from; trip.TravelDateTime = viewModel.DateTime; trip.Type = viewModel.TripType; TripInformation tripInformation = new TripInformation(); tripInformation.Capacity = viewModel.TripType == TripType.LookingForPassengers ? 3 : 1; trip.TripInformation = tripInformation; using (CarpoolContext context = new CarpoolContext()) { user = context.Users.Single(x => x.UserId == user.UserId); trip.TripInformation.Owner = user; context.Trips.Add(trip); AddressBook addressBook = context.AddressBooks.SingleOrDefault(x => x.UserId == user.UserId); bool isNewAddressBook = addressBook == null; if (viewModel.AddOriginToAddressBook) { if (addressBook == null) { addressBook = new AddressBook() { User = user, UserId = user.UserId, Locations = new List <Location>() }; } addressBook.Locations.Add(from); } if (viewModel.AddDestinationToAddressBook) { if (addressBook == null) { addressBook = new AddressBook() { User = user, UserId = user.UserId, Locations = new List <Location>() }; } addressBook.Locations.Add(to); } if (viewModel.AddOriginToAddressBook || viewModel.AddDestinationToAddressBook) { if (isNewAddressBook) { context.AddressBooks.Add(addressBook); } else { context.Set <AddressBook>().Attach(addressBook); context.Entry(addressBook).State = EntityState.Modified; } } context.SaveChanges(); } } }