1+ /* eslint-disable @typescript-eslint/no-unsafe-call */
2+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
13import express from "express" ;
24import { calculateBMI } from "./bmiCalculator" ;
5+ import { calculateExercise } from "./exerciseCalculator" ;
36
47const app = express ( ) ;
8+ app . use ( express . json ( ) ) ;
59
610app . 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+
3059app . listen ( 3333 , ( ) => {
3160 console . log ( "🚀 Server started on port 3333!" ) ;
3261} ) ;
0 commit comments