Example #1
0
 public async Task <JsonResult> GetServices(ServicesClientRequest request)
 {
     return(Json(await _clientService.GetServices(request)));
 }
        public async Task <IEnumerable <ServiceClientViewModel> > GetServices(ServicesClientRequest req)
        {
            GeoCoordinate reqCoordinate = new GeoCoordinate(req.latitude, req.longitude);

            var allServiceLocations = await _locationRepository.GetAllIncludeAsync();

            var x = allServiceLocations.Where(e =>
            {
                GeoCoordinate geo      = new GeoCoordinate((double)e.Latitude, (double)e.Longitude);
                double distanceBetween = reqCoordinate.GetDistanceTo(geo);

                if (distanceBetween / 1000 > req.distance)
                {
                    return(false);
                }

                if (req.operations.Length > 0)
                {
                    if (!e.ServiceLocationOperations.Any(z => req.operations.Any(m => m == z.ServiceOperationId)))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (req.opTypeId != null)
                    {
                        if (!e.ServiceLocationOperations.Any(z =>
                                                             z.ServiceOperation.ServiceOperationType.ServiceOperationTypeId == req.opTypeId))
                        {
                            return(false);
                        }
                    }
                }

                if (req.facilites.Length > 0)
                {
                    if (!e.ServiceLocationFacilities.Any(z => req.facilites.Any(m => m == z.ServiceFacilityId)))
                    {
                        return(false);
                    }
                }

                if (req.carManId != null)
                {
                    if (!e.ServiceManufacturers.Any(z => z.CarManufacturerId == req.carManId))
                    {
                        return(false);
                    }
                }

                return(true);
            }).Select(e => new ServiceClientViewModel()
            {
                LocationId = e.ServiceLocationId,
                Name       = e.ServiceInfo.Nume,
                Address    = e.Address,
                Distance   = GetDistance(reqCoordinate, e.Latitude, e.Longitude),
                Image      = e.ServiceLocationPhotos.FirstOrDefault().Link,
                NoImages   = e.ServiceLocationPhotos.Count(),
                ReviewAvg  = Math.Round(e.ServiceLocationReviews.Select(z => z.Rate).DefaultIfEmpty(0).Average(), 1),
                Comments   = e.ServiceLocationReviews.Count(z => z.Review.Length > 0),
                Longitude  = (double)e.Longitude,
                Latitude   = (double)e.Latitude
            });

            return(x);
        }