2026-01-09 06:28:15 +00:00
|
|
|
# E-Learning Platform Backend
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
Backend API for E-Learning Platform built with TypeScript, Express, TSOA, and Prisma.
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
## 🚀 Features
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
- **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
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
## 📋 Prerequisites
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
- Node.js >= 18
|
|
|
|
|
- Docker & Docker Compose
|
|
|
|
|
- PostgreSQL (via Docker)
|
|
|
|
|
- MinIO (via Docker)
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🛠️ Setup
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 1. Install Dependencies
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
npm install
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 2. Environment Configuration
|
|
|
|
|
|
2026-01-08 06:51:33 +00:00
|
|
|
```bash
|
|
|
|
|
cp .env.example .env
|
|
|
|
|
# Edit .env with your configuration
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 3. Start Docker Services
|
|
|
|
|
|
2026-01-08 06:51:33 +00:00
|
|
|
```bash
|
|
|
|
|
docker compose up -d
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 4. Database Setup
|
|
|
|
|
|
2026-01-08 06:51:33 +00:00
|
|
|
```bash
|
2026-01-09 06:28:15 +00:00
|
|
|
# Generate Prisma client
|
|
|
|
|
npx prisma generate
|
|
|
|
|
|
|
|
|
|
# Run migrations
|
2026-01-08 06:51:33 +00:00
|
|
|
npx prisma migrate dev
|
2026-01-09 06:28:15 +00:00
|
|
|
|
|
|
|
|
# Seed database
|
|
|
|
|
npx prisma db seed
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 5. Generate TSOA Routes & Swagger
|
|
|
|
|
|
2026-01-08 06:51:33 +00:00
|
|
|
```bash
|
2026-01-09 06:28:15 +00:00
|
|
|
npm run tsoa:gen
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### 6. Start Development Server
|
|
|
|
|
|
2026-01-08 06:51:33 +00:00
|
|
|
```bash
|
|
|
|
|
npm run dev
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
The server will start at `http://localhost:4000`
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 📚 API Documentation
|
|
|
|
|
|
|
|
|
|
Swagger documentation is available at: `http://localhost:4000/api-docs`
|
|
|
|
|
|
|
|
|
|
## 🏗️ Project Structure
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Backend/
|
|
|
|
|
├── prisma/
|
2026-01-09 06:28:15 +00:00
|
|
|
│ ├── migrations/ # Database migrations
|
|
|
|
|
│ ├── schema.prisma # Database schema
|
|
|
|
|
│ └── seed.js # Database seeder
|
2026-01-08 06:51:33 +00:00
|
|
|
├── src/
|
2026-01-09 06:28:15 +00:00
|
|
|
│ ├── 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
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🔧 Available Scripts
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
```bash
|
2026-01-09 06:28:15 +00:00
|
|
|
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
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🗄️ Database Commands
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
```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
|
|
|
|
|
```
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🐳 Docker Commands
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
```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
|
|
|
|
|
```
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
## 🔐 Default Credentials
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
After seeding, you can login with:
|
2026-01-08 06:51:33 +00:00
|
|
|
|
|
|
|
|
- **Admin**: `admin` / `admin123`
|
2026-01-09 06:28:15 +00:00
|
|
|
- **Instructor**: `instructor` / `instructor123`
|
|
|
|
|
- **Student**: `student` / `student123`
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 📝 Development Workflow
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
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
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🧪 Testing
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
```bash
|
|
|
|
|
npm test # Run all tests
|
|
|
|
|
npm run test:watch # Run tests in watch mode
|
|
|
|
|
npm run test:coverage # Generate coverage report
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🚀 Deployment
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
See [Deployment Workflow](./.agent/workflows/deployment.md) for production deployment instructions.
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 📖 Documentation
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
- [Backend Development Rules](./.agent/rules/rules.md)
|
|
|
|
|
- [Agent Skills Backend](./agent_skills_backend.md)
|
|
|
|
|
- [API Workflows](./.agent/workflows/)
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 🔍 Troubleshooting
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### Port already in use
|
|
|
|
|
```bash
|
|
|
|
|
lsof -i :4000
|
|
|
|
|
kill -9 <PID>
|
2026-01-08 06:51:33 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### Database connection error
|
|
|
|
|
```bash
|
|
|
|
|
docker compose logs postgres
|
|
|
|
|
npx prisma db pull
|
|
|
|
|
```
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
### Prisma client error
|
|
|
|
|
```bash
|
|
|
|
|
npx prisma generate
|
|
|
|
|
```
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 📄 License
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
ISC
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
## 👥 Team
|
2026-01-08 06:51:33 +00:00
|
|
|
|
2026-01-09 06:28:15 +00:00
|
|
|
E-Learning Development Team
|