public OfferringDTO GetById([FromQuery] int id)
        {
            Offerring    Offer    = _repos.GetById(id);
            OfferringDTO OfferDTO = _mapper.Map <Offerring, OfferringDTO>(Offer);

            return(OfferDTO);
        }
        //POST
        public OfferringDTO Create([FromBody] OfferringDTO offerring)
        {
            Offerring    Offer    = _repos.Create(offerring);
            OfferringDTO OfferDTO = _mapper.Map <Offerring, OfferringDTO>(Offer);

            return(OfferDTO);
        }
Beispiel #3
0
        public bool Update(OfferringDTO putOfferring)
        {
            bool flag = true;

            if (IsUpdatable(putOfferring.Id))
            {
                using (CarPoolContext context = new CarPoolContext()) {
                    Offerring offer = context.Offerrings.FirstOrDefault(e => e.Id == putOfferring.Id);
                    offer.Source = new Location()
                    {
                        Lattitude = putOfferring.Source.Lattitude, Longitude = putOfferring.Source.Longitude, LocationName = putOfferring.Source.LocationName
                    };
                    offer.Destination = new Location()
                    {
                        Lattitude = putOfferring.Destination.Lattitude, Longitude = putOfferring.Destination.Longitude, LocationName = putOfferring.Destination.LocationName
                    };
                    offer.CurrentLocation = new Location()
                    {
                        Lattitude = putOfferring.Source.Lattitude, Longitude = putOfferring.Source.Longitude, LocationName = putOfferring.Source.LocationName
                    };
                    offer.Discount              = putOfferring.Discount;
                    offer.StartTime             = putOfferring.StartTime;
                    offer.TotalEarning          = 0;
                    offer.Vechicles.Capacity    = putOfferring.Vehicle.Capacity;
                    offer.Vechicles.NumberPlate = putOfferring.Vehicle.NumberPlate;
                    offer.Vechicles.Type        = (VehicleType)Enum.Parse(typeof(VehicleType), putOfferring.Vehicle.Type.ToString());
                    context.ViaPoints.RemoveRange(context.ViaPoints.Where(e => e.OfferId == offer.Id).ToList());
                    foreach (LocationDTO location in putOfferring.ViaPoints)
                    {
                        context.Add(new ViaPoints()
                        {
                            Location = new Location()
                            {
                                Lattitude = location.Lattitude, Longitude = location.Longitude, LocationName = location.LocationName
                            }, OfferId = offer.Id
                        });
                    }
                    context.SaveChanges();
                }
                flag = true;
            }
            else
            {
                flag = false;
            }

            return(flag);
        }
Beispiel #4
0
        public bool Update(OfferringDTO putOfferring)
        {
            bool flag = true;

            if (IsUpdatable(putOfferring.Id))
            {
                using (CarPoolContext context = new CarPoolContext()) {
                    Offerring offer = context.Offerrings.Where(e => e.Id == putOfferring.Id).Single();
                    offer.Source = new Location()
                    {
                        Lattitude = putOfferring.Source.Lattitude, Longitude = putOfferring.Source.Longitude, LocationName = putOfferring.Source.LocationName
                    };
                    offer.Destination = new Location()
                    {
                        Lattitude = putOfferring.Destination.Lattitude, Longitude = putOfferring.Destination.Longitude, LocationName = putOfferring.Destination.LocationName
                    };
                    offer.CurrentLocation = new Location()
                    {
                        Lattitude = putOfferring.Source.Lattitude, Longitude = putOfferring.Source.Longitude, LocationName = putOfferring.Source.LocationName
                    };
                    offer.Discount     = putOfferring.Discount;
                    offer.StartTime    = putOfferring.StartTime;
                    offer.TotalEarning = 0;
                    context.ViaPoints.RemoveRange(context.ViaPoints.Where(e => e.OfferId == offer.Id).ToList());
                    foreach (LocationDTO location in putOfferring.ViaPoints)
                    {
                        context.Add(new ViaPoints()
                        {
                            Location = new Location()
                            {
                                Lattitude = location.Lattitude, Longitude = location.Longitude, LocationName = location.LocationName
                            }, OfferId = offer.Id
                        });
                    }
                    context.SaveChanges();
                }
                flag = true;
            }
            else
            {
                flag = false;
            }

            return(flag);
        }
Beispiel #5
0
        public bool IsEndPointsWithinReachByOfferId(int offerId, Location source, Location destination)
        {
            bool flag = false;

            using (var context = new CarPoolContext()) {
                List <Location> route = new List <Location> ();
                Offerring       offer = context.Offerrings.FirstOrDefault(e => e.Id == offerId);
                route.Add(offer.Source);
                route.AddRange(context.ViaPoints.Where(e => e.OfferId.Equals(offer.Id)).Select(e => e.Location).ToList());
                route.Add(offer.Destination);
                List <Location> path = new List <Location> ();
                if (route.IndexOf(source) != -1 && route.IndexOf(destination) != -1)
                {
                    flag = true;
                }
            }
            return(flag);
        }
Beispiel #6
0
        public Offerring Create(OfferringDTO postOfferring)
        {
            Offerring offer = new Offerring {
                Active      = true,
                Destination = new Location()
                {
                    Lattitude = postOfferring.Destination.Lattitude, Longitude = postOfferring.Destination.Longitude, LocationName = postOfferring.Destination.LocationName
                },
                Source = new Location()
                {
                    Lattitude = postOfferring.Source.Lattitude, Longitude = postOfferring.Source.Longitude, LocationName = postOfferring.Source.LocationName
                },
                CurrentLocation = new Location()
                {
                    Lattitude = postOfferring.Source.Lattitude, Longitude = postOfferring.Source.Longitude, LocationName = postOfferring.Source.LocationName
                },
                UserId         = postOfferring.UserId,
                SeatsOffered   = postOfferring.MaxOfferSeats,
                SeatsAvailable = postOfferring.MaxOfferSeats,
                Discount       = postOfferring.Discount,
                PricePerKM     = postOfferring.PricePerKM,
                StartTime      = postOfferring.StartTime,
                Vechicles      = new Vechicles()
                {
                    Active = true, Capacity = postOfferring.Vehicle.Capacity, NumberPlate = postOfferring.Vehicle.NumberPlate, Type = (VehicleType)Enum.Parse(typeof(VehicleType), postOfferring.Vehicle.Type.ToString())
                },
            };
            var offerring = _context.Offerrings.Add(offer).Entity;

            _context.SaveChanges();
            foreach (LocationDTO point in postOfferring.ViaPoints)
            {
                _context.ViaPoints.Add(new ViaPoints {
                    Location = new Location()
                    {
                        Lattitude = point.Lattitude, LocationName = point.LocationName, Longitude = point.Longitude
                    }, OfferId = offerring.Id
                });
            }
            _context.SaveChanges();

            return(_context.Offerrings.Where(e => e.Id == offerring.Id).Include(e => e.ViaPoints).Include(e => e.Vechicles).Include(e => e.User).Single());
        }
Beispiel #7
0
        public string HandleNextLocation(int offerId)
        {
            string nextLocation = null;

            using (var context = new CarPoolContext()) {
                Offerring     offer = context.Offerrings.Where(e => e.Id == offerId).Include(e => e.Source).Include(e => e.Destination).Include(e => e.CurrentLocation).Include(e => e.ViaPoints).ThenInclude(e => e.Location).Single();
                List <string> route = new List <string> ();
                route.Add(offer.Source.LocationName);
                route.AddRange(offer.ViaPoints.Select(e => e.Location.LocationName).ToList());
                route.Add(offer.Destination.LocationName);
                int nextLocationIndex = route.IndexOf(offer.CurrentLocation.LocationName) + 1;
                if (nextLocationIndex == route.Count())
                {
                    nextLocation = null;
                }
                else
                {
                    nextLocation = route[nextLocationIndex];
                }
            }
            return(nextLocation);
        }
Beispiel #8
0
        public bool UpdateLocation(int offerId, string reachedLocation)
        {
            bool flag = false;

            using (var context = new CarPoolContext()) {
                Offerring       offer           = context.Offerrings.Where(e => e.Id == offerId).Include(e => e.CurrentLocation).Include(e => e.Source).Include(e => e.Destination).Include(e => e.ViaPoints).ThenInclude(e => e.Location).Single();
                Location        CurrentLocation = offer.CurrentLocation;
                List <Location> route           = new List <Location> ();
                route.Add(offer.Source);
                System.Console.WriteLine(offer.ViaPoints.Select(e => e.Location));
                route.AddRange(offer.ViaPoints.Select(e => e.Location).ToList());
                route.Add(offer.Destination);
                Location tempLocation = route.Find(e => e.LocationName == reachedLocation);
                CurrentLocation.Lattitude    = tempLocation.Lattitude;
                CurrentLocation.LocationName = tempLocation.LocationName;
                CurrentLocation.Longitude    = tempLocation.Longitude;
                List <Booking> AcceptedBooking = context.Bookings.Where(e => e.OfferId == offerId && e.BookingStatus == BookingStatus.ACCEPTED).ToList();
                foreach (Booking booking in AcceptedBooking)
                {
                    booking.BookingStatus = BookingStatus.COMPLETED;
                    booking.Active        = false;
                }
                List <Booking> RejectedBooking = context.Bookings.Where(e => e.OfferId == offerId && e.BookingStatus == BookingStatus.REQUESTED).ToList();
                foreach (Booking booking in RejectedBooking)
                {
                    booking.BookingStatus = BookingStatus.DESTROYED;
                    booking.Active        = false;
                }
                if (CurrentLocation.LocationName == offer.Destination.LocationName)
                {
                    offer.Active = false;
                }
                context.SaveChanges();
                flag = true;
            }
            return(flag);
        }
Beispiel #9
0
        public Offerring GetById(int id)
        {
            Offerring offer = _context.Offerrings.Where(e => e.Id == id).Include(e => e.ViaPoints).Include(e => e.Bookings).Include(e => e.Source).Include(e => e.Destination).Include(e => e.CurrentLocation).Single();

            return(offer);
        }