Exemple #1
0
        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));
        }