Skip to content

Commit 530914b

Browse files
committed
9.6 - 9.7
1 parent 95c7e65 commit 530914b

5 files changed

Lines changed: 768 additions & 7 deletions

File tree

part9/.eslintrc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:@typescript-eslint/recommended",
5+
"plugin:@typescript-eslint/recommended-requiring-type-checking"
6+
],
7+
"plugins": ["@typescript-eslint"],
8+
"env": {
9+
"node": true,
10+
"es6": true
11+
},
12+
"rules": {
13+
"@typescript-eslint/semi": ["error"],
14+
"@typescript-eslint/explicit-function-return-type": "off",
15+
"@typescript-eslint/explicit-module-boundary-types": "off",
16+
"@typescript-eslint/restrict-template-expressions": "off",
17+
"@typescript-eslint/restrict-plus-operands": "off",
18+
"@typescript-eslint/no-unused-vars": [
19+
"error",
20+
{ "argsIgnorePattern": "^_" }
21+
],
22+
"no-case-declarations": "off"
23+
},
24+
"parser": "@typescript-eslint/parser",
25+
"parserOptions": {
26+
"project": "./tsconfig.json"
27+
}
28+
}

part9/exerciseCalculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface Result {
88
average: number;
99
}
1010

11-
const calculateExercise = (
11+
export const calculateExercise = (
1212
dailyExerciseHours: number[],
1313
target: number
1414
): Result => {

part9/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-call */
2+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
13
import express from "express";
24
import { calculateBMI } from "./bmiCalculator";
5+
import { calculateExercise } from "./exerciseCalculator";
36

47
const app = express();
8+
app.use(express.json());
59

610
app.get("/hello", (_req, res) => {
711
res.send("Hello Full Stack!");
@@ -27,6 +31,31 @@ app.get("/bmi", (req, res) => {
2731
});
2832
});
2933

34+
app.post("/exercises", (req, res) => {
35+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
36+
const { daily_exercises, target } = req.body;
37+
console.log(req.body);
38+
if (!daily_exercises || !target) {
39+
res.status(400).send({ error: "malformatted parameters" });
40+
}
41+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
42+
const dailyExerciseHoursNum: number[] = daily_exercises.map(
43+
(hours: number) => {
44+
const hoursNum = Number(hours);
45+
if (isNaN(hoursNum)) {
46+
res.status(400).send({ error: "malformation parameters" });
47+
}
48+
return hoursNum;
49+
}
50+
);
51+
const targetNum = Number(target);
52+
if (isNaN(targetNum)) {
53+
res.status(400).send({ error: "malformatted parameters" });
54+
}
55+
const result = calculateExercise(dailyExerciseHoursNum, targetNum);
56+
res.send(result);
57+
});
58+
3059
app.listen(3333, () => {
3160
console.log("🚀 Server started on port 3333!");
3261
});

part9/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
"calculateBMI": "ts-node bmiCalculator.ts",
88
"calculateExercises": "ts-node exerciseCalculator.ts",
99
"start": "ts-node index.ts",
10-
"dev": "ts-node-dev index.ts"
10+
"dev": "ts-node-dev index.ts",
11+
"lint": "eslint --ext .ts ."
1112
},
1213
"devDependencies": {
1314
"@types/express": "^4.17.13",
1415
"@types/node": "^17.0.10",
16+
"@typescript-eslint/eslint-plugin": "^5.10.0",
17+
"@typescript-eslint/parser": "^5.10.0",
18+
"eslint": "^8.7.0",
1519
"ts-node": "^10.4.0",
1620
"ts-node-dev": "^1.1.8",
1721
"tslib": "^2.3.1",

0 commit comments

Comments
 (0)