# Adding New Routes

## Dynamic Route Registration

The server uses an **auto-discovery system** for routes. No need to manually import routes in `index.ts`!

### How it Works

1. All files matching `*Routes.ts` in `src/routes/` are automatically discovered
2. Each route file creates a `/api/{routeName}` endpoint
3. Route name is derived from the filename (e.g., `userRoutes.ts` → `/api/user`)

### Adding a New Route

#### Step 1: Create a Controller
Create `src/controllers/UserController.ts`:
```typescript
import { Request, Response } from 'express';
import UserService from '../services/UserService';

export class UserController {
  async getAllUsers(req: Request, res: Response): Promise<void> {
    try {
      const users = await UserService.getAllUsers();
      res.json({ success: true, data: users });
    } catch (error: any) {
      res.status(500).json({ success: false, error: error.message });
    }
  }
  
  // ... other methods
}

export default new UserController();
```

#### Step 2: Create a Service
Create `src/services/UserService.ts`:
```typescript
import User from '../models/User';
import { IUser } from '../models/User';

export class UserService {
  async getAllUsers(): Promise<IUser[]> {
    return await User.find();
  }
  
  // ... other methods
}

export default new UserService();
```

#### Step 3: Create a Model
Create `src/models/User.ts`:
```typescript
import mongoose, { Document, Schema } from 'mongoose';

export interface IUser extends Document {
  firstName: string;
  lastName: string;
  email: string;
}

const userSchema = new Schema<IUser>({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true, unique: true }
});

export default mongoose.model<IUser>('User', userSchema);
```

#### Step 4: Create Routes File
Create `src/routes/userRoutes.ts`:
```typescript
import { Router } from 'express';
import UserController from '../controllers/UserController';

const router = Router();

router.get('/', UserController.getAllUsers.bind(UserController));
router.post('/', UserController.createUser.bind(UserController));
router.get('/:id', UserController.getUserById.bind(UserController));
router.put('/:id', UserController.updateUser.bind(UserController));
router.delete('/:id', UserController.deleteUser.bind(UserController));

export default router;
```

#### Step 5: Done! 🎉

The route is now automatically registered at `/api/user`

No changes needed to `index.ts`!

## Benefits

✅ **Scalability** - Add 100+ routes without modifying `index.ts`
✅ **Consistency** - All routes follow the same pattern
✅ **Maintainability** - Each route is self-contained
✅ **Auto-discovery** - New routes are automatically loaded
✅ **Clean Code** - No manual route imports

## Naming Convention

- Route file: `{resourceName}Routes.ts` (e.g., `userRoutes.ts`, `productRoutes.ts`)
- Endpoint: `/api/{resourceName}` (lowercase, auto-derived from filename)

## Current Routes

All routes starting with `/api/` are automatically discovered and loaded:

```
GET    /api/health                    - Health check
GET    /api/patients                  - Get all patients
GET    /api/patients/:id              - Get patient by ID
POST   /api/patients                  - Create patient
PUT    /api/patients/:id              - Update patient
DELETE /api/patients/:id              - Delete patient

GET    /api/doctors                   - Get all doctors
GET    /api/doctors/:id               - Get doctor by ID
GET    /api/doctors/specialization    - Get by specialization
POST   /api/doctors                   - Create doctor
PUT    /api/doctors/:id               - Update doctor
DELETE /api/doctors/:id               - Delete doctor

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