Exemplo n.º 1
0
 public TripController()
 {
     dbContext = new TripContext();
     if (!dbContext.Trips.Any())
     {
         dbContext.Trips.AddRange(Trips);
         dbContext.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public IActionResult Post([FromBody] Trip values)
        {
            // we are specifying Trips here because we don't
            // need the db _context to automatically assume it knows
            // which object. If we had multiple objects, we could leave it
            // as something like _context.Add(values);

            // if no workie
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // if workie
            _context.Trips.Add(values);
            _context.SaveChanges();

            return(Ok());
        }
Exemplo n.º 3
0
        public static void DeleteVehicle(int Id)
        {
            using (var db = new TripContext())
            {
                List <Vehicle> vehicles = new List <Vehicle>();

                var collection = db.Vehicles.
                                 Where(v => v.VehicleId == Id);

                foreach (var item in collection)
                {
                    vehicles.Add(item);
                }

                db.Vehicles.Remove(vehicles.ElementAt(0));

                db.SaveChanges();
            }
        }
        public IActionResult Delete(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var myTrip = _context.Trips.Find(id);

            if (myTrip == null)
            {
                return(NotFound());
            }

            _context.Trips.Remove(myTrip);
            _context.SaveChanges();

            return(NoContent());
        }
Exemplo n.º 5
0
        public UnitOfWorkResult <DTO.Trip> Execute(DTO.TripForCreation input)
        {
            if (input == null)
            {
                return(new UnitOfWorkResult <DTO.Trip>(null, UnitOfWorkStatus.Invalid));
            }

            if (_ownerId == null)
            {
                // cannot create a trip when there's no owner id
                return(new UnitOfWorkResult <DTO.Trip>(null, UnitOfWorkStatus.Forbidden));
            }

            // map to entity
            var tripEntity = Mapper.Map <DTO.TripForCreation, Repository.Entities.Trip>(input);

            // create guid
            var id = Guid.NewGuid();

            tripEntity.Id      = id;
            tripEntity.OwnerId = _ownerId;

            // save the array of bytes on disk
            // file name (limited/assumes JPG)
            var    fileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem("images");
            string fileName   = Guid.NewGuid().ToString() + ".jpg";

            // write and auto-close
            File.WriteAllBytes(fileSystem.Root + "/" + fileName, input.MainPictureBytes);

            // fill out URI
            tripEntity.MainPictureUri = "images/" + fileName;

            _tripRepository.InsertTrip(tripEntity);

            // commit
            _ctx.SaveChanges();

            // return a dto
            var dto = Mapper.Map <Repository.Entities.Trip, DTO.Trip>(tripEntity);

            return(new UnitOfWorkResult <DTO.Trip>(dto, UnitOfWorkStatus.Ok));
        }
Exemplo n.º 6
0
        public static void DeleteEmployee(int EmployeeId_)
        {
            using (var db = new TripContext())
            {
                List <Employee> employees = new List <Employee>();

                var result = db.Employees.Where(
                    e => e.EmployeeId == EmployeeId_);

                foreach (var item in result)
                {
                    employees.Add(item);
                }

                db.Employees.Remove(employees.ElementAt(0));

                db.SaveChanges();
            }
        }
Exemplo n.º 7
0
        private void YesBt_Click(object sender, RoutedEventArgs e)
        {
            using (TripContext db = new TripContext())
            {
                Trip trip = db.Trips.Where(t => t.Id == id).FirstOrDefault();

                if (trip != null)
                {
                    if (b)
                    {
                        trip.State = "Завершена";
                    }
                    else
                    {
                        trip.State = "Отменена";
                    }
                }
                db.SaveChanges();

                using (UserContext udb = new UserContext())
                {
                    User user = udb.Users.Where(u => u.Name == trip.Name).FirstOrDefault();
                    if (user != null)
                    {
                        user.state = "Свободен";
                    }
                    udb.SaveChanges();
                }

                using (CarContext cdb = new CarContext())
                {
                    Car car = cdb.Cars.Where(c => c.CarName == trip.CarName).FirstOrDefault();
                    if (car != null)
                    {
                        car.State = "Свободна";
                    }
                    cdb.SaveChanges();
                }
            }
            em.UpdateTripsList();
            this.Close();
        }
Exemplo n.º 8
0
        public UnitOfWorkResult <DTO.Trip> Execute(JsonPatchDocument <DTO.Trip> input)
        {
            if (input == null)
            {
                return(new UnitOfWorkResult <DTO.Trip>(null, UnitOfWorkStatus.Invalid));
            }

            // get the trip
            var tripEntity = _tripRepository.GetTrip(_tripId);

            if (tripEntity == null)
            {
                return(new UnitOfWorkResult <DTO.Trip>(null, UnitOfWorkStatus.NotFound));
            }

            // we're updating.  Only the user that created the trip can update it.
            if (string.IsNullOrWhiteSpace(tripEntity.OwnerId) || tripEntity.OwnerId != _ownerId)
            {
                return(new UnitOfWorkResult <DTO.Trip>(null, UnitOfWorkStatus.Forbidden));
            }

            // convert entity to DTO
            var dtoForPatch = Mapper.Map <TripGallery.Repository.Entities.Trip,
                                          DTO.Trip>(tripEntity);

            // apply the patchdoc to the DTO
            input.ApplyTo(dtoForPatch);

            // update entity
            tripEntity = Mapper.Map <DTO.Trip, TripGallery.Repository.Entities.Trip>
                             (dtoForPatch);

            _tripRepository.UpdateTrip(tripEntity);

            // save
            _ctx.SaveChanges();

            // return DTO
            var dto = Mapper.Map <Repository.Entities.Trip, DTO.Trip>(tripEntity);

            return(new UnitOfWorkResult <DTO.Trip>(dto, UnitOfWorkStatus.Ok));
        }
Exemplo n.º 9
0
        //could be served as a service, and then we could use async
        internal static User EnsureUser(TripContext db, ClaimsPrincipal user)
        {
            if (user == null)
            {
                return(null);
            }

            var u = db.Users.FirstOrDefault(x => x.Name == user.Identity.Name);

            if (u == null)
            {
                //register new user
                u = new User {
                    Name = user.Identity.Name
                };
                db.Users.Add(u);
                db.SaveChanges();
            }
            return(u);
        }
        public ActionResult AddTrip(Trip t)
        {
            TripContext ts = new TripContext();

            if (t.carAvailable == false)
            {
                t.vacant_seats = -1;
            }
            AspNetUsersContext us  = new AspNetUsersContext();
            AspNetUsers        asp = us.AspNetUsers.Single(a => a.Email == User.Identity.Name);

            t.created_by = asp.Id;
            ts.Trips.Add(t);
            ts.SaveChanges();
            TripGroupContext t_gc = new TripGroupContext();
            TripGroup        tg   = new TripGroup();

            tg.Id        = t.id;
            tg.People    = User.Identity.Name;
            tg.TripAdmin = t.carAvailable;
            t_gc.TripGroups.Add(tg);
            t_gc.SaveChanges();

            //if similar trip exists send mail to those people
            List <Trip> similarTrips = new List <Trip>();

            similarTrips = ts.Trips.Where(s => s.source == t.source && s.destination == t.destination).ToList();
            foreach (var s in similarTrips)
            {
                AspNetUsers aspnetusers = us.AspNetUsers.Single(u => u.Id == s.created_by);
                if (aspnetusers.Email != User.Identity.Name)
                {
                    TripController tp = new TripController();
                    tp.SMTPSendEmail(aspnetusers.Email,
                                     User.Identity.Name + " has created a new trip similar to yours",
                                     "A trip similar to yours has been created on CarpoolManagementSystem");
                }
            }

            return(RedirectToAction("TripDetails/" + t.id, "Trip"));
        }
Exemplo n.º 11
0
        public ActionResult Create([Bind(Include = "SequenceID,Name,RegionID")] SequenceDisp sDisp)
        {
            if (ModelState.IsValid)
            {
                Sequence sequence = new Sequence();
                sequence.CopyData(sDisp);
                db.Sequences.Add(sequence);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            sDisp.Region = (from r in db.Regions where r.RegionID == sDisp.RegionID select r).First();

            ViewBag.RegionID = new SelectList(db.Regions, "RegionID", "Name", sDisp.RegionID);
            return(View(sDisp));
        }
        public string JoinTrip(string id)
        {
            if (User.Identity.IsAuthenticated)
            {
                try
                {
                    TripGroupContext g = new TripGroupContext();
                    var tg             = g.TripGroups.Where(b => b.Id.ToString() == id).ToArray();
                    foreach (var d in tg)
                    {
                        if (d.People == User.Identity.Name)
                        {
                            return("2");
                        }
                    }

                    TripGroup t_g = new TripGroup();
                    t_g.Id        = Int32.Parse(id);
                    t_g.People    = User.Identity.Name;
                    t_g.TripAdmin = false;
                    g.TripGroups.Add(t_g);
                    g.SaveChanges();
                    TripContext t_s = new TripContext();
                    Trip        t   = t_s.Trips.Single(d => d.id.ToString() == id);
                    t.vacant_seats = t.vacant_seats - 1;
                    t_s.SaveChanges();
                    return("3");
                }
                catch
                {
                    return("3");
                }
            }

            else
            {
                return("1");
            }
        }
Exemplo n.º 13
0
        public async Task <JsonResult> Post(TripViewModel trip, StopViewModel stop)
        {
            var aTrip = Mapper.Map <Trip>(trip);
            var aStop = Mapper.Map <Stop>(stop);

            var coord = await cs.Lookup(aStop.Name);

            if (coord.Success)
            {
                aStop.Longitude = coord.Longitude;
                aStop.Latitude  = coord.Latitude;
            }

            aTrip.Stops.Add(aStop);
            if (ModelState.IsValid)
            {
                db.Trips.Update(aTrip);
                db.SaveChanges();
            }

            var result = Mapper.Map <TripViewModel>(aTrip);

            return(Json(result));
        }
Exemplo n.º 14
0
 public bool Save()
 {
     return(_context.SaveChanges() >= 0);
 }
Exemplo n.º 15
0
 public void Save()
 {
     db.SaveChanges();
 }
Exemplo n.º 16
0
 public void AddTrip(Trip trip)
 {
     Context.Trips.Add(trip);
     Context.SaveChanges();
 }
Exemplo n.º 17
0
        public void InsertSeedData()
        {
            // If database does not contain any data, add some
            if (!db.Trips.Any())
            {
                var aTrip = new Trip()
                {
                    Name        = "Coast to Coast",
                    DateCreated = DateTime.Now,
                    UserName    = "",
                    Stops       = new List <Stop>()
                    {
                        new Stop()
                        {
                            Name = "Philadelphia, PA", ArrivalDate = new DateTime(2015, 10, 20), Latitude = 39.94907, Longitude = -75.16059, Order = 0
                        },
                        new Stop()
                        {
                            Name = "New York, NY", ArrivalDate = new DateTime(2015, 10, 23), Latitude = 40.712784, Longitude = -74.005941, Order = 1
                        },
                        new Stop()
                        {
                            Name = "Chicago, IL", ArrivalDate = new DateTime(2015, 10, 30), Latitude = 41.878114, Longitude = -87.629798, Order = 2
                        },
                        new Stop()
                        {
                            Name = "Seattle, WA", ArrivalDate = new DateTime(2015, 11, 1), Latitude = 47.606209, Longitude = -122.332071, Order = 3
                        },
                        new Stop()
                        {
                            Name = "Philadelphia, PA", ArrivalDate = new DateTime(2015, 11, 20), Latitude = 39.94907, Longitude = -75.16059, Order = 4
                        }
                    }
                };
                db.Trips.Add(aTrip);
                db.Stops.AddRange(aTrip.Stops);

                var bigTrip = new Trip()
                {
                    Name        = "Around the World",
                    DateCreated = DateTime.Now,
                    UserName    = "",
                    Stops       = new List <Stop>()
                    {
                        new Stop()
                        {
                            Order = 0, Latitude = 39.94907, Longitude = -75.16059, Name = "Philadelphia, PA", ArrivalDate = DateTime.Parse("June 1, 2014")
                        },
                        new Stop()
                        {
                            Order = 1, Latitude = 48.856614, Longitude = 2.352222, Name = "Paris, France", ArrivalDate = DateTime.Parse("Jun 4, 2014")
                        },
                        new Stop()
                        {
                            Order = 2, Latitude = 50.850000, Longitude = 4.350000, Name = "Brussels, Belgium", ArrivalDate = DateTime.Parse("Jun 25, 2014")
                        },
                        new Stop()
                        {
                            Order = 3, Latitude = 51.209348, Longitude = 3.224700, Name = "Bruges, Belgium", ArrivalDate = DateTime.Parse("Jun 28, 2014")
                        },
                        new Stop()
                        {
                            Order = 4, Latitude = 48.856614, Longitude = 2.352222, Name = "Paris, France", ArrivalDate = DateTime.Parse("Jun 30, 2014")
                        },
                        new Stop()
                        {
                            Order = 5, Latitude = 51.508515, Longitude = -0.125487, Name = "London, UK", ArrivalDate = DateTime.Parse("Jul 8, 2014")
                        },
                        new Stop()
                        {
                            Order = 6, Latitude = 51.454513, Longitude = -2.587910, Name = "Bristol, UK", ArrivalDate = DateTime.Parse("Jul 24, 2014")
                        },
                        new Stop()
                        {
                            Order = 7, Latitude = 52.954783, Longitude = -1.158109, Name = "Nottingham, UK", ArrivalDate = DateTime.Parse("Jul 31, 2014")
                        },
                        new Stop()
                        {
                            Order = 8, Latitude = 55.953252, Longitude = -3.188267, Name = "Edinburgh, UK", ArrivalDate = DateTime.Parse("Aug 5, 2014")
                        },
                        new Stop()
                        {
                            Order = 9, Latitude = 55.864237, Longitude = -4.251806, Name = "Glasgow, UK", ArrivalDate = DateTime.Parse("Aug 6, 2014")
                        },
                        new Stop()
                        {
                            Order = 10, Latitude = 55.953252, Longitude = -3.188267, Name = "Edinburgh, UK", ArrivalDate = DateTime.Parse("Aug 8, 2014")
                        },
                        new Stop()
                        {
                            Order = 11, Latitude = 51.508515, Longitude = -0.125487, Name = "London, UK", ArrivalDate = DateTime.Parse("Aug 10, 2014")
                        },
                        new Stop()
                        {
                            Order = 12, Latitude = 52.370216, Longitude = 4.895168, Name = "Amsterdam, Netherlands", ArrivalDate = DateTime.Parse("Aug 14, 2014")
                        },
                        new Stop()
                        {
                            Order = 13, Latitude = 48.583148, Longitude = 7.747882, Name = "Strasbourg, France", ArrivalDate = DateTime.Parse("Aug 17, 2014")
                        },
                        new Stop()
                        {
                            Order = 14, Latitude = 46.519962, Longitude = 6.633597, Name = "Lausanne, Switzerland", ArrivalDate = DateTime.Parse("Aug 19, 2014")
                        },
                        new Stop()
                        {
                            Order = 15, Latitude = 46.021073, Longitude = 7.747937, Name = "Zermatt, Switzerland", ArrivalDate = DateTime.Parse("Aug 27, 2014")
                        },
                        new Stop()
                        {
                            Order = 16, Latitude = 46.519962, Longitude = 6.633597, Name = "Lausanne, Switzerland", ArrivalDate = DateTime.Parse("Aug 29, 2014")
                        },
                        new Stop()
                        {
                            Order = 17, Latitude = 54.597285, Longitude = -5.930120, Name = "Belfast, Northern Ireland", ArrivalDate = DateTime.Parse("Sep 7, 2014")
                        },
                        new Stop()
                        {
                            Order = 18, Latitude = 53.349805, Longitude = -6.260310, Name = "Dublin, Ireland", ArrivalDate = DateTime.Parse("Sep 9, 2014")
                        },
                        new Stop()
                        {
                            Order = 19, Latitude = 47.368650, Longitude = 8.539183, Name = "Zurich, Switzerland", ArrivalDate = DateTime.Parse("Sep 16, 2014")
                        },
                        new Stop()
                        {
                            Order = 20, Latitude = 48.135125, Longitude = 11.581981, Name = "Munich, Germany", ArrivalDate = DateTime.Parse("Sep 19, 2014")
                        },
                        new Stop()
                        {
                            Order = 21, Latitude = 51.050409, Longitude = 13.737262, Name = "Dresden, Germany", ArrivalDate = DateTime.Parse("Oct 1, 2014")
                        },
                        new Stop()
                        {
                            Order = 22, Latitude = 50.075538, Longitude = 14.437800, Name = "Prague, Czech Republic", ArrivalDate = DateTime.Parse("Oct 4, 2014")
                        },
                        new Stop()
                        {
                            Order = 23, Latitude = 42.697708, Longitude = 23.321868, Name = "Sofia, Bulgaria", ArrivalDate = DateTime.Parse("Oct 16, 2014")
                        },
                        new Stop()
                        {
                            Order = 24, Latitude = 41.005270, Longitude = 28.976960, Name = "Istanbul, Turkey", ArrivalDate = DateTime.Parse("Nov 1, 2014")
                        },
                        new Stop()
                        {
                            Order = 25, Latitude = 45.815011, Longitude = 15.981919, Name = "Zagreb, Croatia", ArrivalDate = DateTime.Parse("Nov 11, 2014")
                        },
                        new Stop()
                        {
                            Order = 26, Latitude = 41.005270, Longitude = 28.976960, Name = "Istanbul, Turkey", ArrivalDate = DateTime.Parse("Nov 15, 2014")
                        },
                        new Stop()
                        {
                            Order = 27, Latitude = 50.850000, Longitude = 4.350000, Name = "Brussels, Belgium", ArrivalDate = DateTime.Parse("Nov 25, 2014")
                        },
                        new Stop()
                        {
                            Order = 28, Latitude = 50.937531, Longitude = 6.960279, Name = "Cologne, Germany", ArrivalDate = DateTime.Parse("Nov 30, 2014")
                        },
                        new Stop()
                        {
                            Order = 29, Latitude = 48.208174, Longitude = 16.373819, Name = "Vienna, Austria", ArrivalDate = DateTime.Parse("Dec 4, 2014")
                        },
                        new Stop()
                        {
                            Order = 30, Latitude = 47.497912, Longitude = 19.040235, Name = "Budapest, Hungary", ArrivalDate = DateTime.Parse("Dec 28,2014")
                        },
                        new Stop()
                        {
                            Order = 31, Latitude = 37.983716, Longitude = 23.729310, Name = "Athens, Greece", ArrivalDate = DateTime.Parse("Jan 2, 2015")
                        },
                        new Stop()
                        {
                            Order = 32, Latitude = 43.771033, Longitude = 11.248001, Name = "Florence, Italy", ArrivalDate = DateTime.Parse("Feb 1, 2015")
                        },
                        new Stop()
                        {
                            Order = 33, Latitude = 41.872389, Longitude = 12.480180, Name = "Rome, Italy", ArrivalDate = DateTime.Parse("Feb 17, 2015")
                        },
                        new Stop()
                        {
                            Order = 34, Latitude = 28.632244, Longitude = 77.220724, Name = "New Delhi, India", ArrivalDate = DateTime.Parse("Mar 4, 2015")
                        },
                        new Stop()
                        {
                            Order = 35, Latitude = 27.700000, Longitude = 85.333333, Name = "Kathmandu, Nepal", ArrivalDate = DateTime.Parse("Mar 10, 2015")
                        },
                        new Stop()
                        {
                            Order = 36, Latitude = 22.1667, Longitude = 113.5500, Name = "Macau", ArrivalDate = DateTime.Parse("Mar 21, 2015")
                        },
                        new Stop()
                        {
                            Order = 37, Latitude = 22.396428, Longitude = 114.109497, Name = "Hong Kong", ArrivalDate = DateTime.Parse("Mar 24, 2015")
                        },
                        new Stop()
                        {
                            Order = 38, Latitude = 39.904030, Longitude = 116.407526, Name = "Beijing, China", ArrivalDate = DateTime.Parse("Apr 19, 2015")
                        },
                        new Stop()
                        {
                            Order = 39, Latitude = 1.352083, Longitude = 103.819836, Name = "Singapore", ArrivalDate = DateTime.Parse("Apr 30, 2015")
                        },
                        new Stop()
                        {
                            Order = 40, Latitude = 3.139003, Longitude = 101.686855, Name = "Kuala Lumpor, Malaysia", ArrivalDate = DateTime.Parse("May 7, 2015")
                        },
                        new Stop()
                        {
                            Order = 41, Latitude = 13.727896, Longitude = 100.524123, Name = "Bangkok, Thailand", ArrivalDate = DateTime.Parse("May 24, 2015")
                        },
                        new Stop()
                        {
                            Order = 42, Latitude = 39.94907, Longitude = -75.16059, Name = "Philadelphia, PA", ArrivalDate = DateTime.Parse("Jun 1, 2015")
                        },
                    }
                };

                db.Trips.Add(bigTrip);
                db.Stops.AddRange(bigTrip.Stops);
                db.SaveChanges();
            }
        }