diff --git a/assignments/HackYourTemperature/__tests__/app.test.js b/assignments/HackYourTemperature/__tests__/app.test.js new file mode 100644 index 000000000..4d3390c76 --- /dev/null +++ b/assignments/HackYourTemperature/__tests__/app.test.js @@ -0,0 +1,28 @@ +import request from "supertest"; +import app from "../server.js"; + +describe("POST /weather", () => { + it("should return a 400 error if cityName is missing", async () => { + const response = await request.post("/weather").send({}); + expect(response.status).toBe(400); + expect(response.body).toEqual({ message: "City name is required." }); + }); + + it("should return a 404 error if cityName is not found", async () => { + const response = await request + .post("/weather") + .send({ cityName: "invalidCity" }); + expect(response.status).toBe(404); + expect(response.body.weatherText).toBe("city is not found!"); + }); + + it("should return a valid weather response for a real city", async () => { + const response = await request(app) + .post("/weather") + .send({ cityName: "Amsterdam" }); + expect(response.status).toBe(200); + expect(response.body.weatherText).toContain( + "The temperature in Amsterdam is" + ); + }); +}); diff --git a/assignments/HackYourTemperature/app.js b/assignments/HackYourTemperature/app.js new file mode 100644 index 000000000..b92414de2 --- /dev/null +++ b/assignments/HackYourTemperature/app.js @@ -0,0 +1,47 @@ +import express from "express"; +import keys from "./sources/keys.js"; +import fetch from "node-fetch"; + +const app = express(); + +app.use(express.json()); + +app.get("/", (req, res) => { + res.send("hello from backend to frontend!"); +}); + +app.post("/weather", async (req, res) => { + const cityName = req.body.cityName; + + if (!cityName) { + return res.status(400).json({ message: "City name is required." }); + } + const API_KEY = keys.API_KEY; + if (!API_KEY) { + return res.status(500).json({ message: "API key is missing." }); + } + const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`; + + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`API request failed with status ${response.status}`); + } + const data = await response.json(); + if (data.cod === "404") { + return res.status(404).json({ weatherText: "city is not found!" }); + } + + const temperature = data.main.temp; + res.status(200).json({ + weatherText: `The temperature in ${cityName} is ${temperature}°C`, + }); + } catch (error) { + console.error("Error fetching weather data:", error.message); + res + .status(500) + .json({ message: "An error occurred while fetching weather data." }); + } +}); + +export default app; diff --git a/assignments/HackYourTemperature/babel.config.cjs b/assignments/HackYourTemperature/babel.config.cjs new file mode 100644 index 000000000..edcf10310 --- /dev/null +++ b/assignments/HackYourTemperature/babel.config.cjs @@ -0,0 +1,4 @@ +module.exports = { + presets: ["@babel/preset-env", "@babel/preset-react"], + plugins: ["@babel/plugin-transform-modules-commonjs"], +}; diff --git a/assignments/HackYourTemperature/jest.config.js b/assignments/HackYourTemperature/jest.config.js new file mode 100644 index 000000000..013cf3358 --- /dev/null +++ b/assignments/HackYourTemperature/jest.config.js @@ -0,0 +1,3 @@ +export default { + transformIgnorePatterns: ["/node_modules/(?!node-fetch)"], +}; diff --git a/assignments/HackYourTemperature/package.json b/assignments/HackYourTemperature/package.json new file mode 100644 index 000000000..1efa7314a --- /dev/null +++ b/assignments/HackYourTemperature/package.json @@ -0,0 +1,25 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "main": "server.js", + "scripts": { + "test": "jest" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "dotenv": "^16.4.7", + "express": "^4.21.2", + "express-handlebars": "^8.0.1", + "node-fetch": "^3.3.2" + }, + "type": "module", + "devDependencies": { + "@babel/preset-env": "^7.26.9", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "supertest": "^7.0.0" + } +} diff --git a/assignments/HackYourTemperature/server.js b/assignments/HackYourTemperature/server.js new file mode 100644 index 000000000..9df674eda --- /dev/null +++ b/assignments/HackYourTemperature/server.js @@ -0,0 +1,7 @@ +import app from "./app.js"; + +const PORT = 3000; + +app.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); +}); diff --git a/assignments/HackYourTemperature/sources/.env b/assignments/HackYourTemperature/sources/.env new file mode 100644 index 000000000..d728cb490 --- /dev/null +++ b/assignments/HackYourTemperature/sources/.env @@ -0,0 +1 @@ +API_KEY: "6c6488cdc94811312595951b156b69ba", \ No newline at end of file diff --git a/assignments/HackYourTemperature/sources/keys.js b/assignments/HackYourTemperature/sources/keys.js new file mode 100644 index 000000000..502f27440 --- /dev/null +++ b/assignments/HackYourTemperature/sources/keys.js @@ -0,0 +1,5 @@ +import dotenv from "dotenv"; +dotenv.config(); +export const keys = { + API_KEY: process.env.API_KEY, +}; diff --git a/cd b/cd new file mode 100644 index 000000000..e69de29bb