Skip to content

Commit 2ef3f34

Browse files
Weather-API - add firebase endpoints
1 parent 7e52726 commit 2ef3f34

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

weather-api/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
const express = require("express");
2+
const Firestore = require("@google-cloud/firestore");
3+
4+
const db = new Firestore();
25
const app = express();
6+
app.use(express.json());
37

48
app.get("/", (req, res) => {
59
const name = process.env.NAME || "ESP32";
6-
res.send(`Hello ${name}!`);
10+
res.send(`Welcome into ${name} Weather Station API!`);
11+
});
12+
13+
app.get("/weather/:placeId", async (req, res) => {
14+
const placeId = req.params.placeId;
15+
const query = db.collection("weather-data").where("placeId", "==", placeId);
16+
const querySnapshot = await query.get();
17+
if (querySnapshot.size > 0) {
18+
res.json(querySnapshot.docs);
19+
} else {
20+
res.status(404).json({ status: `${placeID} not Found` });
21+
}
22+
});
23+
24+
app.post("/weather", async (req, res) => {
25+
const data = {
26+
temperature: req.body.temperature,
27+
humidity: req.body.humidity,
28+
pressure: req.body.pressure,
29+
placeId: req.body.placeId,
30+
};
31+
// .doc() creates a new document, .set() sets the data into it
32+
await db.collection("weather-data").doc().set(data);
33+
// res.status(200).json({ ...data });
34+
res.statusCode(200); // to send only status code without any data
735
});
836

937
const port = process.env.PORT || 8080;

weather-api/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "weather-api",
33
"description": "API for weather data from ESP32 and BME280 sensor",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"private": true,
66
"main": "index.js",
77
"scripts": {
@@ -13,6 +13,7 @@
1313
"author": "Lukasz Matuszewski",
1414
"license": "Apache-2.0",
1515
"dependencies": {
16-
"express": "^4.17.1"
16+
"express": "^4.17.1",
17+
"@google-cloud/firestore": "^4.9.8"
1718
}
1819
}

0 commit comments

Comments
 (0)