コード例 #1
0
        public ActionResult GetALocation([FromRoute] int id)
        {
            var db = new RecycleMeApiContext();

            return(Ok(db.Locations
                      .Include(i => i.LocationMaterials)
                      .ThenInclude(i => i.Material)
                      .SingleOrDefault(s => s.Id == id)));
        }
コード例 #2
0
        public ActionResult Get([FromQuery] String searchTerm = null, bool?plastics = null, bool?paper = null, bool?glass = null, bool?cardboard = null, bool?aluminum_cans = null, bool?electronics = null, bool?metal = null, bool?chemicals = null, bool?yard_waste = null)
        {
            var db = new RecycleMeApiContext();

            searchTerm = searchTerm?.ToLower();
            Console.WriteLine("search term is " + searchTerm);

            var materials = GetMaterialListFilter(plastics, paper, glass, cardboard, aluminum_cans, electronics, metal, chemicals, yard_waste);

            // Get the Ids of the materials we are searching for
            var materialsIds = db.Materials.Where(w => materials.Contains(w.MaterialType)).Select(s => s.Id);


            // query the locationsMaterials table to select locations that have that
            // This is where the filtering happens

            var filteredLocationsByName = db
                                          .LocationMaterials
                                          .Include(i => i.Location)
                                          .Include(i => i.Material)
                                          .AsQueryable();

            if (searchTerm != null)
            {
                filteredLocationsByName = filteredLocationsByName.Where(w => w.Location.CenterName.ToLower().Contains(searchTerm));
            }

            if (materialsIds.Any())
            {
                filteredLocationsByName = filteredLocationsByName
                                          .Where(w => materialsIds.Contains(w.MaterialsId));
            }

            var locationIds = filteredLocationsByName.Select(s => s.Id);

            var locations = db
                            .Locations
                            .Include(i => i.LocationMaterials).ThenInclude(t => t.Material)
                            .Where(w => locationIds.Contains(w.Id));

            return(Ok(locations));
        }
コード例 #3
0
        public ActionResult GetNearBy([FromQuery] double lat, [FromQuery] double lng, bool?plastics = null, bool?paper = null, bool?glass = null, bool?cardboard = null, bool?aluminum_cans = null, bool?electronics = null, bool?metal = null, bool?chemicals = null, bool?yard_waste = null)
        {
            var db        = new RecycleMeApiContext();
            var materials = GetMaterialListFilter(plastics, paper, glass, cardboard, aluminum_cans, electronics, metal, chemicals, yard_waste);

            // Get the Ids of the materials we are searching for
            var materialsIds = db.Materials.Where(w => materials.Contains(w.MaterialType)).Select(s => s.Id);

            var filteredLocationsByName = db
                                          .LocationMaterials
                                          .Include(i => i.Location)
                                          .Include(i => i.Material)
                                          .AsQueryable();

            if (materialsIds.Any())
            {
                filteredLocationsByName = filteredLocationsByName
                                          .Where(w => materialsIds.Contains(w.MaterialsId));
            }

            var locationIds = filteredLocationsByName.Select(s => s.Id);

            var locations = db
                            .Locations
                            .Include(i => i.LocationMaterials).ThenInclude(t => t.Material)
                            .Where(w => locationIds.Contains(w.Id));


            var rv = (from location in locations
                      let distance = Math.Sqrt(Math.Pow(location.Latitude - lat, 2) + Math.Pow(location.Longitude - lng, 2))                                                                                           // where distance <= 10000

                                     select new { location = location, Distance = distance }).OrderBy(o => o.Distance).Select(s => s.location).Include(i => i.LocationMaterials).ThenInclude(t => t.Material).Take(5); //.Take(5).OrderBy(x => x.distance).ToList();



            return(Ok(rv));
        }
コード例 #4
0
        public ActionResult GetAction()
        {
            var db = new RecycleMeApiContext();

            return(Ok(db.Locations.Include(i => i.LocationMaterials).ThenInclude(t => t.Material)));
        }