# Architecture Documentation

## Project Structure: MVC Layer Architecture

```
src/
├── models/              # Data models (Mongoose schemas)
│   ├── Patient.ts
│   ├── Doctor.ts
│   └── Appointment.ts
├── services/            # Business logic layer
│   ├── PatientService.ts
│   ├── DoctorService.ts
│   └── AppointmentService.ts
├── controllers/         # Request handlers
│   ├── PatientController.ts
│   ├── DoctorController.ts
│   └── AppointmentController.ts
├── routes/              # Route definitions
│   ├── patientRoutes.ts
│   ├── doctorRoutes.ts
│   └── appointmentRoutes.ts
└── index.ts             # Main application entry point
```

## Layer Responsibilities

### Models (`src/models/`)
- Define Mongoose schemas with TypeScript interfaces
- Handle data structure and validation
- Provide database models for services to use

### Services (`src/services/`)
- Implement business logic
- Handle database operations (CRUD)
- Provide reusable methods for controllers

### Controllers (`src/controllers/`)
- Handle HTTP requests and responses
- Call appropriate service methods
- Return formatted JSON responses

### Routes (`src/routes/`)
- Define API endpoints
- Map HTTP methods to controller methods
- Handle route parameters

## API Endpoints

### Patients
- `GET /api/patients` - Get all patients
- `GET /api/patients/:id` - Get patient by ID
- `POST /api/patients` - Create new patient
- `PUT /api/patients/:id` - Update patient
- `DELETE /api/patients/:id` - Delete patient

### Doctors
- `GET /api/doctors` - Get all doctors
- `GET /api/doctors/:id` - Get doctor by ID
- `GET /api/doctors/specialization?specialization=<value>` - Get doctors by specialization
- `POST /api/doctors` - Create new doctor
- `PUT /api/doctors/:id` - Update doctor
- `DELETE /api/doctors/:id` - Delete doctor

### Appointments
- `GET /api/appointments` - Get all appointments
- `GET /api/appointments/:id` - Get appointment by ID
- `GET /api/appointments/patient/:patientId` - Get appointments by patient
- `GET /api/appointments/doctor/:doctorId` - Get appointments by doctor
- `POST /api/appointments` - Create new appointment
- `PUT /api/appointments/:id` - Update appointment
- `DELETE /api/appointments/:id` - Delete appointment

## Data Flow

1. **Request** → Route receives HTTP request
2. **Route** → Directs to appropriate Controller method
3. **Controller** → Validates request, calls Service
4. **Service** → Executes business logic, queries Model
5. **Model** → Communicates with MongoDB
6. **Response** → Service returns data to Controller
7. **Response** → Controller formats and sends JSON response

## Benefits

✅ **Separation of Concerns** - Each layer has specific responsibility
✅ **Reusability** - Services can be used by multiple controllers
✅ **Testability** - Each layer can be tested independently
✅ **Maintainability** - Easy to locate and modify code
✅ **Scalability** - Easy to add new features with same pattern
