# 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 ``` ### 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