Sql_Adv_Proj

πŸ“š Examination System - Backend API Documentation

Table of Contents


Overview

This is a comprehensive Examination Management System backend API built with Node.js and Express. The system manages students, instructors, courses, exams, and tracks within an educational institution. It uses Microsoft SQL Server as the database with Sequelize ORM for database interactions.

Key Features


Tech Stack

Technology Version Purpose
Node.js Latest Runtime Environment
Express ^5.2.1 Web Framework
Sequelize ^6.37.7 ORM for SQL Server
Tedious ^19.2.0 MS SQL Server Driver
express-validator ^7.3.1 Request Validation
Morgan ^1.10.1 HTTP Request Logger
dotenv ^17.2.3 Environment Variables
Nodemon ^3.1.11 Development Server (dev)

Project Structure

Back_sql_project/
β”œβ”€β”€ index.js                    # Application entry point
β”œβ”€β”€ package.json                # Dependencies and scripts
β”œβ”€β”€ .env                        # Environment variables (not in repo)
β”œβ”€β”€ .gitignore                  # Git ignore rules
β”‚
β”œβ”€β”€ Auth/
β”‚   └── auth.js                 # Authentication routes
β”‚
β”œβ”€β”€ Configuration/
β”‚   └── Sequelize.js            # Database configuration
β”‚
β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ Courses.js              # Course controller logic
β”‚   β”œβ”€β”€ Exams.js                # Exam controller logic
β”‚   β”œβ”€β”€ instructors.js          # Instructor controller logic
β”‚   β”œβ”€β”€ Students.js             # Student auth controller
β”‚   └── StudentsController.js   # Student operations controller
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ init-models.js          # Model initialization & associations
β”‚   β”œβ”€β”€ choice.js               # Choice model
β”‚   β”œβ”€β”€ course.js               # Course model
β”‚   β”œβ”€β”€ department.js           # Department model
β”‚   β”œβ”€β”€ exam.js                 # Exam model
β”‚   β”œβ”€β”€ examquestion.js         # Exam-Question junction model
β”‚   β”œβ”€β”€ instructor.js           # Instructor model
β”‚   β”œβ”€β”€ question.js             # Question model
β”‚   β”œβ”€β”€ student.js              # Student model
β”‚   β”œβ”€β”€ studentquestionanswer.js # Student answers model
β”‚   β”œβ”€β”€ topic.js                # Topic model
β”‚   └── track.js                # Track model
β”‚
β”œβ”€β”€ Routes/
β”‚   β”œβ”€β”€ Courses.js              # Course routes
β”‚   β”œβ”€β”€ ExamsRoute.js           # Exam routes (placeholder)
β”‚   β”œβ”€β”€ instructor.js           # Instructor routes
β”‚   └── StudentRoute.js         # Student routes
β”‚
└── validators/
    β”œβ”€β”€ coursesValidators.js            # Course validation schemas
    β”œβ”€β”€ validationSchemaForExams.js     # Exam validation schemas
    β”œβ”€β”€ validationSchemaForInstructors.js # Instructor validation schemas
    └── ValidationSchemaForStudent.js   # Student validation schemas

Getting Started

Prerequisites

Installation

  1. Clone the repository
    git clone <repository-url>
    cd Back_sql_project
    
  2. Install dependencies
    npm install
    
  3. Configure environment variables

    Create a .env file in the root directory:

    PORT=3000
    API_VERSION=api/v1
    DB_NAME=your_database_name
    DB_USER=your_database_user
    DB_PASSWORD=your_database_password
    DB_HOST=localhost
    
  4. Restore the database

    Use one of the provided backup files:

    • dbafterstored.bak
    • dbveido.bak
  5. Start the server
    # Development mode (with hot reload)
    npm run dev
    
    # Production mode
    npm start
    
  6. Verify the server is running
    Server running at: http://localhost:3000
    

Environment Variables

Variable Description Default
PORT Server port number 3000
API_VERSION API version prefix api/v1
DB_NAME SQL Server database name -
DB_USER Database username -
DB_PASSWORD Database password -
DB_HOST Database host address -

Database Schema

Entity Relationship Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Department β”‚       β”‚    Topic    β”‚       β”‚   Choice    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ departmentidβ”‚       β”‚ topicid (PK)β”‚       β”‚ choiceid(PK)β”‚
β”‚ departmentnameβ”‚     β”‚ topicname   β”‚       β”‚ choicelabel β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β”‚ choicetext  β”‚
       β”‚                     β”‚              β”‚ questionid  β”‚
       β”‚                     β”‚              β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β–Ό                     β–Ό                     β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚    Track    β”‚       β”‚   Course    β”‚              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€              β”‚
β”‚ trackid(PK) β”‚       β”‚ courseid(PK)β”‚              β”‚
β”‚ trackname   β”‚       β”‚ coursename  β”‚              β”‚
β”‚ deptid (FK) β”‚       β”‚instructorid β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β”‚ topicid(FK) β”‚              β”‚
       β”‚              β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜              β”‚
       β”‚                     β”‚                     β”‚
       β–Ό                     β–Ό                     β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
β”‚   Student   β”‚       β”‚  Instructor β”‚       β”‚  Question   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚studentid(PK)β”‚       β”‚instructorid β”‚       β”‚questionid(PK)β”‚
β”‚ studentname β”‚       β”‚instructornameβ”‚      β”‚ questiontextβ”‚
β”‚ email       β”‚       β”‚ email       β”‚       β”‚ questiontypeβ”‚
β”‚ trackid(FK) β”‚       β”‚ deptid(FK)  β”‚       β”‚correctanswerβ”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚ degree      β”‚
       β”‚                                     β”‚ courseid(FK)β”‚
       β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚              β”‚    Exam     β”‚              β”‚
       β”‚              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€              β”‚
       β”‚              β”‚ examid(PK)  β”‚              β”‚
       └──────────────│ studentid   β”‚β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ courseid    β”‚
                      β”‚ grade       β”‚
                      β”‚ examdate    β”‚
                      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β–Ό              β–Ό              β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚ExamQuestion β”‚ β”‚StudentAnswerβ”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
       β”‚ examid(PK)  β”‚ β”‚ examid(PK)  β”‚
       β”‚questionid(PK)β”‚ β”‚studentid(PK)β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚questionid(PK)β”‚
                       β”‚studentanswerβ”‚
                       β”‚ mark        β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Models Description

Model Table Description
Student student Students enrolled in tracks
Instructor instructor Teaching staff assigned to departments
Course course Courses taught by instructors
Department department Academic departments
Track track Learning tracks within departments
Topic topic Course topics/categories
Exam exam Exams taken by students
Question question Exam questions with answers
Choice choice Multiple choice options for questions
ExamQuestion examquestion Junction table linking exams to questions
StudentQuestionAnswer studentquestionanswer Student responses to exam questions

API Endpoints

Base URL

http://localhost:3000/api/v1

Authentication

Register Student

Creates a new student account.

POST /api/v1/auth/register

Request Body:

Field Type Required Description
studentname string βœ… Student’s full name (min 3 chars)
email string βœ… Valid email address
password string βœ… Password (min 6 chars)
trackid integer ❌ Track ID to assign student

Example Request:

{
  "studentname": "John Doe",
  "email": "john.doe@example.com",
  "password": "securePass123",
  "trackid": 1
}

Success Response (201):

{
  "success": true,
  "data": "Student registered successfully"
}

Error Response (400):

{
  "success": false,
  "data": [
    {
      "msg": "studentname must be at least 3 characters long",
      "param": "studentname",
      "location": "body"
    }
  ]
}

Login Student

Authenticates a student and returns their data with enrolled courses.

POST /api/v1/auth/login

Request Body:

Field Type Required Description
email string βœ… Registered email address
password string βœ… Account password (min 6 chars)

Example Request:

{
  "email": "john.doe@example.com",
  "password": "securePass123"
}

Success Response (200):

{
  "success": true,
  "data": {
    "student": {
      "studentid": 1,
      "studentname": "John Doe",
      "email": "john.doe@example.com",
      "trackid": 1
    },
    "courses": [
      {
        "courseid": 1,
        "coursename": "Introduction to SQL"
      }
    ]
  }
}

Register Instructor

Creates a new instructor account.

POST /api/v1/auth/instructor/register

Request Body:

Field Type Required Description
instructorname string βœ… Instructor’s full name (min 3 chars)
email string βœ… Valid email address
password string βœ… Password (min 6 chars)
deptid integer ❌ Department ID

Example Request:

{
  "instructorname": "Dr. Jane Smith",
  "email": "jane.smith@university.edu",
  "password": "profPass123",
  "deptid": 2
}

Success Response (201):

{
  "success": true,
  "data": "Instructor registered successfully"
}

Login Instructor

Authenticates an instructor and returns their data with assigned courses.

POST /api/v1/auth/instructor/login

Request Body:

Field Type Required Description
email string βœ… Registered email address
password string βœ… Account password (min 6 chars)

Example Request:

{
  "email": "jane.smith@university.edu",
  "password": "profPass123"
}

Success Response (200):

{
  "success": true,
  "data": {
    "InstructorData": {
      "instructorid": 1,
      "instructorname": "Dr. Jane Smith",
      "email": "jane.smith@university.edu",
      "deptid": 2
    },
    "courses": [
      {
        "courseid": 1,
        "coursename": "Advanced Database Systems"
      }
    ],
    "track": {
      "trackid": 1,
      "trackname": "Backend Development"
    }
  }
}

Courses

Get All Courses (Paginated)

Retrieves a paginated list of all courses.

GET /api/v1/course

Query Parameters:

Parameter Type Required Default Description
page integer ❌ 1 Page number (min: 1)
size integer ❌ 10 Items per page (min: 1)

Example Request:

GET /api/v1/course?page=1&size=10

Success Response (200):

{
  "success": true,
  "data": [
    {
      "courseid": 1,
      "coursename": "Introduction to SQL",
      "instructorid": 1,
      "topicid": 1
    },
    {
      "courseid": 2,
      "coursename": "Advanced Node.js",
      "instructorid": 2,
      "topicid": 2
    }
  ]
}

Get Course by ID

Retrieves a specific course by its ID.

GET /api/v1/course/:id

Path Parameters:

Parameter Type Description
id integer Course ID

Example Request:

GET /api/v1/course/1

Success Response (200):

{
  "success": true,
  "data": [
    {
      "courseid": 1,
      "coursename": "Introduction to SQL",
      "instructorid": 1,
      "topicid": 1
    }
  ]
}

Create Course

Creates a new course.

POST /api/v1/course

Request Body:

Field Type Required Description
coursename string βœ… Course name (min 3 chars)
instructorid integer ❌ Instructor ID
topicid integer ❌ Topic ID

Example Request:

{
  "coursename": "Machine Learning Basics",
  "instructorid": 3,
  "topicid": 5
}

Success Response (201):

{
  "success": true,
  "data": [
    {
      "courseid": 10
    }
  ]
}

Update Course

Updates an existing course.

PATCH /api/v1/course

Request Body:

Field Type Required Description
courseid integer βœ… Course ID to update
coursename string βœ… New course name
instructorid integer βœ… New instructor ID
topicid integer βœ… New topic ID

Example Request:

{
  "courseid": 1,
  "coursename": "Advanced SQL Techniques",
  "instructorid": 2,
  "topicid": 1
}

Success Response (200):

{
  "success": true,
  "data": "Course updated successfully"
}

Delete Course

Deletes a course by ID.

DELETE /api/v1/course

Request Body:

Field Type Required Description
courseid integer βœ… Course ID to delete

Example Request:

{
  "courseid": 5
}

Success Response (200):

{
  "success": true,
  "data": "Course deleted successfully"
}

Instructors

Get All Students

Retrieves all students (instructor access).

GET /api/v1/instructor/students

Success Response (200):

{
  "success": true,
  "data": [
    {
      "studentid": 1,
      "studentname": "John Doe",
      "email": "john.doe@example.com",
      "trackid": 1
    },
    {
      "studentid": 2,
      "studentname": "Jane Smith",
      "email": "jane.smith@example.com",
      "trackid": 2
    }
  ]
}

Get Student by ID

Retrieves a specific student by their ID.

GET /api/v1/instructor/students/:studentId

Path Parameters:

Parameter Type Description
studentId integer Student ID

Example Request:

GET /api/v1/instructor/students/1

Success Response (200):

{
  "success": true,
  "data": [
    {
      "studentid": 1,
      "studentname": "John Doe",
      "email": "john.doe@example.com",
      "trackid": 1
    }
  ]
}

Assign Student to Track

Assigns a student to a specific track.

PATCH /api/v1/instructor/students

Request Body:

Field Type Required Description
studentid integer βœ… Student ID
trackid integer βœ… Track ID to assign

Example Request:

{
  "studentid": 1,
  "trackid": 3
}

Success Response (200):

{
  "success": true,
  "data": "Student assigned to track successfully"
}

Students

Note: Student routes are currently minimal. The main student functionality is handled through the authentication endpoints.


Request/Response Format

Standard Success Response

{
  "success": true,
  "data": "<response_data>"
}

Standard Error Response

{
  "success": false,
  "data": "<error_message>",
  "error": "<detailed_error>" // Optional
}

Validation Error Response

{
  "success": false,
  "data": [
    {
      "type": "field",
      "value": "<invalid_value>",
      "msg": "<error_message>",
      "path": "<field_name>",
      "location": "body"
    }
  ]
}

Validation Schemas

Student Validation

Register Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | studentname | Required, trimmed, min 3 characters | | email | Required, valid email format | | password | Required, min 6 characters | | trackid | Optional, must be integer |

Login Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | email | Required, valid email format | | password | Required, min 6 characters |

Instructor Validation

Register Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | instructorname | Required, trimmed, min 3 characters | | email | Required, valid email format | | password | Required, min 6 characters | | deptid | Optional, must be integer |

Login Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | email | Required, valid email format | | password | Required, min 6 characters |

Edit (Assign to Track) Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | studentid | Required, must be integer | | trackid | Required, must be integer |

Course Validation

Get Courses Schema

| Field | Location | Validation Rules | |β€”β€”-|β€”β€”β€”-|β€”β€”β€”β€”β€”β€”| | page | query | Optional, positive integer | | size | query | Optional, positive integer |

Add Course Schema

| Field | Validation Rules | |β€”β€”-|β€”β€”β€”β€”β€”β€”| | coursename | Required, min 3 characters | | instructorid | Optional, positive integer | | topicid | Optional, positive integer |


Error Handling

HTTP Status Codes

Code Status Description
200 OK Request successful
201 Created Resource created successfully
400 Bad Request Validation error or invalid request
404 Not Found Resource not found
500 Internal Server Error Server-side error

Common Error Scenarios

  1. Validation Errors (400)
    • Missing required fields
    • Invalid field format (email, integer, etc.)
    • Field length constraints violated
  2. Database Errors (400/500)
    • Foreign key constraint violations
    • Duplicate entries
    • Connection failures
  3. Authentication Errors (500)
    • Invalid credentials
    • User not found

Stored Procedures

The application uses SQL Server stored procedures for database operations:

Procedure Purpose Parameters
insert_student Register new student @studentname, @email, @trackid, @password
sp_StudentLogin Authenticate student @email, @password
insert_instructor Register new instructor @instructorname, @email, @password, @deptid
sp_InstructorLogin Authenticate instructor @email, @password
select_course Get courses (paginated) @PageNumber, @PageSize, @courseid
insert_course Create new course @coursename, @instructorid, @topicid
update_course Update existing course @courseid, @coursename, @instructorid, @topicid
delete_course Delete course @courseid
select_student Get student(s) @studentId
assignStudentToTrack Assign student to track @studentid, @trackid

API Quick Reference

Method Endpoint Description
POST /api/v1/auth/register Register student
POST /api/v1/auth/login Login student
POST /api/v1/auth/instructor/register Register instructor
POST /api/v1/auth/instructor/login Login instructor
GET /api/v1/course Get all courses
GET /api/v1/course/:id Get course by ID
POST /api/v1/course Create course
PATCH /api/v1/course Update course
DELETE /api/v1/course Delete course
GET /api/v1/instructor/students Get all students
GET /api/v1/instructor/students/:studentId Get student by ID
PATCH /api/v1/instructor/students Assign student to track

Future Enhancements

The following features are planned or in development:


Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the ISC License.


Support

For support, please open an issue in the repository or contact the development team.


Documentation generated on January 22, 2026