public IActionResult GetListedWithinDistance([FromBody] NearbyRequest request) { var user = userRepository.GetUserFromPrincipal(HttpContext.User); if (!userRepository.IsActiveUser(user)) { return(new UnauthorizedResult()); } if (!userRepository.IsRecipient(user) && !userRepository.IsActiveAdmin(user)) { return(BadRequest("You don't have priveleges to view all nearby donations.")); } double meters = request.Miles * 1609.34; // If no location given, use organization location var Lat = request.Latitude ?? user.Lat; var Long = request.Longitude ?? user.Long; var donations = donationRepository.GetListedWithinDistance(Lat, Long, meters); foreach (var donation in donations) { donation.Donor = userRepository.GetById(donation.DonorId); if (donation.RecipientId.HasValue) { donation.Recipient = userRepository.GetById(donation.RecipientId.Value); } } return(Ok(donations)); }
public async Task <IActionResult> GetEstablishmentsInRadiusAsync( [FromBody] NearbyRequest nearbyRequest, CancellationToken cancellationToken = default) { if (nearbyRequest == null) { throw new ArgumentNullException(nameof(nearbyRequest)); } if (nearbyRequest.Radius <= 0) { return(BadRequest("Radius must be greater than 0")); } var center = new Point(nearbyRequest.Longitude, nearbyRequest.Latitude); var radius = nearbyRequest.Radius; _logger.LogInformation($"Searching Establishments in a radius of {radius} FROM {center}"); var establishments = await _establishmentRepository.GetAllWithinRadiusAsync(radius, center, cancellationToken); var mappedEstablishments = _domainMapper.MapEstablishmentResponses(establishments); return(Ok(mappedEstablishments.Any() ? mappedEstablishments : Enumerable.Empty <EstablishmentResponse>())); }
public async Task <ActionResult <NearbyResponse> > GetNearbyWantedHelp([FromBody] NearbyRequest req) { try { await HttpContext.Session.LoadAsync().ConfigureAwait(false); if (!HttpContext.Session.Keys.Contains("Id")) { return(Unauthorized()); } User user = this._db.User .Where(u => u.Id == HttpContext.Session.GetInt32("Id")) .FirstOrDefault(); float lat = req.Latitiude; float lng = req.Longitude; IEnumerable <Post> posts = this._db.Post.AsEnumerable().Where(p => Helper.IsClose(p, lat, lng, user.Radius)); NearbyResponse res = new NearbyResponse(); res.posts = new List <ResponsePost>(); foreach (Post p in posts) { res.posts.Add(new ResponsePost() { Address = p.Address, Description = p.Description, FirstName = p.OwnerNavigation.FirstName, LastName = p.OwnerNavigation.LastName, Latitude = p.Latitude, Longitude = p.Longitude, TimePosted = p.TimePosted, Title = p.Title, TimeDue = p.TimeDue, Username = p.OwnerNavigation.Username }); } res.statusCode = WebTypes.StatusCode.OK; return(res); } catch (Exception e) { this._logger.LogError(e, "Fucky wuckie happened!", null); return(new NearbyResponse() { statusCode = WebTypes.StatusCode.ERROR, errorMessage = "Server side error!" }); } }