From 1578203d7c504e118c86b13c9ae486642bcb8f6e Mon Sep 17 00:00:00 2001 From: bhnuka <01bhanukaekanayake@gmail.com> Date: Sat, 2 Nov 2024 20:51:41 +0800 Subject: [PATCH 1/2] add question ewnhancements for fetching --- .../src/controllers/databaseController.ts | 53 +++++++++++++++++++ .../src/models/difficultyModel.ts | 11 ++++ .../question-service/src/models/topicModel.ts | 11 ++++ .../src/routes/databaseRoutes.ts | 12 +++++ .../backend/question-service/src/server.ts | 4 ++ 5 files changed, 91 insertions(+) create mode 100644 peerprep/backend/question-service/src/controllers/databaseController.ts create mode 100644 peerprep/backend/question-service/src/models/difficultyModel.ts create mode 100644 peerprep/backend/question-service/src/models/topicModel.ts create mode 100644 peerprep/backend/question-service/src/routes/databaseRoutes.ts diff --git a/peerprep/backend/question-service/src/controllers/databaseController.ts b/peerprep/backend/question-service/src/controllers/databaseController.ts new file mode 100644 index 0000000000..6f7c98b876 --- /dev/null +++ b/peerprep/backend/question-service/src/controllers/databaseController.ts @@ -0,0 +1,53 @@ +import { Request, Response } from 'express'; +import Topic from '../models/topicModel'; +import Difficulty from '../models/difficultyModel'; + +// Get all topics +export const getAllTopics = async (req: Request, res: Response): Promise => { + try { + const topics = await Topic.find(); + res.status(200).json(topics); + } catch (error) { + res.status(500).json({ message: 'Error fetching topics', error }); + } +}; + +// Check if a topic exists +export const topicExists = async (req: Request, res: Response): Promise => { + try { + const { topicName } = req.params; + const topic = await Topic.findOne({ name: topicName }); + if (topic) { + res.status(200).json({ exists: true }); + } else { + res.status(404).json({ exists: false, message: 'Topic not found' }); + } + } catch (error) { + res.status(500).json({ message: 'Error checking topic', error }); + } +}; + +// Get all difficulties +export const getDifficulties = async (req: Request, res: Response): Promise => { + try { + const difficulties = await Difficulty.find(); + res.status(200).json(difficulties); + } catch (error) { + res.status(500).json({ message: 'Error fetching difficulties', error }); + } +}; + +// Check if a difficulty exists +export const difficultyExists = async (req: Request, res: Response): Promise => { + try { + const { difficultyName } = req.params; + const topic = await Topic.findOne({ name: difficultyName }); + if (topic) { + res.status(200).json({ exists: true }); + } else { + res.status(404).json({ exists: false, message: 'Difficulty not found' }); + } + } catch (error) { + res.status(500).json({ message: 'Error checking difficulty', error }); + } + }; \ No newline at end of file diff --git a/peerprep/backend/question-service/src/models/difficultyModel.ts b/peerprep/backend/question-service/src/models/difficultyModel.ts new file mode 100644 index 0000000000..d5667fe810 --- /dev/null +++ b/peerprep/backend/question-service/src/models/difficultyModel.ts @@ -0,0 +1,11 @@ +import mongoose, { Schema, Document } from 'mongoose'; + +interface IDifficulty extends Document { + level: string; +} + +const DifficultySchema: Schema = new Schema({ + level: { type: String, required: true, unique: true } +}); + +export default mongoose.model('Difficulty', DifficultySchema); diff --git a/peerprep/backend/question-service/src/models/topicModel.ts b/peerprep/backend/question-service/src/models/topicModel.ts new file mode 100644 index 0000000000..aa2c0c51f6 --- /dev/null +++ b/peerprep/backend/question-service/src/models/topicModel.ts @@ -0,0 +1,11 @@ +import mongoose, { Schema, Document } from 'mongoose'; + +interface ITopic extends Document { + name: string; +} + +const TopicSchema: Schema = new Schema({ + name: { type: String, required: true, unique: true } +}); + +export default mongoose.model('Topic', TopicSchema); diff --git a/peerprep/backend/question-service/src/routes/databaseRoutes.ts b/peerprep/backend/question-service/src/routes/databaseRoutes.ts new file mode 100644 index 0000000000..a69f297381 --- /dev/null +++ b/peerprep/backend/question-service/src/routes/databaseRoutes.ts @@ -0,0 +1,12 @@ +import express from 'express'; +import { getAllTopics, topicExists, getDifficulties } from '../controllers/databaseController'; + +const router = express.Router(); + +router.get('/topics', getAllTopics); +router.get('/topics/exists/:topicName', topicExists); +router.get('/difficulties', getDifficulties); +router.get('/difficulties/exists/:difficultyName', topicExists); + +export default router; + diff --git a/peerprep/backend/question-service/src/server.ts b/peerprep/backend/question-service/src/server.ts index 94e98696a6..c9286032ad 100644 --- a/peerprep/backend/question-service/src/server.ts +++ b/peerprep/backend/question-service/src/server.ts @@ -4,6 +4,7 @@ import dotenv from 'dotenv'; dotenv.config(); import connectDB from '../config/db'; import questionRoutes from './routes/questionRoutes'; +import databaseRoutes from './routes/databaseRoutes'; import loadSampleData from './sampleData'; connectDB() // Initialize MongoDB connection @@ -35,6 +36,9 @@ app.use(express.json()); // API routes app.use('/api', questionRoutes); +// Database routes +app.use('/api', databaseRoutes); + // Health check route app.get('/hello', (req, res) => { res.json({ message: 'Hello World' }); From 7c758a7fb46a960bdf94064aa9c4971739a382d8 Mon Sep 17 00:00:00 2001 From: bhnuka <01bhanukaekanayake@gmail.com> Date: Sat, 2 Nov 2024 21:00:21 +0800 Subject: [PATCH 2/2] consistent naming --- .../backend/question-service/src/models/difficultyModel.ts | 4 ++-- peerprep/backend/question-service/src/models/topicModel.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/peerprep/backend/question-service/src/models/difficultyModel.ts b/peerprep/backend/question-service/src/models/difficultyModel.ts index d5667fe810..80a7804145 100644 --- a/peerprep/backend/question-service/src/models/difficultyModel.ts +++ b/peerprep/backend/question-service/src/models/difficultyModel.ts @@ -1,11 +1,11 @@ import mongoose, { Schema, Document } from 'mongoose'; interface IDifficulty extends Document { - level: string; + difficulty: string; } const DifficultySchema: Schema = new Schema({ - level: { type: String, required: true, unique: true } + difficulty: { type: String, required: true, unique: true } }); export default mongoose.model('Difficulty', DifficultySchema); diff --git a/peerprep/backend/question-service/src/models/topicModel.ts b/peerprep/backend/question-service/src/models/topicModel.ts index aa2c0c51f6..812b67be78 100644 --- a/peerprep/backend/question-service/src/models/topicModel.ts +++ b/peerprep/backend/question-service/src/models/topicModel.ts @@ -1,11 +1,11 @@ import mongoose, { Schema, Document } from 'mongoose'; interface ITopic extends Document { - name: string; + topic: string; } const TopicSchema: Schema = new Schema({ - name: { type: String, required: true, unique: true } + topic: { type: String, required: true, unique: true } }); export default mongoose.model('Topic', TopicSchema);