# Student Management System

A full-stack student management application built with Express.js (backend) and React (frontend). This system allows admins to manage students, courses, and view reports, while teachers can only view the dashboard.

## Features

- **Authentication**: Register and login with role-based access (admin/teacher)
- **Student Management**: Create, read, update, and delete students (admin only)
- **Course Management**: Create, read, update, and delete courses (admin only)
- **Dashboard**: View system reports (total students, courses, averages)
- **Role-based Access Control**: Different features for admin and teacher roles

## Prerequisites

- Node.js (v14+)
- MySQL/MariaDB server running
- npm

## Installation

### 1. Navigate to the project directory

```bash
cd student-management-system
```

### 2. Backend Setup

```bash
cd backend

# Install dependencies
npm install
```

### 3. Frontend Setup

```bash
cd ../frontend

# Install dependencies
npm install
```

## Running the Application

### Start Backend Server

```bash
cd backend
npm run dev
```

The backend will run on `http://localhost:3000`

### Start Frontend Development Server

In a new terminal:

```bash
cd frontend
npm run dev
```

The frontend will run on `http://localhost:5173`

### Database Initialization

The database is automatically initialized when the backend server starts. It creates the required tables:
- `users` - for authentication
- `courses` - for course management
- `students` - for student records

## Usage

### Admin Workflows

1. **Register**: Create an admin account on the Register page
2. **Login**: Log in with your admin credentials
3. **Manage Students**: 
   - View all students on the Students page
   - Click "Create Student" to add new students
   - Click "Edit" on a student row to update their information
   - Click "Delete" to remove a student
4. **Manage Courses**:
   - View all courses on the Courses page
   - Click "Create Course" to add new courses
   - Click "Edit" on a course to update its details
   - Click "Delete" to remove a course
5. **View Dashboard**: See system statistics and reports

### Teacher Workflows

1. **Register**: Create a teacher account on the Register page
2. **Login**: Log in with your teacher credentials
3. **View Dashboard**: Teachers can only view the dashboard with system statistics

## API Endpoints

### Authentication Endpoints

- `POST /api/auth/register` - Register a new user
- `POST /api/auth/login` - Login user
- `GET /api/auth/logout` - Logout user
- `GET /api/auth/check` - Check current authentication status

### Student Endpoints (Admin Only)

- `GET /api/students/all` - Get all students
- `GET /api/students/:id` - Get a specific student
- `POST /api/students/create` - Create a new student
- `PUT /api/students/update/:id` - Update a student
- `DELETE /api/students/delete/:id` - Delete a student

### Course Endpoints (Admin Only)

- `GET /api/courses/all` - Get all courses
- `GET /api/courses/:id` - Get a specific course
- `POST /api/courses/create` - Create a new course
- `PUT /api/courses/update/:id` - Update a course
- `DELETE /api/courses/delete/:id` - Delete a course

### Report Endpoints

- `GET /api/report` - Get dashboard statistics

## Database Schema

### Users Table
```sql
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'teacher') DEFAULT 'teacher',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

### Courses Table
```sql
CREATE TABLE courses (
    id INT PRIMARY KEY AUTO_INCREMENT,
    course_name VARCHAR(100) NOT NULL,
    duration INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

### Students Table
```sql
CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    gender ENUM('male', 'female') NOT NULL,
    email VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    course_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE
);
```

## Technology Stack

### Backend
- Express.js - Web framework
- MySQL2 - Database driver
- JWT - Authentication
- bcryptjs - Password hashing
- cors - Cross-origin resource sharing
- dotenv - Environment variables

### Frontend
- React - UI framework
- Vite - Build tool
- React Router - Client-side routing
- Zustand - State management
- Axios - HTTP client
- React Hot Toast - Notifications

## Development Notes

- Backend uses JWT tokens stored in HTTP-only cookies
- Frontend automatically checks authentication on load
- All admin routes are protected and redirect to login if unauthorized
- Frontend stores application state using Zustand
- API calls are made through a centralized axios instance

## Troubleshooting

### Database Connection Failed
- Ensure MySQL/MariaDB is running
- Verify credentials in backend `.env` file
- Check that the `student_management` database exists

### CORS Errors
- Backend is configured to accept requests from `http://localhost:5173`
- Ensure both frontend and backend are running

### Port Already in Use
- Backend defaults to port 3000
- Frontend defaults to port 5173
- Modify the PORT in backend `.env` if needed

## License

ISC

## Features (continued)

- Add, edit, delete students
- Track name, email, grade, age
- JSON-based storage

## Tech Stack

- Backend: Express.js
- Frontend: React, Vite, Zustand