Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sofiia assignment week 2 #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import supertest from "supertest";
import { app } from "../app.js";

const request = supertest(app);

describe("POST /weather", () => {
it("should return weather data for a valid city", async () => {
const response = await request
.post("/weather")
.send({ cityName: "Amsterdam" });
expect(response.status).toBe(200);
expect(response.body.weatherText).toContain("The weather in Amsterdam is");
});

it("should return an error if the city is not found", async () => {
const response = await request
.post("/weather")
.send({ cityName: "BlaBlaBla" });
expect(response.status).toBe(404);
expect(response.body.weatherText).toBe("City is not found!");
});

it("should return an error if no cityName is provided", async () => {
const response = await request.post("/weather").send({});
expect(response.status).toBe(400);
expect(response.body.weatherText).toBe("City name is required!");
});
});
41 changes: 41 additions & 0 deletions assignments/hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import express from "express";
import fetch from "node-fetch";
import { API_KEY } from "./sources/keys.js";

export const app = express();

app.use(express.json());

app.get("/", (req, res) => {
res.send("Welcome to the Weather App!");
});

app.post("/weather", async (req, res) => {
const cityName = req.body.cityName;

if (!cityName) {
return res.status(400).json({ weatherText: "City name is required!" });
}

try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&APPID=${API_KEY}&units=metric`
);
const data = await response.json();

if (data.cod !== 200) {
if (data.cod === "404") {
return res.status(data.cod).json({ weatherText: "City is not found!" });
}

return res.status(data.cod).json({ weatherText: `${data.message}` });
}

const temp = data.main.temp;
return res
.status(200)
.json({ weatherText: `The weather in ${cityName} is ${temp}°C` });
} catch (error) {
return res.status(500).json({ weatherText: "Something went wrong!" });
}
});
3 changes: 3 additions & 0 deletions assignments/hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
7 changes: 7 additions & 0 deletions assignments/hackyourtemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
transform: {
"^.+\\.js$": "babel-jest",
},
testEnvironment: "node",
transformIgnorePatterns: ["/node_modules/(?!(node-fetch)/)"],
};
27 changes: 27 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"dev": "nodemon server.js",
"start": "node server.js",
"test": "jest"
},
"keywords": [],
"author": "Sofiia <sonia.nosenko@gmail.com>",
"license": "ISC",
"description": "",
"type": "module",
"dependencies": {
"express": "^4.21.0",
"express-handlebars": "^8.0.1",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"@babel/preset-env": "^7.25.4",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"nodemon": "^3.1.7",
"supertest": "^7.0.0"
}
}
7 changes: 7 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { app } from "./app.js";

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
1 change: 1 addition & 0 deletions assignments/hackyourtemperature/sources/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_KEY = "2e63eec1418ead995cb13d960d42730c";