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..80a7804145 --- /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 { + difficulty: string; +} + +const DifficultySchema: Schema = new Schema({ + 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 new file mode 100644 index 0000000000..812b67be78 --- /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 { + topic: string; +} + +const TopicSchema: Schema = new Schema({ + topic: { 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 e0a9e91d20..4831dfa5c8 100644 --- a/peerprep/backend/question-service/src/server.ts +++ b/peerprep/backend/question-service/src/server.ts @@ -5,6 +5,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 @@ -44,6 +45,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' });