Example #1
0
        public async Task <APIResult <List <DAL.trip_book> > > Booking(int trip_id, int?seat = 1, bool?all_shoots = true)
        {
            var u = Requests.APIRequest.User(HttpContext.Current.Request);

            if (trip_id == 0)
            {
                return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserValidationField, "'trip_id' is required!"));
            }

            List <DAL.trip_book> lst = new List <DAL.trip_book>();

            using (var ctx = new DAL.MainEntities())
            {
                var selectedTrip = ctx.trip_share_details.Include("trip_share").FirstOrDefault(a => a.id == trip_id);
                if (selectedTrip.trip_share.driver_id == u.Entity.id)
                {
                    return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserValidationField, "You cannot booking your shared trip!"));
                }
                //var cTime = DateTime.Now.AddMinutes(-30) ; //&& a.start_at_date >= cTime
                var triplist = ctx.trip_share_details.Include("trip_share").Where(a => a.trip_share_id == selectedTrip.trip_share_id).ToList();
                if (triplist.Count == 0)
                {
                    return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserValidationField, "This trip not available for booking now!"));
                }
                foreach (var trip in triplist)
                {
                    //var trip=ctx.trip_share_details.Include("trip_share").Where(a=>a.id==trip_id).FirstOrDefault();

                    if (trip == null)
                    {
                        return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserNotFound, "This trip not found!"));
                    }

                    if (trip.booked_seats == null)
                    {
                        trip.booked_seats = 0;
                    }

                    if (trip.seats == trip.booked_seats)
                    {
                        return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserNotAcceptable, "There are no available seat in this trip!"));
                    }

                    if (trip.seats - trip.booked_seats < seat)
                    {
                        return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.UserNotAcceptable, "There are only " + (trip.seats - trip.booked_seats) + " available seat/s in this trip"));
                    }


                    //var requests=ctx.trip_request_details.Include("trip_request").Where(a=>)


                    var req = new DAL.trip_request()
                    {
                        created_at    = DateTime.Now,
                        created_by    = u.Entity.id,
                        end_at_date   = null,
                        rider_id      = u.Entity.id,
                        seats         = seat,
                        start_at_date = trip.start_at_date,
                        trip_type_id  = trip.trip_share.trip_type_id,
                    };

                    var req_details = new DAL.trip_request_details()
                    {
                        from_lat       = trip.from_lat,
                        from_lng       = trip.from_lng,
                        from_plc       = trip.from_plc,
                        gender_id      = trip.gender_id,
                        is_active      = true,
                        seats          = seat,
                        seat_cost_from = trip.seat_cost,
                        seat_cost_to   = trip.seat_cost,
                        start_at_date  = trip.start_at_date,
                        start_at_time  = trip.start_at_time,
                        to_lat         = trip.to_lat,
                        to_lng         = trip.to_lng,
                        to_plc         = trip.to_plc,
                        booked         = true
                    };

                    req.trip_request_details.Add(req_details);

                    ctx.trip_request.Add(req);

                    if (ctx.SaveChanges() > 0)
                    {
                        for (int i = 0; i < seat; i++)
                        {
                            var itm = new DAL.trip_book()
                            {
                                trip_request_details_id = req_details.id,
                                booked_at             = DateTime.Now,
                                seats                 = 1,
                                trip_share_details_id = trip_id,
                                trip_token            = Guid.NewGuid(),
                            };

                            trip.booked_seats++;
                            ctx.Entry(trip).State = System.Data.Entity.EntityState.Modified;


                            ctx.trip_book.Add(itm);
                            lst.Add(itm);
                        }

                        ctx.SaveChanges();
                    }
                    else
                    {
                        return(APIResult <List <DAL.trip_book> > .Error(ResponseCode.BackendDatabase, "Error while save trip_request"));
                    }



                    if (all_shoots != true)
                    {
                        return(APIResult <List <DAL.trip_book> > .Success(lst));
                    }
                }
                var driverDeviceID = ctx.sec_sessions.Where(a => a.user_id == selectedTrip.trip_share.driver_id && a.device_id != null).Select(a => a.device_id).Distinct().ToArray();
                //var ddd = ctx.sec_sessions.Where(a => a.user_id == selectedTrip.trip_share.driver_id).ToArray();
                // Logger.log(JsonConvert.SerializeObject( ddd));
                await FirebaseNotifications.Send(driverDeviceID, "Seats Booking", "Someone has booked your shared seats.", new { type = 1, screen = "trip_share", id = selectedTrip.id, sender = ctx.vwProfile.FirstOrDefault(a => a.id == u.Entity.id).firebase_uid });

                //FirebaseNotifications.Send(new string[] { "rP2tRzF3uKNfYrZu9YypmohBKJX2" }, "Seats Booking", "Someone has booked your shared seats.", new { type = 1, screen = "trip_share", id = 2272, sender = "PF3nbGndlZV99YyPI1J4NOs6XVE2" });
                return(APIResult <List <DAL.trip_book> > .Success(lst));
            }
        }
Example #2
0
        private bool BookIfAvailable(DAL.trip_share_details details, DAL.trip_share trip)
        {
            using (var ctx = new DAL.MainEntities())
            {
                int searchReduis = int.Parse(Settings.Get("search_redius", "500"));

                var current_trips = ctx.trip_request_details.Include("trip_request").AsQueryable();


                //current_trips = current_trips.Where(a => a.from_plc.Contains(details.from_plc));

                //current_trips = current_trips.Where(a => (a.from_plc + ",").Contains(request.from.city + ","));


                //current_trips = current_trips.Where(a => a.from_plc.Contains(request.from.street + ","));

                current_trips = current_trips.Where(a => a.from_lat.Value + searchReduis <details.from_lat && a.from_lat.Value - searchReduis> details.from_lat);

                current_trips = current_trips.Where(a => a.from_lng.Value + searchReduis <details.from_lng && a.from_lng.Value - searchReduis> details.from_lng);



                //current_trips = current_trips.Where(a => a.to_plc.Contains(details.to_plc));



                //current_trips = current_trips.Where(a => a.to_plc.Contains(request.to.street + ","));

                current_trips = current_trips.Where(a => a.to_lat.Value + searchReduis <details.to_lat && a.to_lat.Value - searchReduis> details.to_lat);

                current_trips = current_trips.Where(a => a.to_lng.Value + searchReduis <details.to_lng && a.to_lng.Value - searchReduis> details.to_lng);



                if (details.gender_id > 0)
                {
                    current_trips = current_trips.Where(a => a.gender_id == details.gender_id && a.gender_id == 0);
                }


                current_trips = current_trips.Where(a => a.trip_request.trip_type_id == trip.trip_type_id);

                var sTime = details.start_at_date.Value.AddMinutes(120);
                var rDate = details.start_at_date.Value.Date;
                var rTime = details.start_at_date.Value.AddMinutes(-30);


                current_trips = current_trips.Where(a => a.start_at_date <sTime && a.start_at_date> rTime);

                current_trips = current_trips.Where(a => ((a.seat_cost_from > 0 && a.seat_cost_from <= details.seat_cost) || a.seat_cost_from == 0) && ((a.seat_cost_to > 0 && a.seat_cost_to >= details.seat_cost) || a.seat_cost_to == 0));
                current_trips = current_trips.Where(a => a.seats <= details.seats);
                current_trips = current_trips.Where(a => a.is_active == true);
                current_trips = current_trips.Where(a => a.booked == false || a.booked == null);

                if (details.booked_seats == null)
                {
                    details.booked_seats = 0;
                }
                foreach (var trp in current_trips)
                {
                    if (details.booked_seats >= details.seats)
                    {
                        break;
                    }
                    if (details.booked_seats + trp.seats >= details.seats)
                    {
                        continue;
                    }

                    for (var x = 0; x < trp.seats; x++)
                    {
                        var itm = new DAL.trip_book()
                        {
                            trip_request_details_id = trp.id,
                            booked_at             = DateTime.Now,
                            seats                 = 1,
                            trip_share_details_id = details.id,
                            trip_token            = Guid.NewGuid(),
                        };

                        ctx.trip_book.Add(itm);
                    }
                    details.booked_seats++;
                    trp.booked           = true;
                    ctx.Entry(trp).State = System.Data.Entity.EntityState.Modified;
                    //ctx.Entry(details).State = System.Data.Entity.EntityState.Modified;
                }
                //ctx.Entry(trip)
                ctx.SaveChanges();

                return(true);
            }
        }