Skip to content

Commit

Permalink
Merge branch 'dev' into branch-rubesh-collabIntegration
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Nov 3, 2024
2 parents 82a2dc4 + 2bc0ab5 commit 625005e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<void> => {
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<void> => {
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<void> => {
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 });
}
};
11 changes: 11 additions & 0 deletions peerprep/backend/question-service/src/models/difficultyModel.ts
Original file line number Diff line number Diff line change
@@ -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<IDifficulty>('Difficulty', DifficultySchema);
11 changes: 11 additions & 0 deletions peerprep/backend/question-service/src/models/topicModel.ts
Original file line number Diff line number Diff line change
@@ -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<ITopic>('Topic', TopicSchema);
12 changes: 12 additions & 0 deletions peerprep/backend/question-service/src/routes/databaseRoutes.ts
Original file line number Diff line number Diff line change
@@ -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;

4 changes: 4 additions & 0 deletions peerprep/backend/question-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' });
Expand Down

0 comments on commit 625005e

Please sign in to comment.