# E-Learning Platform Backend

Backend API for E-Learning Platform built with TypeScript, Express, TSOA, and Prisma.

## 🚀 Features

- **TypeScript** - Type-safe development
- **TSOA** - Automatic OpenAPI/Swagger documentation
- **Prisma** - Type-safe database ORM
- **JWT Authentication** - Secure user authentication
- **Role-based Authorization** - Admin, Instructor, Student roles
- **Multi-language Support** - Thai and English
- **File Upload** - Video and attachment support with MinIO/S3
- **Rate Limiting** - API protection
- **Comprehensive Error Handling** - Structured error responses

## 📋 Prerequisites

- Node.js >= 18
- Docker & Docker Compose
- PostgreSQL (via Docker)
- MinIO (via Docker)

## 🛠️ Setup

### 1. Install Dependencies

```bash
npm install
```

### 2. Environment Configuration

```bash
cp .env.example .env
# Edit .env with your configuration
```

### 3. Start Docker Services

```bash
docker compose up -d
```

### 4. Database Setup

```bash
# Generate Prisma client
npx prisma generate

# Run migrations
npx prisma migrate dev

# Seed database
npx prisma db seed
```

### 5. Generate TSOA Routes & Swagger

```bash
npm run tsoa:gen
```

### 6. Start Development Server

```bash
npm run dev
```

The server will start at `http://localhost:4000`

## 📚 API Documentation

Swagger documentation is available at: `http://localhost:4000/api-docs`

## 🏗️ Project Structure

```
Backend/
├── prisma/
│   ├── migrations/      # Database migrations
│   ├── schema.prisma    # Database schema
│   └── seed.js         # Database seeder
├── src/
│   ├── config/         # Configuration files
│   │   ├── index.ts    # Main config
│   │   ├── logger.ts   # Winston logger
│   │   └── database.ts # Prisma client
│   ├── controllers/    # TSOA controllers
│   │   └── HealthController.ts
│   ├── middleware/     # Express middleware
│   │   ├── authentication.ts
│   │   └── errorHandler.ts
│   ├── services/       # Business logic
│   ├── types/          # TypeScript types
│   │   └── index.ts
│   ├── utils/          # Utility functions
│   ├── validators/     # Input validation
│   ├── app.ts         # Express app setup
│   └── server.ts      # Server entry point
├── public/            # Generated Swagger docs
├── .env.example       # Environment template
├── tsconfig.json      # TypeScript config
├── tsoa.json         # TSOA config
├── nodemon.json      # Nodemon config
└── package.json
```

## 🔧 Available Scripts

```bash
npm run dev              # Start dev server with hot reload
npm run build            # Build TypeScript + generate TSOA routes
npm start                # Start production server
npm run tsoa:gen         # Generate TSOA routes & Swagger
npm test                 # Run tests
npm run lint            # Run ESLint
npm run format          # Format code with Prettier
```

## 🗄️ Database Commands

```bash
npx prisma studio       # Open Prisma Studio (GUI)
npx prisma migrate dev  # Create and apply migration
npx prisma db seed      # Seed database
npx prisma generate     # Generate Prisma client
```

## 🐳 Docker Commands

```bash
docker compose up -d    # Start all services
docker compose down     # Stop all services
docker compose logs -f  # View logs
docker compose ps       # List running services
```

## 🔐 Default Credentials

After seeding, you can login with:

- **Admin**: `admin` / `admin123`
- **Instructor**: `instructor` / `instructor123`
- **Student**: `student` / `student123`

## 📝 Development Workflow

1. Create a new controller in `src/controllers/`
2. Add TSOA decorators (`@Route`, `@Get`, `@Post`, etc.)
3. Run `npm run tsoa:gen` to generate routes
4. Implement business logic in `src/services/`
5. Test your endpoints

## 🧪 Testing

```bash
npm test                # Run all tests
npm run test:watch      # Run tests in watch mode
npm run test:coverage   # Generate coverage report
```

## 🚀 Deployment

See [Deployment Workflow](./.agent/workflows/deployment.md) for production deployment instructions.

## 📖 Documentation

- [Backend Development Rules](./.agent/rules/rules.md)
- [Agent Skills Backend](./agent_skills_backend.md)
- [API Workflows](./.agent/workflows/)

## 🔍 Troubleshooting

### Port already in use
```bash
lsof -i :4000
kill -9 <PID>
```

### Database connection error
```bash
docker compose logs postgres
npx prisma db pull
```

### Prisma client error
```bash
npx prisma generate
```

## 📄 License

ISC

## 👥 Team

E-Learning Development Team
