public async Task <IActionResult> FindAround(FindAroundRequest request) { try { var users = await _userApplication.FindAround(request); return(Ok(users)); } catch (Exception e) { return(BadRequest(new { Message = e.Message })); } }
public async Task <List <UserDisplay> > FindAround(FindAroundRequest request) { var user = await _context.Users.FindAsync(request.UserId); if (user == null) { throw new Exception("Can not find user"); } var users = await _context.Users.Where(x => x.Status == EUserStatus.Active && x.IsUpdatePosition).ToListAsync(); users.Remove(user); try { if (request.Gender == 1) { users = users.Where(x => x.Gender == EGender.Nam).ToList(); } else if (request.Gender == 2) { users = users.Where(x => x.Gender == EGender.Nữ).ToList(); } if (request.AgeGroup != -1) { users = users.Where(x => GetAgeGroup(x.Dob) == (EAgeGroup)request.AgeGroup).ToList(); } } catch (Exception) { throw new Exception("Parameter not correct"); } var currentCoordinates = new Coordinates(user.Latitude, user.Longitude); foreach (var u in users) { var distance = new Coordinates(u.Latitude, u.Longitude).DistanceTo(currentCoordinates); u.Distance = distance; } users = users.Where(x => x.Distance <= request.Distance).ToList(); users = users.OrderBy(x => x.Distance) .Skip((request.PageIndex - 1) * request.PageSize) .Take(request.PageSize).ToList(); var responses = new List <UserDisplay>(); foreach (var item in users) { var res = new UserDisplay(item, _storageService); res.Followed = await IsFollowed(request.UserId, item.Id); res.Favorited = await IsLiked(request.UserId, item.Id); responses.Add(res); } return(responses); }