Skip to content

Commit 61e7540

Browse files
authored
Merge branch 'master' into master
2 parents 8342303 + 711bc87 commit 61e7540

File tree

14 files changed

+885
-74
lines changed

14 files changed

+885
-74
lines changed

question_service/package-lock.json

Lines changed: 691 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

question_service/package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
"name": "question_service",
33
"version": "1.0.0",
44
"description": "Backend service for accessing question bank",
5-
"main": "server.js",
5+
"main": "index.ts",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"dev": "nodemon server.js",
9-
"start": "node server.js"
9+
"start": "ts-node src/index.ts"
1010
},
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14+
"adm-zip": "^0.5.10",
1415
"cors": "^2.8.5",
1516
"dotenv": "^16.3.1",
1617
"express": "^4.18.2",
17-
"mongoose": "^7.5.3"
18+
"mongoose": "^7.5.3",
19+
"multer": "^1.4.5-lts.1"
1820
},
1921
"devDependencies": {
20-
"nodemon": "^3.0.1"
22+
"@types/cors": "^2.8.16",
23+
"@types/express": "^4.17.21",
24+
"@types/multer": "^1.4.10",
25+
"nodemon": "^3.0.1",
26+
"ts-node-dev": "^2.0.0",
27+
"typescript": "^5.2.2"
2128
}
2229
}

question_service/routes/questionRouter.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const mongoose = require('mongoose')
2-
const Counter = require('../model/counterModel');
1+
import mongoose from 'mongoose';
2+
import Counter from '../model/counterModel';
33

44

55
// function to start up and connect to MongoDB database
6-
const connectDB = async () => {
6+
export const connectDB = async () => {
77
try {
88
// attempt to connect to MongoDB database via the connection string specified in .env file
99
console.log(`connecting with... ${process.env.MONGODB_URI}`)
10-
const con = await mongoose.connect(process.env.MONGODB_URI)
10+
const con = await mongoose.connect(process.env.MONGODB_URI ?? '')
1111
console.log(`MongoDB connected: ${con.connection.host} `)
1212
} catch (error) {
1313
console.log(error)
@@ -16,7 +16,7 @@ const connectDB = async () => {
1616
}
1717
}
1818

19-
const initCounter = async () => {
19+
export const initCounter = async () => {
2020
try {
2121
const questionIndex = await Counter.findById('questionIndex');
2222
if (!questionIndex) {
@@ -30,7 +30,3 @@ const initCounter = async () => {
3030
console.log('Error initializing counter:', error);
3131
}
3232
};
33-
34-
// export connection function to be used in index.js
35-
module.exports = { connectDB, initCounter }
36-

question_service/config/populate_qns.js renamed to question_service/src/config/populate_qns.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const Question = require('../model/questionModel');
2-
const questions = require('./question_data');
3-
const getNextSequenceValue = require('../controller/counterController')
1+
import Question from '../model/questionModel';
2+
import { questions } from './question_data';
3+
import { getNextSequenceValue } from '../controller/counterController';
4+
45

56
/**
67
* Populates the database if questions dont exist
78
*/
8-
async function populateData() {
9+
export async function populateData() {
910
try {
1011
for (const question of questions) {
1112

@@ -24,9 +25,7 @@ async function populateData() {
2425

2526
console.log('Questions population completed.');
2627

27-
} catch (error) {
28+
} catch (error : any) {
2829
console.log(`Populating questions ran into error: ${error.message}`);
2930
}
3031
}
31-
32-
module.exports = populateData;

question_service/config/question_data.js renamed to question_service/src/config/question_data.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
const questions = [
1+
interface QuestionData {
2+
title: string;
3+
description: string;
4+
topics: string[];
5+
difficulty: number;
6+
id?: number;
7+
}
8+
9+
10+
export const questions : QuestionData[] = [
211
{
312
title: 'Reverse a string',
413
description: `
@@ -280,5 +289,3 @@ const questions = [
280289
difficulty: 9,
281290
},
282291
];
283-
284-
module.exports = questions;

question_service/controller/counterController.js renamed to question_service/src/controller/counterController.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const Counter = require('../model/counterModel');
1+
import Counter from '../model/counterModel';
22

3-
const getNextSequenceValue = async (sequenceName) => {
3+
export const getNextSequenceValue = async (sequenceName : string) => {
44
try {
55
const sequenceDocument = await Counter.findOneAndUpdate(
66
{ _id: sequenceName },
@@ -13,5 +13,3 @@ const getNextSequenceValue = async (sequenceName) => {
1313
throw error; // or handle this in some other appropriate way
1414
}
1515
};
16-
17-
module.exports = getNextSequenceValue;

question_service/controller/questionController.js renamed to question_service/src/controller/questionController.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const Question = require('../model/questionModel')
2-
const getNextSequenceValue = require('./counterController')
1+
import Question from '../model/questionModel';
2+
import { getNextSequenceValue } from './counterController';
33

44
//@desc fetch all questions
55
//@route GET /api/questions
66
//@access authenticated users
7-
const fetchAllQuestions = async (req, res) => {
7+
export const fetchAllQuestions = async (req : any, res : any) => {
88
const questions = await Question.find({})
99

1010
res.status(200).json(questions)
@@ -14,12 +14,15 @@ const fetchAllQuestions = async (req, res) => {
1414
//@desc fetch a question
1515
//@route GET /api/questions/:id
1616
//@access authenticated users
17-
const fetchQuestion = async (req, res) => {
17+
export const fetchQuestion = async (req : any, res : any) => {
1818
try {
1919
// function provided by mongoose to find an Question document with a given ID
2020
// req.params.id is retrieved from /:id in route
2121
const question = await Question.findById(req.params.id)
2222

23+
if(question === null) {
24+
throw Error('Invalid ID. Question not found in database.');
25+
}
2326
res.status(200).json({
2427
id : question.id,
2528
_id: question._id,
@@ -28,6 +31,7 @@ const fetchQuestion = async (req, res) => {
2831
topics: question.topics,
2932
difficulty: question.difficulty
3033
})
34+
3135
} catch (error) {
3236
res.status(400).json({ message: 'Invalid ID. Question not found in database.' })
3337
}
@@ -36,7 +40,7 @@ const fetchQuestion = async (req, res) => {
3640
//@desc add a question
3741
//@route POST /api/questions
3842
//@access admin only
39-
const addQuestion = async (req, res) => {
43+
export const addQuestion = async (req : any, res : any) => {
4044
const { title, description, topics, difficulty } = req.body;
4145

4246
if (!title || !description || !topics || !difficulty) {
@@ -58,15 +62,15 @@ const addQuestion = async (req, res) => {
5862
topics: question.topics,
5963
difficulty: question.difficulty
6064
})
61-
} catch (error) {
65+
} catch (error : any) {
6266
res.status(400).json({ message: 'Invalid question data', error: error.message })
6367
}
6468
}
6569

6670
// @desc Update a question
6771
// @route PUT /api/addresses/:id
6872
// @access admin only
69-
const updateQuestion = async (req, res) => {
73+
export const updateQuestion = async (req : any, res : any) => {
7074
const { title, description, topics, difficulty } = req.body
7175

7276
if (!title || !description || !topics || !difficulty) {
@@ -77,6 +81,11 @@ const updateQuestion = async (req, res) => {
7781
// function provided by mongoose to find an Question document with a given ID
7882
// req.params.id is retrieved from /:id in route
7983
const question = await Question.findById(req.params.id)
84+
85+
if(question === null) {
86+
throw Error('Invalid ID. Question not found in database.');
87+
}
88+
8089
// update the document
8190
question.title = title
8291
question.description = description
@@ -105,17 +114,17 @@ const updateQuestion = async (req, res) => {
105114
// @desc Delete a question
106115
// @route DELETE /api/addresses/:id
107116
// @access admin only
108-
const deleteQuestion = async (req, res) => {
117+
export const deleteQuestion = async (req : any, res : any) => {
109118
try {
110119
// function provided by mongoose to find an Question document with a given ID
111120
// req.params.id is retrieved from /:id in route
112121
const question = await Question.findById(req.params.id);
122+
if(question === null) {
123+
throw Error('Invalid ID. Question not found in database.');
124+
}
113125
await question.deleteOne();
114126
res.status(200).json({ message: 'Question removed' });
115127
} catch (error) {
116128
res.status(404).json({ message: 'Question not found' })
117129
}
118-
}
119-
120-
121-
module.exports = { fetchAllQuestions, fetchQuestion, addQuestion, updateQuestion, deleteQuestion }
130+
}

question_service/server.js renamed to question_service/src/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
const express = require('express')
2-
const cors = require('cors')
3-
const dotenv = require('dotenv').config()
4-
const {connectDB, initCounter } = require('./config/db')
5-
const populateData = require('./config/populate_qns');
1+
import express from 'express';
2+
import cors from 'cors';
3+
import dotenv from 'dotenv';
4+
import { connectDB, initCounter } from './config/db';
5+
import { populateData } from './config/populate_qns';
6+
import questionRouter from './routes/questionRouter'
67

7-
require('dotenv').config()
8+
9+
dotenv.config();
810

911
connectDB().then((v) => {
1012
initCounter();
@@ -27,7 +29,7 @@ app.use(express.urlencoded({ extended: false }))
2729

2830
// use the address router to handle requests
2931
// at http://localhost:8080/api/addresses
30-
app.use('/api/questions', require('./routes/questionRouter'))
32+
app.use('/api/questions', questionRouter)
3133

3234

3335
const PORT = process.env.PORT || 8080;
@@ -36,11 +38,3 @@ const PORT = process.env.PORT || 8080;
3638
app.listen(PORT, () => {
3739
console.log(`Server is running on port ${ PORT }...`)
3840
})
39-
40-
41-
app.get('/', (req, res) => {
42-
res.json({ message: 'Hello World' })
43-
})
44-
45-
46-
module.exports = app
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const multer = require('multer');
2+
const fs = require('fs');
3+
const unzipper = require('unzipper');

0 commit comments

Comments
 (0)