concept location in category angular

appears as: locations, location, locations, Location
Getting MEAN with Mongo, Express, Angular, and Node.js 2ED

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.

Next, take the list of screens and collate them where they logically belong together. The first three screens in the list, for example, deal with locations. The About page doesn’t belong anywhere, so it can go in a miscellaneous Others collection. A sketch of this arrangement looks something like figure 2.9.

Table 6.1. URL paths and parameters for an API to the Locations collection

Action

URL path

Example

Create new location /locations http://loc8r.com/api/locations
Read list of locations /locations http://loc8r.com/api/locations
Read a specific location /locations/:locationid http://loc8r.com/api/locations/123
Update a specific location /locations/:locationid http://loc8r.com/api/locations/123
Delete a specific location /locations/:locationid http://loc8r.com/api/locations/123

As you can see from table 6.1, each action has the same URL path, and three of them expect the same parameter to specify a location. This situation poses an obvious question: how do you use the same URL to initiate different actions? The answer lies in request methods.

The homepage of Loc8r should display a list of locations based on the user’s current geographical location. MongoDB and Mongoose have some special geospatial aggregation methods to help find nearby places.

Here, you’ll use the Mongoose aggregate $geoNear to find a list of locations close to a specified point, up to a specified maximum distance. $geoNear is an aggregation method that accepts multiple configuration options, of which of the following are required:

Listing 6.4. Locations list controller locationsListByDistance
const locationsListByDistance = async(req, res) => {
  const lng = parseFloat(req.query.lng);
  const lat = parseFloat(req.query.lat);
  const near = {
    type: "Point",
    coordinates: [lng, lat]
  };
  const geoOptions = {
    distanceField: "distance.calculated",
    key: 'coords',
    spherical: true,
    maxDistance: 20000,
    limit: 10
  };
  if (!lng || !lat) {                                            #1
    return res                                                   #1
      .status(404)                                               #1
      .json({                                                    #1
      "message": "lng and lat query parameters are required"     #1
    });                                                          #1
  }                                                              #1
  try {
    const results = await Loc.aggregate([
      {
        $geoNear: {
          near,
          ...geoOptions
        }
      }
    ]);
    const locations = results.map(result => {
      return {
        id: result._id
        name: result.name,
        address: result.address,
        rating: result.rating,
        facilities: result.facilities,
        distance: `${result.distance.calculated.toFixed()}m`
      }
    });
    res
      .status(200)
      .json(locations);
  } catch (err) {
    res
      .status(404)                                               #2
      .json(err);
  }
};
Figure 7.8. The complete Details page. The ID of the location is passed from the URL to the API, and the API retrieves the data and passes it back to the page to be formatted and rendered correctly.