Skip to content

Latest commit

 

History

84 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Campus-Student Management API

This project is a RESTful API built with Node.js, Express, and Sequelize, allowing users to manage students and campuses.

Features:

  • Campus Management:
    • Add, edit, delete, and fetch campuses.
    • Fetch campuses with associated students.
  • Student Management:
    • Add, edit, delete, and fetch students.
    • Fetch students with their associated campus.

Tech Stack

NodeJS Express.js JavaScript Postgres Sequelize Nodemon Vercel

  • Middleware: CORS, body-parser

Prerequisites

For this only install brew, Install anything below brew, through brew.

Install brew

bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install node

brew install node

Check npm Version

npm -v

Setup

  1. Clone the repository:
git clone https://github.com/AleHS01/campus-student-api.git
cd campus-student-api
  1. Install dependecies:
npm install
  1. Setup .env file:
  • Create a .env file in the root directory.
  • Add your database credentials and server configuration:
PORT=your_port
POSTGRES_URL=your_db_url

Run the server

  • Start the server
npm run start
  • or use a task runner like nodemon for automatic restarts:
nodemon index.js
  • Local Backend URL:
http://localhost:8080

API Endpoints:

  • Campus Endpoints
    • GET /api/campus: Fetch all campuses.
    • GET /api/campus/:id: Fetch a campus by ID (with associated students).
    • POST /api/campus/addCampus: Add a new campus.
    • PUT /api/campus/updateCampus/:id: Update a campus by ID.
    • DELETE /api/campus/removeCampus/:id: Delete a campus by ID.
  • Student Endpoints
    • GET /api/student: Fetch all students.
    • GET /api/student/:id: Fetch a student by ID (with associated campus).
    • POST /api/student/addStudent: Add a new student.
    • PUT /api/student/updateStudent/:id: Update a student by ID.
    • DELETE /api/student/removeStudent/:id: Delete a student by ID.

Database Structure

This project uses Sequelize to define models and manage relationships with a PostgreSQL database. Below is an overview of the database structure and relationships between the models.

Models

Campus Model

The Campus model represents a campus in the system and contains the following fields:

  • id: The primary key, auto-incremented integer.
  • name: The name of the campus (required).
  • imageurl: URL for the campus image, with a default value of an empty string.
  • address: The address of the campus (required).
  • description: A textual description of the campus.
const { DataTypes } = require("sequelize");
const db = require("../db");

const Campus = db.define("Campus", {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    imageurl: {
        type: DataTypes.STRING,
        defaultValue: "",
    },
    address: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    description: {
        type: DataTypes.STRING,
    },
});

module.exports = Campus;

Student Model

The Student model represents a student enrolled in a campus and contains the following fields:

  • id: The primary key, auto-incremented integer.
  • firstname: The student's first name (required).
  • lastname: The student's last name (required).
  • email: The student's email address (required, must be a valid email).
  • imageurl: URL for the student's image, with a default value.
  • gpa: The student's GPA (must be between 0.0 and 4.0).
  • CampusId: A foreign key linking the student to a campus. This is an optional relationship, as students may not be associated with a campus initially.
const { DataTypes } = require("sequelize");
const db = require("../db");

const Student = db.define("Student", {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  firstname: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  lastname: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      isEmail: {
        msg: "Invalid Email Format",
      },
    },
  },
  imageurl: {
    type: DataTypes.STRING,
    defaultValue: "https://picsum.photos/200",
  },
  gpa: {
    type: DataTypes.DECIMAL,
    validate: {
      min: 0.0,
      max: 4.0,
    }
  },
  CampusId: {
    type: DataTypes.INTEGER,
    allowNull: true
  }
});

module.exports = Student;

Relationships

  • One-to-Many Relationship:
    • A Campus can have many Students. This relationship is represented by the CampusId foreign key in the Student model.
    • Sequelize handles the relationship by associating each Student with a Campus.
Campus.hasMany(Student);
Student.belongsTo(Campus);

Database Configuration:

  • The database connection is managed by Sequelize, and the connection details are stored in the environment variable POSTGRES_URL.
  • The connection is established using sequelize.authenticate(), which logs a success message to the console if the connection is successful.
const { Sequelize } = require("sequelize");
const dotenv = require("dotenv").config();
const pg = require("pg");

const db = new Sequelize(process.env.POSTGRES_URL + "?sslmode=require", {
  logging: false,
  dialectModule: pg,
});

db.authenticate()
  .then(() => {
    console.log("DB connection works");
  })
  .catch((error) => {
    console.error("DB connection failed:", error);
  });

module.exports = db;

Development Notes

  • Sequelize models and associations are defined in database/models/index.js.
  • Database connection is established in database/db.js.
  • The seed script can populate the database with initial data by enabling this on index.js:
    await seed()
  • All API routes are located in the api/ folder and accessible throught api/index.js:
    const router = require("express").Router();
    
    router.use("/student", require("./student"));
    router.use("/campus", require("./campus"));
    
    router.use((req, res, next) => {
      const error = new Error("404 Not Found");
      error.status = 404;
      next(error);
    });
    
    module.exports = router;

Deployment

This project is deployed using Vercel, though it is not currently active. You can deploy your own instance by following these steps:

  • Visit Vercel and create an account.
  • Link your GitHub repository to Vercel.
  • Set up deployment settings and deploy the project.

About

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages