Skip to content

Commit

Permalink
Merge branch 'rest-integrate' into 'main'
Browse files Browse the repository at this point in the history
feat: integrate with spa

See merge request toco-2/toco-rest!3
  • Loading branch information
ditramadia committed Nov 13, 2023
2 parents e3ece37 + 806ae97 commit c63ae7f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 4 deletions.
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"license": "ISC",
"dependencies": {
"@prisma/client": "^5.5.2",
"axios": "^1.6.1",
"bcrypt": "^5.1.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import exerciseRoutes from './routes/exercise';
import languageRoutes from './routes/language';
import questionRoutes from './routes/question'
import optionRoutes from './routes/option'
const cookieParser = require('cookie-parser');

const app = express();
const cors = require('cors');
app.use(cors());
const PORT = 5000;

app.use(express.json());
app.use(cookieParser());

app.use('/auth', authRoutes);
app.use('/exercise', exerciseRoutes);
Expand Down
48 changes: 44 additions & 4 deletions src/routes/exercise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import accessValidation from '../middleware/accessValidation';
import { PrismaClient } from '@prisma/client';
import http from 'http';

const router = express.Router();
const prisma = new PrismaClient();
Expand All @@ -9,23 +10,62 @@ type Results = {
[question_id: number]: string | null;
};

interface OptionData {
option_id: number;
option: string;
is_correct: boolean
};

interface QuestionData {
question: string;
options: OptionData[]
};

// Create exercise
router.post('/create', accessValidation, async (req, res) => {
const { exe_name, language_id, category, difficulty } = req.body;

const token = req.cookies.token;

const {
exe_name,
language_id,
category,
difficulty,
questions
} = req.body;

try {
const result = await prisma.exercise.create({
const exercise_result = await prisma.exercise.create({
data: {
exe_name,
language_id,
category,
difficulty
difficulty,
}
});

for (const question of questions) {
const question_result = await prisma.question.create({
data: {
exercise_id: exercise_result.exercise_id,
question: question.question,
}
});

for (const option of question.options) {
await prisma.option.create({
data: {
question_id: question_result.question_id,
option: option.option,
is_correct: option.is_correct,
}
});
}
}

res.json({
message: 'Exercise created successfully',
result,
exercise_result
});
} catch (error) {
console.error(error);
Expand Down

0 comments on commit c63ae7f

Please sign in to comment.