diff --git a/assignments/config-files/hackyourtemperature/__tests__/app.test.js b/assignments/config-files/hackyourtemperature/__tests__/app.test.js new file mode 100644 index 000000000..36de251aa --- /dev/null +++ b/assignments/config-files/hackyourtemperature/__tests__/app.test.js @@ -0,0 +1,46 @@ +const request = require("supertest"); + +jest.mock("node-fetch", () => jest.fn()); + +const fetch = require("node-fetch"); +const app = require("../app.js"); + +describe("POST /weather", () => { + it("should return weather text for a valid city", async () => { + fetch.mockResolvedValue({ + json: async () => ({ + main: { temp: 280 }, + cod: 200, + }), + }); + + const response = await request(app) + .post("/weather") + .send({ cityName: "London" }); + + expect(response.statusCode).toBe(200); + expect(response.body.weatherText).toContain("The weather in London"); + }); + + it("should return an error if city name is missing", async () => { + const response = await request(app).post("/weather").send({}); + + expect(response.statusCode).toBe(400); + expect(response.body.weatherText).toBe("City name is required!"); + }); + + it("should return city not found for invalid city", async () => { + // نرجّع كود 404 من الـ API + fetch.mockResolvedValue({ + json: async () => ({ + cod: "404", + }), + }); + + const response = await request(app) + .post("/weather") + .send({ cityName: "asdkjashdkjashd" }); + + expect(response.body.weatherText).toBe("City is not found!"); + }); +}); diff --git a/assignments/config-files/hackyourtemperature/app.js b/assignments/config-files/hackyourtemperature/app.js new file mode 100644 index 000000000..ac98169be --- /dev/null +++ b/assignments/config-files/hackyourtemperature/app.js @@ -0,0 +1,40 @@ +const express = require("express"); +const fetch = require("node-fetch"); +const keys = require("./sources/keys.js"); + +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).send({ weatherText: "City name is required!" }); + } + + try { + const response = await fetch( + `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&APPID=${keys.API_KEY}` + ); + + const data = await response.json(); + + if (data.cod === "404") { + return res.send({ weatherText: "City is not found!" }); + } + + const temperature = data.main.temp; + + res.send({ + weatherText: `The weather in ${cityName} is ${temperature}K`, + }); + } catch (error) { + res.status(500).send({ weatherText: "Server error" }); + } +}); + +module.exports = app; diff --git a/assignments/config-files/hackyourtemperature/package.json b/assignments/config-files/hackyourtemperature/package.json new file mode 100644 index 000000000..09cef2bbc --- /dev/null +++ b/assignments/config-files/hackyourtemperature/package.json @@ -0,0 +1,18 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "main": "server.js", + "scripts": { + "start": "node server.js", + "test": "jest" + }, + "dependencies": { + "express": "^5.1.0", + "express-handlebars": "^8.0.3", + "node-fetch": "^2.7.0" + }, + "devDependencies": { + "jest": "^29.7.0", + "supertest": "^7.1.4" + } +} diff --git a/assignments/config-files/hackyourtemperature/server.js b/assignments/config-files/hackyourtemperature/server.js new file mode 100644 index 000000000..70a3aefca --- /dev/null +++ b/assignments/config-files/hackyourtemperature/server.js @@ -0,0 +1,7 @@ +import app from "./app.js"; + +const PORT = 3000; + +app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +}); diff --git a/assignments/config-files/hackyourtemperature/sources/keys.js b/assignments/config-files/hackyourtemperature/sources/keys.js new file mode 100644 index 000000000..79b666c95 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/sources/keys.js @@ -0,0 +1,3 @@ +const keys = { + API_KEY: "bd21a4fa2fde54d62c4e7df4d764ef31" +}; diff --git a/package.json b/package.json new file mode 100644 index 000000000..9803be463 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "node.js-cohort54", + "version": "1.0.0", + "description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Hadidreem17/Node.js-Cohort54.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Hadidreem17/Node.js-Cohort54/issues" + }, + "homepage": "https://github.com/Hadidreem17/Node.js-Cohort54#readme" +}