Example #1
0
        public Trip SaveAndReturnTrip([FromBody] Trip trip)
        {
            ITripManager m           = ObjectContainer.GetTripManager();
            IMapsManager mapsManager = ObjectContainer.GetMapsManager();

            for (var i = 0; i < trip.Locations.Count; i++)
            {
                trip.Locations[i].Position = i;
            }

            if (trip.ID > 0)
            {
                Trip old = m.Get(trip.ID);
                List <List <Location> > oldSections = mapsManager.SplitTripIntoSections(old);

                if (oldSections == null)
                {
                    return(m.SaveAndReturn(GetUser().ID, trip));
                }

                List <List <Location> > sections = mapsManager.SplitTripIntoSections(trip);

                //If a new point is added, it shows up as a new section here. If a transit point is added, it gets added to a section and changes the length
                Location modified = null;
                for (var i = 0; i < oldSections.Count; i++)
                {
                    if (oldSections[i].Count != sections[i].Count)
                    {
                        modified = sections[i][1];
                    }
                }

                if (modified != null)
                {
                    mapsManager.SetSectionAsModified(GetUser(), trip, modified);
                }
            }

            return(m.SaveAndReturn(GetUser().ID, trip));
        }
Example #2
0
        public async Task <Trip> FillBorderPoints(User user, Trip trip, Location editedLocation)
        {
            if (trip.Locations.Count < 3)
            {
                return(trip);
            }

            trip.ArrangePoints();

            ITripManager m = ObjectContainer.GetTripManager();

            trip = m.SaveAndReturn(user.ID, trip);

            //--------------------------------------------

            List <List <Location> > sections = SplitTripIntoSections(trip);

            for (var j = 0; j < sections.Count; j++)
            {
                List <Location> section = sections[j];
                //section[1] is most probably always a border cross and it must always exist
                if (section[1].SectionModified)
                {
                    continue;
                }

                EraseListLocationPoints(section);

                //This for loop should be useless, as at this point there is only one border-cross point in the section
                for (var i = 0; i < section.Count; i++)
                {
                    if (i == 0 || i == section.Count - 1)
                    {
                        continue;
                    }
                    var l  = section.ElementAt(i);
                    var ln = section.ElementAt(i + 1);
                    var lp = section.ElementAt(i - 1);
                    //if (l.ID != editedLocation.ID && ln.ID != editedLocation.ID && lp.ID != editedLocation.ID) continue;
                    //This partially solves the pointless filling, but it still checks for both inbound and outbound crosses, but it's not always necesarry!!!
                    if (l.IsCrossing && lp.DepartureDate.HasValue && lp.DepartureTime.HasValue && ln.ArrivalDate.HasValue && ln.ArrivalTime.HasValue && ln.InboundTravelType.HasValue)
                    {
                        l.CrossedBorder = lp.City.CountryID != ln.City.CountryID;
                        if (l.CrossedBorder)
                        {
                            BorderCrossObject o = await GetBorderCrossTime(lp, ln, section, i);

                            if (o != null)
                            {
                                long transitPointTime = Convert.ToInt64(o.CrossedAt % (60000 * 60 * 24));
                                long transitPointDate = Convert.ToInt64(o.CrossedAt - transitPointTime);
                                l.CrossedAtDate = transitPointDate;
                                l.CrossedAtTime = transitPointTime;
                                i += o.IncrementBy;
                            }
                            else
                            {
                                l.CrossedAtDate = -1;
                                l.CrossedAtTime = -1;
                            }
                        }
                    }
                    else
                    {
                        l.CrossedAt = null;
                    }
                }
            }

            trip.Locations = StichTogetherSections(sections);

            //----------------------------------------

            trip.Locations.OrderBy(x => x.Position);

            return(trip);
        }