question.db.ts 805 Bytes
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import { Question, IQuestion } from "../models";

Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
5
6
7
8
9
10
11
12
export const findUserByQuestionId = async (questionId: string) => {
  const question = await Question.findById(questionId).populate("user");
  console.log(question);
  if (question !== null) {
    console.log(question.user);
    return question.user;
  }
  return null;
};

Jiwon Yoon's avatar
Jiwon Yoon committed
13
14
export const createQuestion = async (question: IQuestion) => {
  const newQuestion = await Question.create(question);
Jiwon Yoon's avatar
Jiwon Yoon committed
15
  return newQuestion;
Jiwon Yoon's avatar
Jiwon Yoon committed
16
};
Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
19
20
21
22
23

export const updateQuestion = async (question: IQuestion) => {
  const id = question._id;
  const newQuestion = await Question.findOneAndUpdate({ _id: id }, question);
  return newQuestion;
};

Jiwon Yoon's avatar
Jiwon Yoon committed
24
export const deleteQuestionById = async (id: string) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
  const newQuestion = await Question.findByIdAndDelete(id);
  return newQuestion;
};