@@ -9,13 +9,11 @@ const { validateObjectId } = require("../utils/validation");
99exports . getQuestions = async ( req , res ) => {
1010 try {
1111
12- const page = parseInt ( req . query . page , 10 ) || 1 ;
13- const pageSize = parseInt ( req . query . pageSize , 10 ) || 10 ;
12+ let { answerFilter } = req . query ;
13+
1414
1515 let questions = await Question
1616 . find ( )
17- . skip ( ( page - 1 ) * pageSize )
18- . limit ( pageSize )
1917 . sort ( "-createdAt" )
2018 . populate ( "questioner" , "name" )
2119 . lean ( ) ;
@@ -28,6 +26,17 @@ exports.getQuestions = async (req, res) => {
2826 }
2927
3028 questions = await Promise . all ( questions . map ( question => addExtraInfo ( question ) ) ) ;
29+
30+ if ( answerFilter == "noAnswer" ) {
31+ questions = questions . filter ( question => question . ansCount == 0 ) ;
32+ }
33+ else if ( answerFilter == "hasAnswer" ) {
34+ questions = questions . filter ( question => question . ansCount > 0 ) ;
35+ }
36+ else if ( answerFilter == "hasAcceptedAnswer" ) {
37+ questions = questions . filter ( question => question . acceptedAnsCount > 0 ) ;
38+ }
39+
3140 res . status ( 200 ) . json ( { questions, msg : "Questions found successfully" } ) ;
3241 }
3342 catch ( err ) {
@@ -179,3 +188,61 @@ exports.getQuestionsOfCurrentUser = async (req, res) => {
179188 }
180189}
181190
191+
192+ exports . getAnswersByQuestion = async ( req , res ) => {
193+ try {
194+ const questionId = req . params . qid ;
195+
196+ if ( ! validateObjectId ( questionId ) ) {
197+ return res . status ( 400 ) . json ( { msg : "Question id not valid" } ) ;
198+ }
199+
200+ const question = await Question . findById ( questionId ) ;
201+ if ( ! question ) {
202+ return res . status ( 400 ) . json ( { msg : "No question found.." } ) ;
203+ }
204+
205+ const answers = await Answer . find ( { question : questionId } ) . populate ( "answerer" , "-password" ) ;
206+ res . status ( 200 ) . json ( { answers, msg : "Answers found successfully" } ) ;
207+ }
208+ catch ( err ) {
209+ console . error ( err ) ;
210+ return res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
211+ }
212+ }
213+
214+
215+ exports . postAnswer = async ( req , res ) => {
216+ try {
217+ const questionId = req . params . qid ;
218+ const { text } = req . body ;
219+ const userId = req . user . id ;
220+ if ( ! text ) {
221+ return res . status ( 400 ) . json ( { msg : "Answer can't be empty" } ) ;
222+ }
223+
224+ if ( ! validateObjectId ( questionId ) ) {
225+ return res . status ( 400 ) . json ( { msg : "invalid Question id" } ) ;
226+ }
227+
228+ const question = await Question . findById ( questionId ) ;
229+ if ( ! question ) {
230+ return res . status ( 400 ) . json ( { msg : "No question found.." } ) ;
231+ }
232+
233+ if ( question . questioner == req . user . id ) {
234+ return res . status ( 400 ) . json ( { msg : "You can't post answer to your own question!!" } ) ;
235+ }
236+
237+ const answer = await Answer . create ( { question : questionId , answerer : userId , text } ) ;
238+ await Activity . create ( { user : req . user . id , activityType : activityEnum . CREATED_ANSWER , answer : answer . _id } ) ;
239+
240+ res . status ( 200 ) . json ( { answer, msg : "Answer posted successfully" } ) ;
241+
242+ }
243+ catch ( err ) {
244+ console . error ( err ) ;
245+ return res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
246+ }
247+ }
248+
0 commit comments