diff --git a/assignments/hackyourtemperature/__tests__/app.test.js b/assignments/hackyourtemperature/__tests__/app.test.js new file mode 100644 index 000000000..cb858dca2 --- /dev/null +++ b/assignments/hackyourtemperature/__tests__/app.test.js @@ -0,0 +1,30 @@ +import app from "../app.js"; +import supertest from "supertest"; + +jest.setTimeout(20000); +const request = supertest(app); + +describe("POST /weather", () => { + it("Quick test", () => expect(1).toBe(1)); + + it("400 no cityName", async () => { + const res = await request.post("/weather").send({}); + expect(res.status).toBe(400); + expect(res.body.weatherText).toContain("Please provide a cityName"); + }); + + it("404", async () => { + const res = await request + .post("/weather") + .send({ cityName: "asldkfjasldkfj" }); + expect(res.status).toBe(404); + expect(res.body.weatherText).toBe("City is not found!"); + }); + + it("200 °C", async () => { + const res = await request.post("/weather").send({ cityName: "Amsterdam" }); + expect(res.status).toBe(200); + expect(res.body.weatherText).toEqual(expect.stringContaining("Amsterdam")); + expect(res.body.weatherText).toEqual(expect.stringContaining("°C")); + }); +}); \ No newline at end of file diff --git a/assignments/hackyourtemperature/app.js b/assignments/hackyourtemperature/app.js new file mode 100644 index 000000000..440a88791 --- /dev/null +++ b/assignments/hackyourtemperature/app.js @@ -0,0 +1,45 @@ +import express from "express"; +import keys from "./sources/keys.js"; + +const app = express(); +app.use(express.json()); + +app.get("/", (req, res) => { + res.type("text").send("hello from backend to frontend!"); +}); + +app.post("/weather", async (req, res) => { + const { cityName } = req.body ?? {}; + if (!cityName || typeof cityName !== "string" || !cityName.trim()) { + return res.status(400).json({ weatherText: "Please provide a cityName." }); + } + + const url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent( + cityName + )}&units=metric&appid=${keys.API_KEY}`; + + try { + const r = await fetch(url); + const data = await r.json(); + + if (!r.ok || String(data.cod) !== "200") { + return res.status(404).json({ weatherText: "City is not found!" }); + } + + const name = data?.name ?? cityName; + const temp = data?.main?.temp; + + return res + .status(200) + .json({ + weatherText: `The current temperature in ${name} is ${Math.round( + temp + )}°C.`, + }); + } catch (e) { + console.error(e); + return res.status(500).json({ weatherText: "Server error." }); + } +}); + +export default app; \ No newline at end of file diff --git a/assignments/config-files/babel.config.cjs b/assignments/hackyourtemperature/babel.config.cjs similarity index 100% rename from assignments/config-files/babel.config.cjs rename to assignments/hackyourtemperature/babel.config.cjs diff --git a/assignments/config-files/jest.config.js b/assignments/hackyourtemperature/jest.config.js similarity index 100% rename from assignments/config-files/jest.config.js rename to assignments/hackyourtemperature/jest.config.js diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json new file mode 100644 index 000000000..0d7b77f70 --- /dev/null +++ b/assignments/hackyourtemperature/package.json @@ -0,0 +1,19 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "type": "module", + "scripts": { + "start": "node server.js", + "test": "jest" + }, + "dependencies": { + "express": "^5.1.0", + "node-fetch": "^3.3.2" + }, + "devDependencies": { + "@babel/preset-env": "^7.28.5", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "supertest": "^6.3.4" + } +} diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js new file mode 100644 index 000000000..1ab4fab7f --- /dev/null +++ b/assignments/hackyourtemperature/server.js @@ -0,0 +1,6 @@ +import app from "./app.js"; + +const PORT = process.env.PORT || 3009; +app.listen(PORT, () => { + console.log(`Server listening on http://localhost:${PORT}`); +}); \ 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..bc3b84273 --- /dev/null +++ b/assignments/hackyourtemperature/sources/keys.js @@ -0,0 +1,5 @@ +const keys = { + API_KEY: "60238be831d8710a4f2928681949b036" +}; + +export default keys; \ No newline at end of file diff --git a/week2/prep-exercises/1-blog-API/My first blog.txt b/week2/prep-exercises/1-blog-API/My first blog.txt new file mode 100644 index 000000000..6769dd60b --- /dev/null +++ b/week2/prep-exercises/1-blog-API/My first blog.txt @@ -0,0 +1 @@ +Hello world! \ No newline at end of file diff --git a/week2/prep-exercises/1-blog-API/package.json b/week2/prep-exercises/1-blog-API/package.json index d89c4bd76..10032569f 100644 --- a/week2/prep-exercises/1-blog-API/package.json +++ b/week2/prep-exercises/1-blog-API/package.json @@ -1,7 +1,7 @@ { "name": "1-blog-api", "version": "1.0.0", - "description": "", + "description": "Anyone here still remember blogs!? They were all the rage around 10 years ago. We are a bit late to the party, but I think we can still make some money with a blog application.", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.17.1" - } + "express": "^4.21.2" + }, + "keywords": [] } diff --git a/week2/prep-exercises/1-blog-API/server.js b/week2/prep-exercises/1-blog-API/server.js index 3f615e8f5..d725baf48 100644 --- a/week2/prep-exercises/1-blog-API/server.js +++ b/week2/prep-exercises/1-blog-API/server.js @@ -1,10 +1,69 @@ -const express = require('express') +const express = require('express'); +const fs = require('fs'); const app = express(); - -// YOUR CODE GOES IN HERE +app.use(express.json()); + +app.delete('/blogs/:title', (req, res) => { + const title = req.params.title; + const filePath = `${title}.txt`; + + if (fs.existsSync(filePath)) { + fs.unlinkSync(filePath); + res.send('ok'); + } else { + res.status(404).send('This post does not exist!'); + } +}); + +app.put('/blogs/:title', (req, res) => { + const title = req.params.title; + const { content } = req.body; + + if (!content) { + return res.status(400).send('Missing content'); + } + + const filePath = `${title}.txt`; + + if (fs.existsSync(filePath)) { + fs.writeFileSync(filePath, content); + res.send('ok'); + } else { + res.status(404).send('This post does not exist!'); + } +}); + + +app.post('/blogs', (req, res) => { + const { title, content } = req.body; + + if (!title || !content) { + return res.status(400).send('Missing title or content'); + } + + fs.writeFileSync(`${title}.txt`, content); + + res.end('ok'); +}); + + + +app.get('/blogs/:title', (req, res) => { + const title = req.params.title; + const filePath = `${title}.txt`; + + if (fs.existsSync(filePath)) { + const post = fs.readFileSync(filePath, 'utf-8'); + res.send(post); + } else { + res.status(404).send('This post does not exist!'); + } +}); + app.get('/', function (req, res) { res.send('Hello World') }) - -app.listen(3000) \ No newline at end of file + + +app.listen(3000, () => console.log('Server running on http://localhost:3000')); \ No newline at end of file