Commit 4949c01d authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

Merge branch 'main' into file-upload

parents 6a24c10c 80a2b663
import { Survey, ISurvey } from "../models"; import { Survey, ISurvey } from "../models";
export const findUserBySurveyId = async (surveyId: string) => {
const survey = await Survey.findById(surveyId).populate("user");
console.log(survey);
if (survey !== null) {
console.log(survey.user);
return survey.user;
}
return null;
};
export const createSurvey = async (survey: ISurvey) => { export const createSurvey = async (survey: ISurvey) => {
const newSurvey = await Survey.create(survey); const newSurvey = await Survey.create(survey);
return newSurvey; return newSurvey;
}; };
export const getSurveyById = async (surveyId: string) => {
console.log("survey id", surveyId);
const survey = await Survey.findById(surveyId).populate("questions");
return survey;
};
export const updateSurvey = async (survey: ISurvey) => {
const newSurvey = await Survey.findOneAndUpdate({ _id: survey._id }, survey);
return newSurvey;
};
import { model, Schema, Types } from "mongoose"; import { model, ObjectId, Schema, Types } from "mongoose";
export interface IQuestion { export interface IQuestion {
_id?: Types.ObjectId;
user?: Types.ObjectId;
type: string; type: string;
// id: string;
title?: string; title?: string;
isRequired: boolean; isRequired: boolean;
comment?: string; comment?: string;
content?: any; content?: any;
} }
const schema = new Schema<IQuestion>({
type:{type:String},
title: {type:String},
isRequired: {type:Boolean},
comment:{type: String},
content:{type: Object},
}, {toJSON: {
versionKey: false
}});
export default model<IQuestion>("Question", schema); const schema = new Schema<IQuestion>(
{
user: { type: Schema.Types.ObjectId, ref: "User" },
type: { type: String },
title: { type: String },
isRequired: { type: Boolean },
comment: { type: String },
content: { type: Object },
},
{
toJSON: {
versionKey: false,
},
}
);
export default model<IQuestion>("Question", schema);
import { model, Schema, Types } from "mongoose"; import { model, Schema, Types } from "mongoose";
export interface ISurvey { export interface ISurvey {
title: string; _id?: Types.ObjectId;
comment: string; title?: string;
questions: Types.ObjectId[] comment?: string;
} user: Types.ObjectId;
questions: Types.ObjectId[];
}
const schema = new Schema<ISurvey>({ const schema = new Schema<ISurvey>({
title: {type:String}, title: { type: String },
comment: {type:String}, comment: { type: String },
questions: [{ type: Schema.Types.ObjectId, ref: 'Question' }], user: { type: Schema.Types.ObjectId, ref: "User" },
}); questions: [{ type: Schema.Types.ObjectId, ref: "Question" }],
});
export default model<ISurvey>("Survey", schema);
export default model<ISurvey>("Survey", schema);
import express from "express"; import express from "express";
import { questionCtrl } from "../controllers"; import { authCtrl, questionCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router router
.route("/create") .route("/create")
.post(questionCtrl.createQuestion); .post(authCtrl.requireLogin, questionCtrl.createQuestion);
router
.route("/update/:questionId")
.put(
authCtrl.requireLogin,
authCtrl.authenticate,
questionCtrl.updateQuestion
);
router
.route("/delete/:questionId")
.delete(
authCtrl.requireLogin,
authCtrl.authenticate,
questionCtrl.deleteQuestionById
);
router.param("questionId", questionCtrl.userByQuestionId);
export default router; export default router;
import express from "express"; import express from "express";
import { surveyCtrl } from "../controllers"; import { authCtrl, surveyCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router.route("/create").post(authCtrl.requireLogin, surveyCtrl.createSurvey);
router router
.route("/create") .route("/edit/:surveyId")
.post(surveyCtrl.createSurvey); .get(authCtrl.requireLogin, authCtrl.authenticate, surveyCtrl.getSurveyById)
.put(authCtrl.requireLogin, authCtrl.authenticate, surveyCtrl.updateSurvey);
router.param("surveyId", surveyCtrl.userBySurveyId);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment