This project implements a simple API for calculating BMI and exercise statistics, built with TypeScript and Express.
- Endpoint:
GET /bmi - Query Parameters:
height: Height in centimeters (number)weight: Weight in kilograms (number)
- Response:
{ "weight": 75, "height": 180, "bmi": "Normal (healthy weight)" } - Error Handling:
- Returns
400 Bad Requestwith an error message if parameters are missing or malformed.
- Returns
- Endpoint:
POST /exercises - Request Body:
{ "daily_exercises": [1, 0, 2, 0, 3, 0, 2.5], "target": 2.5 } - Response:
{ "periodLength": 7, "trainingDays": 4, "success": false, "rating": 1, "ratingDescription": "bad", "target": 2.5, "average": 1.2142857142857142 } - Error Handling:
- Returns
400 Bad Requestwith an error message if parameters are missing or malformed.
- Returns
The Patientor backend is a TypeScript-based Express application that provides API endpoints for managing patient and diagnosis data. It includes features such as data validation, type safety, and CORS support.
- Exercise 9.8: Initialized the backend project with TypeScript and Express.
- Exercise 9.9: Created a
GET /api/pingendpoint to confirm the server is running. - Exercise 9.10: Added a
GET /api/diagnosesendpoint to fetch all diagnoses. - Exercise 9.11: Added a
GET /api/patientsendpoint to fetch all patients, excluding thessnfield. - Exercise 9.12: Implemented a
POST /api/patientsendpoint to add new patients. - Exercise 9.13: Refactored the
genderfield to use an enum for type safety. - Exercise 9.14: Used Zod to validate requests to the
POST /api/patientsendpoint.
- Navigate to the
patientor-backenddirectory:cd exercises-9.8-9.14/patientor-backend - Install dependencies:
npm install
- Start the development server:
npm run dev
- The server will run on port
3001.
- GET /api/ping: Confirms the server is running.
- GET /api/diagnoses: Fetches all diagnoses.
- GET /api/patients: Fetches all patients, excluding the
ssnfield. - POST /api/patients: Adds a new patient. Requires the following fields:
name: String (required)dateOfBirth: String (required)ssn: String (optional)gender: Enum (male,female,other) (required)occupation: String (required)
- Patient:
interface Patient { id: string; name: string; dateOfBirth: string; ssn?: string; gender: Gender; occupation: string; }
- PublicPatient: A type that excludes the
ssnfield from Patient.
The POST /api/patients endpoint uses Zod for validation. The schema ensures all required fields are present and valid.
The Flight Diary application is a full-stack TypeScript project consisting of a backend API and a React frontend. It allows users to manage flight diary entries with proper type safety and validation.
- Exercise 9.15: Set up the flight diary backend with TypeScript.
- Exercise 9.16: Implemented backend validation for diary entries.
- Exercise 9.17: Created the frontend with React and TypeScript.
- Exercise 9.18: Added functionality to fetch and display diary entries.
- Exercise 9.19: Implemented form validation and error handling.
- Exercise 9.20: Enhanced the UI with styling and user feedback.
- Navigate to the flight diary backend directory:
cd exercises-9.15-9.20/flight-diary-back - Install dependencies:
npm install
- Start the development server:
npm run dev
- Navigate to the flight diary frontend directory:
cd exercises-9.15-9.20/flight-diary-front - Install dependencies:
npm install
- Start the development server:
npm run dev
- GET /api/diaries: Fetches all diary entries
- POST /api/diaries: Adds a new diary entry with the following fields:
date: String (YYYY-MM-DD format)visibility: String (enum: 'great' | 'good' | 'ok' | 'poor')weather: String (enum: 'sunny' | 'rainy' | 'cloudy' | 'stormy' | 'windy')comment: String
- DiaryEntry:
interface DiaryEntry { id: number; date: string; weather: Weather; visibility: Visibility; comment: string; }
- Responsive design with modern UI components
- Real-time form validation
- Error handling with user-friendly messages
- Type-safe API communication
The Enhanced Patientor application builds upon the previous version, adding support for different types of entries (Hospital, OccupationalHealthcare) and improving the user interface. The application demonstrates advanced TypeScript features and proper type safety implementation.
-
Exercise 9.21-9.24: Enhanced the data structure with new entry types
- Added support for Hospital entries
- Implemented OccupationalHealthcare entries
- Created type guards for different entry types
- Updated the backend to handle the new entry types
-
Exercise 9.25-9.27: Improved frontend functionality
- Implemented entry listing in patient page
- Added entry details display with type-specific information
- Enhanced type safety with proper discriminated unions
-
Exercise 9.28-9.30: Added entry creation functionality
- Implemented form for adding new entries
- Added validation for entry-specific fields
- Enhanced error handling and user feedback
- Navigate to the patientor backend directory:
cd exercises-9.21-9.30/patientor-backend - Install dependencies:
npm install
- Start the development server:
npm run dev
- Navigate to the patientor frontend directory:
cd exercises-9.21-9.30/patientor-frontend - Install dependencies:
npm install
- Start the development server:
npm run dev
- Entry Types:
interface BaseEntry { id: string; description: string; date: string; specialist: string; diagnosisCodes?: Array<string>; } interface HospitalEntry extends BaseEntry { type: "Hospital"; discharge: { date: string; criteria: string; }; } interface OccupationalHealthcareEntry extends BaseEntry { type: "OccupationalHealthcare"; employerName: string; sickLeave?: { startDate: string; endDate: string; }; }
- GET /api/patients/:id: Fetches detailed patient information including entries
- POST /api/patients/:id/entries: Adds a new entry to a patient's record
- Supports different entry types (Hospital, OccupationalHealthcare)
- Includes type-specific validation
- Type-safe entry handling with discriminated unions
- Improved UI for displaying different entry types
- Form validation for entry-specific fields
- Real-time error handling and user feedback
-
Install Dependencies:
npm install
-
Run the Server in Development Mode:
npm run dev
-
Build the Project:
npm run build
-
Start the Server in Production Mode:
npm start
- TypeScript: For type safety and modern JavaScript features.
- Express: For building the API.
- ESLint: For code linting and style enforcement.
- React: For building the frontend user interface.
- Zod: For runtime type validation.
-
exercises-9.1-9.7/: BMI and Exercise Calculatorsrc/: Contains TypeScript source filesbmiCalculator.ts: Logic for BMI calculationexerciseCalculator.ts: Logic for exercise statistics calculationindex.ts: Main server file with API endpoints
dist/: Contains compiled JavaScript files
-
exercises-9.8-9.14/: Initial Patientor Applicationpatientor-backend/src/: Backend source filesroutes/: API route handlersservices/: Business logictypes/: TypeScript type definitionsutils/: Utility functions
data/: JSON data files
patientor-frontend/src/: Frontend React components and logicpublic/: Static assets
-
exercises-9.15-9.20/: Flight Diary Applicationflight-diary-back/src/: Backend source filesroutes/: API endpointsservices/: Business logictypes/: Type definitions
data/: Flight diary entries
flight-diary-front/src/: React components and TypeScript filespublic/: Static assets
-
exercises-9.21-9.30/: Enhanced Patientor Applicationpatientor-backend/src/routes/: Enhanced API endpointsservices/: Extended business logictypes/: Advanced type definitionsutils/: Helper functions
data/: JSON data with extended entry types
patientor-frontend/src/components/: React componentstypes/: TypeScript interfacesservices/: API integration
public/: Static assets
This project is licensed under the MIT License. See the LICENSE file for details.