Skip to content

Commit

Permalink
Adding Lugares API
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Garcia committed Apr 14, 2019
1 parent 4fddbb7 commit d1baa61
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,27 @@ Eg schema
type: ''
}
```
__lugares:__ collection for places/landmarks
- name: landmark name.
- attributes: JSON field of landmark atteributes. Eg. Hospital Type
- location: GeoJSON field of point type.
- created_at: date the data is inserted/created
- type: the type of the place. Eg. "hospital", "school"
Eg schema
```
{
location: {
type: "point",
coordinates: [ 40, 5 ]
},
name: '',
attributes: '',
create_at: '2019-01-01 01:00:00',
type: ''
}
```
36 changes: 36 additions & 0 deletions lib/lugares-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const MongoClient = require('mongodb').MongoClient;

class LugaresClient {
constructor(db) {
this.db = db;
}
async insert(data) {
try {
if(Array.isArray(data)) {
return await this.db.collection('lugares').insertMany(data);
}
return await this.db.collection('lugares').insertOne(data);
} catch(error) {
throw error;
}
}
async find(query={}) {
try {
return await this.db.collection('lugares').find(query).toArray();
} catch (error) {
throw error;
}
}
}

async function getLugaresDBClient() {
try {
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
return new LugaresClient(client.db());
} catch(error) {
throw error;
}
}

module.exports = getLugaresDBClient;
20 changes: 20 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,25 @@ router.post('/apagon', async (req, res, next) => {
}
});

// API routes
router.get('/lugar', async (req, res, next) => {
const dbClient = await getLugaresDBClient();
const results = await dbClient.find();

res.json(results);
});


router.post('/lugar', async (req, res, next) => {
const dbClient = await getLugaresDBClient();
const data = req.body;
try {
const results = await dbClient.insert(data);
res.json(results);
} catch (error) {
console.error(error);
}
});

module.exports = router;

0 comments on commit d1baa61

Please sign in to comment.