Пример #1
0
        private int GetDistanceFromCurrentUserAsync(UserModel mainUser, UserModel targetUser)
        {
            double resultDistance = LocationHelper.GetDistanceBetween((double)mainUser.Longitude, (double)mainUser.Latitude,
                                                                      (double)targetUser.Longitude, (double)targetUser.Latitude);

            return((int)Math.Round(resultDistance));
        }
Пример #2
0
        public async Task <Page <User> > GetPageAsync(UserFilter userFilter, User user)
        {
            double latitude  = (double)user.Latitude,
                   longitude = (double)user.Longitude;

            // hack for correct first user's output int filter list
            // without it first user will have no lang, hobbies etc...
            await _context.User
            .Include(u => u.Credentials)
            .Include(u => u.Languages)
            .Include(u => u.Hobbies)
            .ThenInclude(uh => uh.Hobby)
            .ThenInclude(h => h.Category)
            .FirstOrDefaultAsync();

            IEnumerable <User> users = await _context.User
                                       .Where(u => u.Id != user.Id)
                                       .Where(u => userFilter.Gender != null ?
                                              u.Gender == userFilter.Gender : true)
                                       .Where(u => (userFilter.Languages != null &&
                                                    userFilter.Languages.Count() != 0) ?
                                              userFilter.Languages.Any(fl => u.Languages.Any(
                                                                           ul => ul.LanguageCode == fl &&
                                                                           LevelInRange(ul, userFilter.MinLevel))) : true)
                                       .Where(u => (userFilter.MaxAge != null) ?
                                              GetAge(u.Birthdate) <= userFilter.MaxAge : true)
                                       .Where(u => (userFilter.MinAge != null) ?
                                              GetAge(u.Birthdate) >= userFilter.MinAge : true)
                                       .Include(u => u.Languages)
                                       .ThenInclude(ul => ul.Language)
                                       .Include(u => u.Credentials)
                                       .Include(u => u.Hobbies)
                                       .ThenInclude(h => h.Hobby)
                                       .ToListAsync();

            users = users
                    .Where(u => (userFilter.MaxDistance != null) ?
                           LocationHelper.GetDistanceBetween((double)u.Longitude, (double)u.Latitude,
                                                             longitude, latitude) <= userFilter.MaxDistance : true)
                    .ToArray();

            int totalAmount = users.Count();

            if (userFilter.PageSize == null)
            {
                userFilter.PageSize = _defaultPageSize;
            }

            return(new Page <User>()
            {
                Entities = users.OrderBy(u => u.CreatedAt)
                           .SkipWhile(m => m.CreatedAt > userFilter.TimeStamp)
                           .Skip((userFilter.PageNumber - 1) * userFilter.PageSize.Value)
                           .Take(userFilter.PageSize.Value).ToList(),
                PageNumber = userFilter.PageNumber,
                PageSize = userFilter.PageSize.Value,
                TotalAmount = totalAmount,
                TimeStamp = DateTimeOffset.UtcNow
            });
        }
Пример #3
0
        public async Task <int> GetDistanceFromCurrentUserAsync(Guid mainUserId, Guid targetUserId)
        {
            User mainUser = await _repository.GetAsync(mainUserId);

            User targetUser = await _repository.GetAsync(targetUserId);

            double resultDistance = LocationHelper.GetDistanceBetween((double)mainUser.Longitude, (double)mainUser.Latitude,
                                                                      (double)targetUser.Longitude, (double)targetUser.Latitude);

            return((int)Math.Round(resultDistance));
        }
Пример #4
0
        public async Task <Page <Event> > GetEventsPageAsync(
            EventFilter eventFilter, Location location)
        {
            double latitude   = (double)location.Latitude,
                   longtitude = (double)location.Longitude;

            DateTimeOffset now = DateTimeOffset.Now;

            IEnumerable <Event> events = await _context.Event
                                         .Where(e => (eventFilter.Languages != null &&
                                                      eventFilter.Languages.Count() != 0)?
                                                eventFilter.Languages.Contains(e.LanguageCode) : true)
                                         .Where(e => (eventFilter.MaxNumberOfParticipants != null) ?
                                                e.MaxParticipants <= eventFilter.MaxNumberOfParticipants : true)
                                         .Where(e => (eventFilter.MaxStartDate != null) ?
                                                e.StartTime <= eventFilter.MaxStartDate : true)
                                         .Where(e => (eventFilter.MinStartDate != null) ?
                                                e.StartTime >= eventFilter.MinStartDate : true)
                                         .Where(e => e.StartTime >= now)
                                         .Include(e => e.Creator)
                                         .Include(e => e.Language)
                                         .Include(e => e.Participants)
                                         .ThenInclude(ue => ue.User)
                                         .ToListAsync();

            events = events
                     .Where(e => (eventFilter.MaxDistance != null) ?
                            LocationHelper.GetDistanceBetween((double)e.Longitude, (double)e.Latitude,
                                                              longtitude, latitude) <= eventFilter.MaxDistance : true)
                     .ToArray();

            int totalAmount = events.Count();

            if (eventFilter.PageSize == null)
            {
                eventFilter.PageSize = _defaultPageSize;
            }

            return(new Page <Event>()
            {
                Entities = events.OrderBy(e => e.StartTime)
                           .SkipWhile(m => m.StartTime > eventFilter.TimeStamp)
                           .Skip((eventFilter.PageNumber - 1) * eventFilter.PageSize.Value)
                           .Take(eventFilter.PageSize.Value).ToList(),
                PageNumber = eventFilter.PageNumber,
                PageSize = eventFilter.PageSize.Value,
                TotalAmount = totalAmount,
                TimeStamp = DateTimeOffset.UtcNow
            });
        }