# database1111 **Repository Path**: roxy3203325037/database1111 ## Basic Information - **Project Name**: database1111 - **Description**: No description available - **Primary Language**: SQL - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-14 - **Last Updated**: 2025-10-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ๐Ÿฅ Hospital Management System ๐Ÿš€ **Database Final Project โ€” Our Completed Implementation** We developed a fully functional **Hospital Management System** from scratch, designed to manage patients, medical staff, appointments, treatments, medical records, and billing operations. We chose the hospital theme and implemented every component with realistic data and complete functionality. --- ## ๐Ÿ“‘ Table of Contents - [๐Ÿ‘ฅ Our Team](#-our-team) - [๐Ÿฉบ What We Built](#-what-we-built) - [๐ŸŒŸ Bonus Features (+10%)](#-bonus-features-10) - [๐Ÿงฑ Database Design & Implementation (50%)](#-database-design--implementation-50) - [๐Ÿ’ป Application Development (35%)](#-application-development-35) - [๐ŸŒ REST API (10%)](#-rest-api-10) - [๐Ÿงพ Documentation (5%)](#-documentation-5) - [โš™๏ธ Prerequisites](#๏ธ-prerequisites) - [๐Ÿš€ Installation & Setup](#-installation--setup) - [๐Ÿงญ Usage](#-usage) - [๐Ÿ“‚ Project Structure](#-project-structure) - [โœจ Summary](#-summary) --- ## ๐Ÿ‘ฅ Our Team | English Name | Chinese Name | Student ID | |--------------|--------------|------------| | Leo | ๅ‘จ้€ธๆœ— | 2023905934 | | Leo | ๅดๆ€่ฟœ | 2023905932 | | Alex | ็‰›่ฟœๅ…ฎ | 2023903990 | | Charles | ้™ˆ้Ÿฌๅฎ‡ | 2023900902 | --- ### Document Navigation --- ## ๐Ÿฉบ What We Built We created a comprehensive hospital management database system featuring: - A **PostgreSQL** backend with over 10 interrelated tables - A **Python** application providing both GUI (Tkinter) and REST API (FastAPI) interfaces - Complete **CRUD operations**, **complex SQL queries**, and **data integrity enforcement** - Extra features such as authentication, logging, Docker deployment, and testing Our system enables administrators and staff to efficiently register patients, manage appointments, view records, handle billing, and analyze hospital operations. --- ## ๐ŸŒŸ Bonus Features (+10%) We went beyond the base requirements and implemented all optional enhancements: - ๐Ÿ” **Authentication** โ€“ Added JWT-based API authentication for secure access - ๐Ÿชต **Logging** โ€“ Implemented centralized logging of requests, operations, and SQL queries (`logs/app.log`) - ๐Ÿ‹ **Dockerization** โ€“ Fully containerized both the database and application (`docker-compose.yml`) - ๐Ÿงช **Testing** โ€“ Wrote unit tests for CRUD and query logic using `pytest` These features enhanced **security**, **debugging**, and **deployment reliability**. --- ## ๐Ÿงฑ Database Design & Implementation (50%) We designed and implemented a complete ER model with **10+ normalized tables**. ### Entities - Patients - Medical Staff - Departments - Appointments - Medical Records - Prescriptions - Medications - Treatments - Invoices - Rooms ### Relationships - One-to-Many: Department โ†’ Medical Staff, Patient โ†’ Appointments - Many-to-Many: Appointments โ†” Treatments, Prescriptions โ†” Medications - Flexible foreign key constraints for realistic design We ensured strong data integrity, normalization, and realistic hospital workflows. ### ๐Ÿงฉ Constraints & Advanced SQL We implemented: - โœ… **Primary / Foreign Keys** - โœ… **5+ CHECK constraints** (e.g., valid ages, appointment dates) - โœ… **3+ UNIQUE constraints** (e.g., email, phone, room number) - โœ… **DEFAULT** and **NOT NULL** settings - โœ… **3+ Triggers** โ€“ e.g., auto-update room status, prevent overlapping appointments - โœ… **3+ Stored Functions** โ€“ for validation and calculations - โœ… **Indexes** โ€“ for optimized query performance We inserted **20+ rows per table** of realistic sample data. --- ## ๐Ÿ’ป Application Development (35%) ### ๐Ÿ”น Technology Stack - **Language:** Python - **Database Access:** `psycopg3` (no ORM used) - **Frontend:** Tkinter GUI - **Backend:** FastAPI REST API ### ๐Ÿ”น CRUD Implementation We implemented full CRUD functionality for: **Patients**, **Appointments**, **Medical Records**, **Prescriptions**, and **Invoices** Each supports: - **Create** โ€“ Add new records with validation - **Read** โ€“ Retrieve data by ID, search, or filter - **Update** โ€“ Safely modify records - **Delete** โ€“ Cascade or soft-delete with integrity maintained All operations used **prepared statements**, **transactions**, and **robust error handling**. ### ๐Ÿ”น Complex SQL Queries We built advanced SQL queries to: - List all appointments with patient, doctor, and department details - Calculate total revenue per department - Identify doctors with the most appointments - Retrieve patients with unpaid invoices --- ## ๐ŸŒ REST API (10%) We designed and documented a RESTful API with endpoints for key entities. Each endpoint returns structured JSON with proper HTTP codes. ### Example Endpoints ``` GET /api/patients โ†’ Retrieve all patients POST /api/patients โ†’ Create a new patient PUT /api/patients/{id} โ†’ Update patient details DELETE /api/patients/{id} โ†’ Remove a patient ``` Additional routes: ``` GET /api/appointments/available?date=2025-10-10 โ†’ Query available slots GET /api/patients/search?name=John โ†’ Filter patients by name ``` Full API documentation is available in: ``` docs/API_documentation.md ``` --- ## ๐Ÿงพ Documentation (5%) We prepared complete project documentation, including: - **README.md** (this file) โ€“ Setup, structure, and usage instructions - **Architecture Diagram** โ€“ `docs/architecture.png` - **ER Diagram** โ€“ `docs/ER_diagram.png` - **SQL Scripts:** ``` sql/ โ”œโ”€โ”€ 01_create_tables.sql โ”œโ”€โ”€ 02_insert_sample_data.sql โ”œโ”€โ”€ 03_create_constraints.sql โ””โ”€โ”€ 04_create_triggers_functions.sql ``` --- ## โš™๏ธ Prerequisites ``` PostgreSQL 17+ Python 3.10+ Libraries: psycopg3, fastapi, uvicorn, pydantic, tkinter, pytest Docker (optional for containerized setup) ``` --- ## ๐Ÿš€ Installation & Setup ### ๐Ÿ—„๏ธ Database Setup ```bash createdb hospital_db psql -d hospital_db -f sql/01_create_tables.sql psql -d hospital_db -f sql/02_insert_sample_data.sql pip install -r requirements.txt python src/main.py # Launch GUI # Or run API: uvicorn src.api.main:app --reload # Or with Docker: docker-compose up -d ``` ### ๐Ÿ”— Access Points ``` Database โ†’ localhost:5432 API โ†’ localhost:8000 ``` --- ## ๐Ÿงญ Usage - **GUI:** Manage patients, appointments, and billing via a user-friendly interface - **API:** Interact via HTTP endpoints or Swagger UI at: `http://localhost:8000/docs` - **Testing:** Run unit tests with: ```bash pytest tests/ ``` --- ## ๐Ÿ“‚ Project Structure ``` โ”œโ”€โ”€ sql/ # SQL scripts (table creation, triggers, sample data) โ”‚ โ”œโ”€โ”€ 01_create_tables.sql โ”‚ โ”œโ”€โ”€ 02_insert_sample_data.sql โ”‚ โ””โ”€โ”€ 03_triggers_functions.sql โ”œโ”€โ”€ logs/ # Log files for backend and frontend โ”‚ โ”œโ”€โ”€ backend.log โ”‚ โ””โ”€โ”€ frontend.log โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ db_setup.py # Initializes database, executes SQL files, provides CRUD tests โ”‚ โ”œโ”€โ”€ run_server.py # Launches Flask backend server โ”‚ โ”œโ”€โ”€ run_client.py # Launches Tkinter frontend client โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ backend/ # Flask backend application โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py # Flask app factory: register blueprints, configure logging โ”‚ โ”‚ โ”œโ”€โ”€ models/ # Data access layer (PostgreSQL interaction) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ db.py # Database connection and query utilities โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ *model.py # Models for entities (patients, invoices, medications, staff, etc.) โ”‚ โ”‚ โ”œโ”€โ”€ api/ # RESTful API blueprints โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ admissions.py # Admission management API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ appointments.py # Appointment management API (create, cancel, query) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ assignments.py # Staff-patient assignment API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ departments.py # Department management API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ diseases.py # Disease information API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ invoices.py # Invoice and billing API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ medications.py # Medication management API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ patients.py # Patient management API (includes invoices and appointments) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ prescriptions.py # Prescription management API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ rooms.py # Room management API โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ staff.py # Medical staff management API โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ users.py # User login and registration API โ”‚ โ”‚ โ””โ”€โ”€ utils/ # Backend utility modules โ”‚ โ”‚ โ””โ”€โ”€ password.py # Password hashing and verification โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ frontend/ # Tkinter desktop client โ”‚ โ”‚ โ”œโ”€โ”€ main_app.py # Main application logic (login, navigation, API calls) โ”‚ โ”‚ โ”œโ”€โ”€ api_client.py # HTTP client wrapper for backend API requests โ”‚ โ”‚ โ””โ”€โ”€ views/ # GUI views for different modules โ”‚ โ”‚ โ”œโ”€โ”€ patient_view.py # Patient management view โ”‚ โ”‚ โ”œโ”€โ”€ staff_view.py # Staff management view โ”‚ โ”‚ โ”œโ”€โ”€ appointment_view.py # Appointment view โ”‚ โ”‚ โ”œโ”€โ”€ department_view.py # Department view โ”‚ โ”‚ โ”œโ”€โ”€ invoice_view.py # Invoice view โ”‚ โ”‚ โ”œโ”€โ”€ user_view.py # User management view โ”‚ โ”‚ โ”œโ”€โ”€ admission_view.py # Admission view โ”‚ โ”‚ โ”œโ”€โ”€ disease_view.py # Disease view โ”‚ โ”‚ โ””โ”€โ”€ medication_view.py # Medication view โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ tools/ # Maintenance and testing tools โ”‚ โ”œโ”€โ”€ migrate_passwords.py # Password migration/encryption update tool โ”‚ โ”œโ”€โ”€ reset_sequences.py # Resets PostgreSQL sequence IDs after data import โ”‚ โ”œโ”€โ”€ health_check.py # System health check (dependencies, DB, ports) โ”‚ โ””โ”€โ”€ unit_test.py # Automated backend/frontend integration testing โ”‚ โ””โ”€โ”€ README.md # Project documentation (this file) ``` --- # database1111-master โ€” Docker Usage Instructions This repository includes Docker support for local development (`Dockerfile`, `docker-compose.yml`, `start.sh`) and two helper scripts for pushing the image to a registry or exporting it as a compressed file for sharing with others. ## Quick Start 1. ย **Start the services**: ```bash cd "/Volumes/WD Blue SN5000 Media/paid/database1111-master/database1111-master5/database1111-master" docker compose up --build -d ``` 2. ย **View logs/status**: ```bash docker compose ps docker compose logs -f web ``` ## Share Image 1. ย **Export and compress the image**: ```bash chmod +x ./scripts/save_image.sh mkdir -p dist ./scripts/save_image.sh database1111-master-web:latest dist/db1111_web.tar.gz ``` 2. ย **Send the file** `dist/db1111_web.tar.gz` to the recipient (via scp/cloud drive/USB). 3. ย **Recipient loads the image and runs the container**: ```bash # On the recipient's machine gunzip -c db1111_web.tar.gz | docker load # Or directly docker load < db1111_web.tar.gz docker images docker run -p 5001:5000 ``` --- ## โœจ Summary Our **Hospital Management System** demonstrates a complete understanding of **relational database design**, **SQL programming**, and **application integration**. We delivered a project with both **technical depth** and **real-world usability**, achieving a robust and professional-grade system. ---