Skip to content

Commit 5f900ce

Browse files
committed
Create question services
1 parent f5cc2bc commit 5f900ce

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

question-service/src/app.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from 'express';
2+
3+
import * as dotenv from 'dotenv';
4+
import questionsRouter from './routes/questionsController';
5+
import userQuestionsRouter from './routes/userQuestionsController';
6+
7+
dotenv.config({ path: '.env.dev' });
8+
9+
const app = express();
10+
const PORT = process.env.PORT || 3000;
11+
12+
app.use(express.json());
13+
14+
// different routes
15+
app.use('/questions', questionsRouter);
16+
app.use('/userquestions', userQuestionsRouter);
17+
18+
app.listen(PORT, () => {
19+
console.log(`Server is running on port ${PORT}`);
20+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { MongoClient, ServerApiVersion, Db } from 'mongodb';
2+
import * as dotenv from 'dotenv';
3+
// configure env variables
4+
dotenv.config({ path: '.env.dev' });
5+
6+
const MONGODB_URI = process.env.MONGODB_CONNECTION_STRING || '';
7+
// console.log(MONGODB_URI);
8+
9+
let db: Db;
10+
11+
const client = new MongoClient(MONGODB_URI, {
12+
serverApi: {
13+
version: ServerApiVersion.v1,
14+
strict: true,
15+
deprecationErrors: true,
16+
17+
}
18+
});
19+
20+
export async function connectToDB(): Promise<Db> {
21+
try {
22+
// Connect the client to the server (optional starting in v4.7)
23+
if (!db) {
24+
await client.connect();
25+
db = client.db('Database1');
26+
}
27+
return db;
28+
} catch (err) {
29+
throw new Error(`Error connecting to the database: ${err}`);
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { UUID } from "crypto";
2+
3+
export interface Questions {
4+
_question_id: number;
5+
difficulty: number; // 1, 2, 3
6+
description: string;
7+
examples: string[];
8+
constraints: string;
9+
tags: string[];
10+
title_slug: string;
11+
title: string;
12+
pictures?: File[];
13+
}
14+
15+
export interface UserQuestions {
16+
_user_id: UUID;
17+
_question_id: UUID;
18+
status: string; // 'completed', 'in-progress', 'not-started'
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// src/routes/items.ts
2+
import express, { Request, Response } from 'express';
3+
import { Collection } from 'mongodb';
4+
import { connectToDB } from '../db/mongoClient';
5+
import { Questions } from '../models/types';
6+
7+
const router = express.Router();
8+
let questionsCollection: Collection<Questions>;
9+
10+
// Middleware to connect to MongoDB and get the collection
11+
router.use(async (_, res, next) => {
12+
try {
13+
const db = await connectToDB();
14+
questionsCollection = db.collection<Questions>('questions');
15+
next();
16+
} catch (err) {
17+
res.status(500).json({ error: `Failed to connect to MongoDB, ${err}` });
18+
}
19+
});
20+
21+
// GET all items
22+
router.get('/', async (req: Request, res: Response) => {
23+
try {
24+
const items = await questionsCollection.find().toArray();
25+
res.status(200).json(items);
26+
} catch (error) {
27+
res.status(500).json({ error: "Failed to fetch items" });
28+
}
29+
});
30+
31+
export default router;

question-service/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6", // Specify ECMAScript target version
4+
"module": "commonjs", // Specify module code generation
5+
"outDir": "./dist", // Redirect output structure to the directory
6+
"rootDir": "./src", // Specify the root directory of input files
7+
"strict": true, // Enable all strict type-checking options
8+
"esModuleInterop": true, // Enables emit interoperability between CommonJS and ES Modules
9+
"skipLibCheck": true, // Skip type checking of declaration files
10+
"forceConsistentCasingInFileNames": true // Disallow inconsistently-cased references
11+
},
12+
"include": ["src/**/*"], // Include all files in the src directory
13+
"exclude": ["node_modules", "**/*.spec.ts"] // Exclude node_modules and test files
14+
}

0 commit comments

Comments
 (0)