Skip to content

project structure

Tzesh edited this page May 24, 2023 · 1 revision

VPatient API Project Structure

In this section we will be talking about the project structure of the VPatient API. Our project structure is below:

  • models/ -> Contains the models of the API. Which is our tables in the database.
  • routes/ -> Contains the routes of the API. Which is our endpoints.
  • common/ -> Contains the common functions of the API. Which is our common functions. Such as validation functions.

Creating a Model

Model is a table in the database. You can create a model using mongoose by looking the examples in the models directory. Also you can use them in your routes by just importing models. Let's take BloodSugarTrace model as an example:

const mongoose = require('mongoose'); // You have to import mongoose to use mongoose functions.

const BloodSugarTraceSchema = new mongoose.Schema ({ // You have to create a schema for your model.
    time: { // A column in the table.
        type: Date, // Type of the column.
        required: true // If the column is required or not.
    },
    result: {
        type: String,
        required: true
    },
    note: {
        type: String
    },
    owner: { type: mongoose.Types.ObjectId, ref: 'PatientModel' } // You can use this to create a relationship between two models.
},
{timestamps: true}); // You can use this to create timestamps for your model.

module.exports = mongoose.model('BloodSugarTraceModel', BloodSugarTraceSchema); // Don't forget to export your model.

Creating a Route

Route is an endpoint in the API. You can create a route using express by looking the examples in the routes directory. Also you have to import your routes in the main file of the API. Which is 'index.js' in the main directory of the API.

const router = require("express").Router(); // You have to import express to use express functions.
const { auth, verifyTokenAndAdmin } = require('../../auth/verifyToken'); // You have to import auth functions to use auth functions.
const { verifyPatient } = require('../verifyPatient'); // You have to import verifyPatient functions to use verifyPatient functions this is a middleware that checks if the given id is a valid patient id. If it is valid it will return the patient object. If it is not valid it will return an error.
const BloodSugarTraceModel = require('../../../models/BloodSugarTraceModel'); // You have to import your models to use your models.

// create patient blood sugar trace
router.post("/create", verifyTokenAndAdmin, verifyPatient, async(req, res) => { // A create route to create a blood sugar trace for a patient.
    // get patient
    let patient = req.patient;

    // results array
    var traces = [];

    // control models
    if (!req.body.bloodSugarTrace) return res.status(500).json('Invalid format');

    // fill results
    req.body.bloodSugarTrace.forEach(trace => {
        var bloodSugar = new BloodSugarTraceModel({
            time: trace.time,
            result: trace.result,
            note: trace.note,
            owner: patient._id
        });

        traces.push(bloodSugar);
    });

    // insert all
    BloodSugarTraceModel.insertMany(traces)
        .then(traces => res.status(200).json(traces))
        .catch(err => res.status(500).json({ message: err }));
});

// get patient laboratory results
router.get("/get", auth, verifyPatient, async(req, res) => { // Get route to get blood sugar traces of a patient.
    // get patient
    let patient = req.patient;

    // get results sorted to the time that dosage is taken
    const traces = await BloodSugarTraceModel.find({ owner: patient }).sort({ time: 1 });

    // return
    res.status(200).json(traces);
});

module.exports = router; // Don't forget to export your route.

The basic things that you need to know are above. You can look at the other routes to learn more about the routes.

Since we are using express.js you can use all the functions of express.js. You can learn more about express.js here.