# E-Learning Backend Backend API for the E-Learning Platform built with Node.js, Express, Prisma, and PostgreSQL. ## ๐Ÿš€ Features - **Authentication & Authorization**: JWT-based authentication with role-based access control - **Course Management**: Create, manage, and publish courses with chapters and lessons - **Multi-Language Support**: Thai and English content support - **Quiz System**: Interactive quizzes with multiple attempts and score policies - **Progress Tracking**: Track student progress and issue certificates - **File Upload**: Support for video lessons and attachments (MinIO/S3) - **Caching**: Redis integration for improved performance - **Security**: Helmet, CORS, rate limiting, and input validation ## ๐Ÿ“‹ Prerequisites - Node.js >= 18.0.0 - PostgreSQL >= 14 - Redis (optional, for caching) - MinIO or S3 (for file storage) ## ๐Ÿ› ๏ธ Installation 1. **Clone the repository** ```bash git clone cd e-learning/Backend ``` 2. **Install dependencies** ```bash npm install ``` 3. **Setup environment variables** ```bash cp .env.example .env # Edit .env with your configuration ``` 4. **Start Docker services** (PostgreSQL, Redis, MinIO) ```bash docker compose up -d ``` 5. **Run database migrations** ```bash npx prisma migrate dev ``` 6. **Seed the database** ```bash npm run prisma:seed ``` 7. **Start development server** ```bash npm run dev ``` The server will start on `http://localhost:4000` ## ๐Ÿ“ Project Structure ``` Backend/ โ”œโ”€โ”€ prisma/ โ”‚ โ”œโ”€โ”€ migrations/ # Database migrations โ”‚ โ”œโ”€โ”€ schema.prisma # Database schema โ”‚ โ””โ”€โ”€ seed.js # Database seeding โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ config/ # Configuration files โ”‚ โ”‚ โ”œโ”€โ”€ database.js # Prisma client โ”‚ โ”‚ โ”œโ”€โ”€ logger.js # Winston logger โ”‚ โ”‚ โ””โ”€โ”€ redis.js # Redis client โ”‚ โ”œโ”€โ”€ controllers/ # Request handlers โ”‚ โ”œโ”€โ”€ middleware/ # Express middleware โ”‚ โ”‚ โ”œโ”€โ”€ auth.js # Authentication & authorization โ”‚ โ”‚ โ””โ”€โ”€ errorHandler.js โ”‚ โ”œโ”€โ”€ routes/ # Route definitions โ”‚ โ”œโ”€โ”€ services/ # Business logic โ”‚ โ”œโ”€โ”€ utils/ # Utility functions โ”‚ โ”‚ โ””โ”€โ”€ jwt.js # JWT utilities โ”‚ โ”œโ”€โ”€ validators/ # Input validation โ”‚ โ”œโ”€โ”€ app.js # Express app setup โ”‚ โ””โ”€โ”€ server.js # Server entry point โ”œโ”€โ”€ tests/ # Test files โ”œโ”€โ”€ logs/ # Log files โ”œโ”€โ”€ .env.example # Environment variables template โ”œโ”€โ”€ package.json โ””โ”€โ”€ README.md ``` ## ๐Ÿ”‘ Environment Variables See `.env.example` for all available environment variables. Key variables: - `DATABASE_URL`: PostgreSQL connection string - `JWT_SECRET`: Secret key for JWT tokens - `REDIS_URL`: Redis connection string - `S3_ENDPOINT`: MinIO/S3 endpoint - `CORS_ORIGIN`: Allowed CORS origins ## ๐Ÿงช Testing ```bash # Run all tests npm test # Run tests in watch mode npm run test:watch # Run tests with coverage npm test -- --coverage ``` ## ๐Ÿ“ API Documentation ### Authentication - `POST /api/auth/register` - Register new user - `POST /api/auth/login` - Login user - `GET /api/auth/me` - Get current user profile - `POST /api/auth/logout` - Logout user ### Health Check - `GET /health` - Server health status ## ๐Ÿ” Default Credentials After seeding the database, you can use these credentials: - **Admin**: `admin` / `admin123` - **Instructor**: `instructor` / `admin123` - **Student**: `student` / `admin123` ## ๐Ÿ› ๏ธ Development ```bash # Start dev server with auto-reload npm run dev # Run linter npm run lint # Format code npm run format # Open Prisma Studio (database GUI) npm run prisma:studio ``` ## ๐Ÿ“ฆ Database Commands ```bash # Generate Prisma Client npm run prisma:generate # Create migration npx prisma migrate dev --name migration_name # Apply migrations npx prisma migrate deploy # Reset database (development only) npx prisma migrate reset # Seed database npm run prisma:seed ``` ## ๐Ÿš€ Production Deployment 1. Set `NODE_ENV=production` 2. Set strong `JWT_SECRET` 3. Configure production database 4. Run migrations: `npx prisma migrate deploy` 5. Start with PM2: `pm2 start src/server.js` ## ๐Ÿ“š Resources - [Prisma Documentation](https://www.prisma.io/docs) - [Express Documentation](https://expressjs.com/) - [JWT Documentation](https://jwt.io/) ## ๐Ÿ“„ License MIT