0% found this document useful (0 votes)
4 views5 pages

Quizquest Backend Code

Uploaded by

hasini.thota.04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Quizquest Backend Code

Uploaded by

hasini.thota.04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C:\Users\hasin\Desktop\prog\MERN\quiz\FINAL\finalest\QuizQuest backend\backend\controllers

Controllers:
getAll

getQuiz

postQuiz

getAll.js
import Quizes from "../models/model.js";

const getAll = async (req, res) => {

try {

const quizes = await Quizes.find({});

quizes.reverse();

res.json(quizes);

catch(e) {

res.json({

status: 404,

success: false,

message: "objects not found"

});

export default getAll;

getQuiz.js
import Quizes from "../models/model.js";
const getQuiz = async (req, res) => {

const id = req.params.id;

try {

const quiz = await Quizes.findOne({_id:id});

res.json(quiz);

catch(e) {

res.json({

status: 404,

success: false,

message: "object not found"

});

export default getQuiz;

postQuiz.js
import { __dirname } from "../server.js";

import Quiz from "../models/model.js"

const postQuiz = async (req, res) => {

const entry = req.body;

const quiz = new Quiz({title: entry.title, duration: entry.duration});


entry.questions.map(({description, options, correctOption}) => {

quiz.questions.push({description, options, correctOption});

});

try {

await quiz.save();

res.json({

status: 201,

success: true

});

catch(e) {

console.log(e);

res.json({

status: 406,

success: false,

message: "Internal Server error or the data sent was invalid"

});

export default postQuiz;

Model.js
import mongoose from "mongoose";

const quizSchema = mongoose.Schema({

title: String,

duration: Number,
questions: [{

description: String,

options: [{ type: String }],

correctOption: Number

}],

});

const Quizes = mongoose.model("quiz_db", quizSchema);

export default Quizes;

C:\Users\hasin\Desktop\prog\MERN\quiz\FINAL\finalest\QuizQuest backend\backend\routes

routes.js
import express from "express";

import getAll from "../controllers/getAll.js";

import getQuiz from "../controllers/getQuiz.js";

import postQuiz from "../controllers/postQuiz.js";

const router = express.Router();

router.get('/get-all', getAll);

router.get('/get-quiz/:id', getQuiz);

router.post('/post-quiz', postQuiz);

//router.delete('/delete/:id',delete);

//router.patch('/update/:id',update);

export default router;


server.js
import express from "express";

import dotenv from "dotenv";

import router from "./routes/routes.js";

import mongoose from "mongoose";

import { fileURLToPath } from 'url';

import { dirname } from "path";

import cors from "cors";

const __filename = fileURLToPath(import.meta.url);

export const __dirname = dirname(__filename);

dotenv.config();

const PORT = process.env.PORT || 5500;

const DB_PWD = process.env.DB_PWD || "Honey@2004";

const server = express();

// middlewares

server.use(cors());

server.use(express.json());

server.use("/api", router); // router middleware should be placed at the last

const URI = "mongodb+srv://hasini21bce8857:"+DB_PWD+"@quizquest.rwrs2wi.mongodb.net/?


retryWrites=true&w=majority";

mongoose.connect(URI, {dbName: "main"}).then(() => console.log("db connected


sucessfully!")).catch((e) => console.log(e));

server.listen(PORT, () => console.log("server started successfully on port:", PORT));

You might also like