Пример #1
0
        public GroupModel PopulateGroupModel(string UserId, Location _Location, string searchString, string Email)
        {
            GroupModel _GroupModel = new GroupModel();

            if (!String.IsNullOrEmpty(searchString))
            {
                _GroupModel._Groups = repository.GetGroupsByLocationWithSearch(_Location.id, searchString).ToList();
                _GroupModel.Title   = "All bunches";
            }
            else
            {
                _GroupModel._Groups = repository.GetGroupsByLocation(_Location.id).ToList();
                _GroupModel.Title   = "All bunches";
            }

            _GroupModel.PrivateBunches = repository.GetPrivateGroupsByUserID(UserId,
                                                                             _Location);

            _GroupModel._Groups.AddRange(_GroupModel.PrivateBunches);

            _GroupModel._NextRideDetails            = new List <NextRideDetails>();
            _GroupModel._NextRideDetailsOneWeekAway = new List <NextRideDetails>();
            _GroupModel.UserLocation = _Location.Name;
            CultureHelper _CultureHelper = new CultureHelper(repository);
            TimeZoneInfo  TZone          = _CultureHelper.GetTimeZoneInfo(_Location.id);
            DateTime      LocalNow       = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TZone);

            _GroupModel._OwnerGroupList = new List <int>();
            _GroupModel.lat             = _Location.Lat;
            _GroupModel.lng             = _Location.Lng;

            foreach (Group item in _GroupModel._Groups.Where(o => o.OneOff == true))
            {
                if (item.RideDate <= LocalNow.AddDays(7) && item.RideDate >= DateTime.Now.AddHours(-2))
                {
                    Ride NextRide = repository.GetOneOffRideByGroupID(item.id);
                    if (NextRide != null)
                    {
                        _GroupModel._NextRideDetails.Add(new NextRideDetails
                        {
                            Date               = NextRide.RideDate,
                            GroupId            = item.id,
                            NumberofKeenRiders = NextRide.Riders.Where(i => i.PercentKeen == "In").Count()
                        });
                    }
                }
            }

            foreach (Group item in _GroupModel._Groups.Where(o => o.OneOff == true))
            {
                if (item.RideDate > LocalNow.AddDays(7))
                {
                    Ride NextRide = repository.GetOneOffRideByGroupID(item.id);
                    if (NextRide != null)
                    {
                        _GroupModel._NextRideDetailsOneWeekAway.Add(new NextRideDetails
                        {
                            Date               = NextRide.RideDate,
                            GroupId            = item.id,
                            NumberofKeenRiders = NextRide.Riders.Where(i => i.PercentKeen == "In").Count()
                        });
                    }
                }
                if (repository.IsGroupCreator(item.id, UserId))
                {
                    _GroupModel._OwnerGroupList.Add(item.id);
                }
            }

            _GroupModel._NextRideDetailsOneWeekAway = _GroupModel._NextRideDetailsOneWeekAway.OrderBy(o => o.Date).ToList();

            foreach (Group item in _GroupModel._Groups.Where(o => o.OneOff == false))
            {
                int RideCount = item.Rides.Count();

                //If Ride count does not equal rides greater than now then there are old ride so
                //call delete rides and populate new from latest ride date, note this should have been done by console app
                if (item.Rides.Where(t => t.RideDate >= LocalNow).Count() != RideCount)
                {
                    repository.DeleteOldRides(item.id, TZone);
                    //If no ride is greater than localtime then just need to set as current date to use.
                    DateTime _NextDateTime = item.Rides.OrderByDescending(g => g.RideDate).Select(h => h.RideDate).FirstOrDefault();
                    if (_NextDateTime == DateTime.MinValue)
                    {
                        _NextDateTime = DateTime.Now;
                    }
                    repository.PopulateRideDatesFromDate(item, _NextDateTime, TZone);
                }

                item.Rides = item.Rides.Where(t => t.RideDate >= LocalNow).ToList();
                Ride NextRide = repository.GetClosestNextRide(item, TZone);

                if (NextRide != null)
                {
                    _GroupModel._NextRideDetails.Add(new NextRideDetails {
                        Date               = NextRide.RideDate,
                        GroupId            = item.id,
                        NumberofKeenRiders = NextRide.Riders.Where(i => i.PercentKeen == "In").Count()
                    });
                }
                else
                {
                    if (item.RideDays != null)
                    {
                        repository.PopulateRideDates(item, TZone);
                        repository.Save();
                        NextRide = repository.GetClosestNextRide(item, TZone);
                        _GroupModel._NextRideDetails.Add(new NextRideDetails {
                            Date               = NextRide.RideDate,
                            GroupId            = item.id,
                            NumberofKeenRiders = NextRide.Riders.Where(i => i.PercentKeen == "In").Count()
                        });
                    }
                }

                if (repository.IsGroupCreator(item.id, UserId))
                {
                    _GroupModel._OwnerGroupList.Add(item.id);
                }
            }

            if (_GroupModel._NextRideDetails != null)
            {
                _GroupModel._NextRideGroupbyDayOfWeek = _GroupModel._NextRideDetails.GroupBy(g => g.Date.DayOfWeek, g => g.GroupId, (key, p) =>
                                                                                             new NextRideGroupbyDayOfWeek {
                    Thedayofweek = key, Groupids = p.ToList()
                }).OrderBy(i => i.Thedayofweek);
            }

            if (_GroupModel._NextRideDetailsPrivate != null)
            {
                _GroupModel._NextRideGroupbyDayOfWeekPrivate = _GroupModel._NextRideDetailsPrivate.GroupBy(g => g.Date.DayOfWeek, g => g.GroupId, (key, p) =>
                                                                                                           new NextRideGroupbyDayOfWeekPrivate {
                    Thedayofweek = key, Groupids = p.ToList()
                }).OrderBy(i => i.Thedayofweek);
            }
            //Show private first.
            //_GroupModel._Groups.OrderByDescending(g => g.IsPrivate);
            _GroupModel.CurrentGroupMembership = repository.CurrentGroupsForUser(UserId);

            return(_GroupModel);
        }
Пример #2
0
        public ActionResult Index(int groupid = -1, int rideid = -1, int InviteId = -1, string fromhome = "false")
        {
            var currentUser = idb.Users.Find(User.Identity.GetUserId());

            if (InviteId != -1)
            {
                if (repository.PrivateBunchInviteUserEmailNotSet(InviteId))
                {
                    repository.UpdateInvitePrivateUser(currentUser.Id, currentUser.Email, InviteId);
                    repository.Save();
                }
            }

            RideModelIndex RideModel = new RideModelIndex();
            Group          _Group    = repository.GetGroupByID(groupid);

            //If group is null, see if we can get the group from the rideid and if so recall action with just the groupid set.
            if (_Group == null)
            {
                _Group = repository.GetGroupByRideID(rideid);
                if (_Group != null)
                {
                    rideid  = -1;
                    groupid = _Group.id;
                    return(RedirectToAction("Index", new { groupid = _Group.id }));
                }
            }

            if (_Group == null)
            {
                GroupModel GroupModel = new GroupModel();
                GroupModel._Groups = repository.GetGroups().ToList();
                GroupModel.CurrentGroupMembership = repository.CurrentGroupsForUser(currentUser.Id);
                return(RedirectToAction("index", "group", GroupModel));
            }

            //Just in case location ID has not been set, set to same as ride.
            if (currentUser.LocationID == null)
            {
                currentUser.LocationID = _Group.Location.id;
                idb.SaveChanges();
            }

            RideModelHelper _RideHelper = new RideModelHelper(repository);
            var             t           = Request.QueryString["groupId"];

            if (groupid != -1 || rideid != -1)
            {
                RideModel = _RideHelper.PopulateRideModel(rideid, groupid, currentUser.Id, true);
            }
            else
            {
                GroupModel GroupModel = new GroupModel();
                GroupModel._Groups = repository.GetGroups().ToList();
                GroupModel.CurrentGroupMembership = repository.CurrentGroupsForUser(currentUser.Id);
                return(RedirectToAction("index", "group", GroupModel));
            }

            if (RideModel.Ride != null)
            {
            }
            else
            {
                GroupModel GroupModel = new GroupModel();
                GroupModel._Groups = repository.GetGroups().ToList();
                GroupModel.CurrentGroupMembership = repository.CurrentGroupsForUser(currentUser.Id);
                return(RedirectToAction("index", "group", GroupModel));
            }

            RideModel.FromHome    = fromhome;
            RideModel.IsFavourite = repository.IsInFavouriteList(RideModel.Group.id, currentUser.Id);
            if (_Group.Lat != null)
            {
                RideModel.lat = _Group.Lat;
            }

            if (_Group.Lng != null)
            {
                RideModel.lng = _Group.Lng;
            }

            if (RideModel.Group.IsPrivate)
            {
                if (!repository.IsInvitedToPrivateBunch(RideModel.Group.id, currentUser.Id))
                {
                    GroupModel GroupModel = new GroupModel();
                    GroupModel._Groups = repository.GetGroups().ToList();
                    GroupModel.CurrentGroupMembership = repository.CurrentGroupsForUser(currentUser.Id);
                    return(RedirectToAction("index", "group", GroupModel));
                }
            }

            RideModel.RouteCount = repository.RouteCountForGroup(RideModel.Group.id);

            return(View(RideModel));
        }