diff --git a/.gitignore b/.gitignore index b6b402ed9..fe5cc92eb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ yarn-error.log *.bkp week3/prep-exercise/server-demo/ +.env \ No newline at end of file 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..6be6bddbb --- /dev/null +++ b/assignments/config-files/hackyourtemperature/__tests__/app.test.js @@ -0,0 +1,25 @@ +import app from "../app.js"; +import supertest from "supertest"; + +const request = supertest(app); + +describe("POST /", () => { + it('should return 400 if no cityName provided', async() => { + const response = await request.post('/weather').send({}); + expect(response.status).toBe(400); + expect(response.body).toEqual({ message: 'You have to provide a city name!' }); + }); + + it('should return 404 if the cityName is not present in the data', async() => { + const response = await request.post('/weather').send({ cityName: 'ashjsdks'}); + expect(response.status).toBe(404); + expect(response.body).toEqual({ weatherText: 'City is not found!' }); + }); + + it('should return 200 if the cityName is found', async() => { + const response = await request.post('/weather').send({ cityName: 'Amsterdam' }); + expect(response.status).toBe(200); + expect(response.body.weatherText).toContain('Amsterdam'); + }); +}); + diff --git a/assignments/config-files/hackyourtemperature/app.js b/assignments/config-files/hackyourtemperature/app.js new file mode 100644 index 000000000..7288abd29 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/app.js @@ -0,0 +1,39 @@ +import express from 'express'; +import fetch from 'node-fetch'; +import { API_KEY } from './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) => { + let { cityName } = req.body; + + if (!cityName) { + return res.status(400).json({ message: 'You have to provide a city name!'}); + } + + try { + const response = await fetch( + `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric` + ); + + if (!response.ok) { + return res.status(404).json({ weatherText: 'City is not found!' }); + } + + const data = await response.json(); + const weather = data.main.temp; + + res.status(200).json({ weatherText: `The weather is ${weather}°C in ${cityName}` }); + } catch (error) { + console.error('Error fetching data!'); + res.status(500).json({ message: 'Something went wrong...' }) + } +}); + +export default app; \ No newline at end of file diff --git a/assignments/config-files/babel.config.cjs b/assignments/config-files/hackyourtemperature/babel.config.cjs similarity index 100% rename from assignments/config-files/babel.config.cjs rename to assignments/config-files/hackyourtemperature/babel.config.cjs diff --git a/assignments/config-files/jest.config.js b/assignments/config-files/hackyourtemperature/jest.config.cjs similarity index 94% rename from assignments/config-files/jest.config.js rename to assignments/config-files/hackyourtemperature/jest.config.cjs index 19ba9649e..26f9da62e 100644 --- a/assignments/config-files/jest.config.js +++ b/assignments/config-files/hackyourtemperature/jest.config.cjs @@ -1,4 +1,4 @@ -export default { +module.exports = { // Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer transform: { "^.+\\.jsx?$": "babel-jest", diff --git a/assignments/config-files/hackyourtemperature/package.json b/assignments/config-files/hackyourtemperature/package.json new file mode 100644 index 000000000..87d5e7790 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/package.json @@ -0,0 +1,27 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "type": "module", + "main": "server.js", + "scripts": { + "test": "jest", + "start": "nodemon server.js" + }, + "keywords": [], + "author": "Sertan Erdogan", + "license": "ISC", + "description": "", + "dependencies": { + "dotenv": "^16.4.7", + "express": "^4.21.2", + "express-handlebars": "^8.0.1", + "node-fetch": "^3.3.2" + }, + "devDependencies": { + "@babel/preset-env": "^7.26.9", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "nodemon": "^3.1.9", + "supertest": "^7.0.0" + } +} diff --git a/assignments/config-files/hackyourtemperature/server.js b/assignments/config-files/hackyourtemperature/server.js new file mode 100644 index 000000000..b39a23ebf --- /dev/null +++ b/assignments/config-files/hackyourtemperature/server.js @@ -0,0 +1,6 @@ +import app from './app.js'; +const PORT = 3000; + +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}); \ No newline at end of file diff --git a/assignments/config-files/hackyourtemperature/sources/keys.js b/assignments/config-files/hackyourtemperature/sources/keys.js new file mode 100644 index 000000000..9c92d38a7 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/sources/keys.js @@ -0,0 +1,4 @@ +import dotenv from 'dotenv'; +dotenv.config(); + +export const API_KEY = process.env.API_KEY; \ No newline at end of file