1
+ // contains the database-related functions for the questions API.
2
+ package database
3
+
4
+ import (
5
+ "context"
6
+ "errors"
7
+ "net/http"
8
+ "fmt"
9
+
10
+ "go.mongodb.org/mongo-driver/bson"
11
+ "go.mongodb.org/mongo-driver/mongo/options"
12
+ "peerprep/common"
13
+ )
14
+
15
+ func (db * QuestionDB ) GetAllQuestionsWithQuery (logger * common.Logger , filter bson.D ) ([]common.Question , error ) {
16
+ questionCursor , err := db .questions .Find (context .Background (), filter )
17
+
18
+ if err != nil {
19
+ logger .Log .Error ("Error retrieving questions: " , err .Error ())
20
+ return nil , err
21
+ }
22
+
23
+ var questions []common.Question
24
+
25
+ if err = questionCursor .All (context .Background (), & questions ); err != nil {
26
+ logger .Log .Error ("Error decoding questions: " , err .Error ())
27
+ return nil , err
28
+ }
29
+
30
+ return questions , nil
31
+ }
32
+
33
+ func (db * QuestionDB ) AddQuestion (logger * common.Logger , question * common.Question ) (int , error ) {
34
+ if db .QuestionExists (question ) {
35
+ logger .Log .Warn ("Cannot add question: question already exists" )
36
+ return http .StatusConflict , errors .New ("question already exists" )
37
+ }
38
+
39
+ question .ID = db .FindNextQuestionId ()
40
+
41
+ if question .ID == - 1 {
42
+ logger .Log .Error ("Could not find next question ID" )
43
+ return http .StatusBadGateway , errors .New ("could not find the next question ID" )
44
+ }
45
+
46
+ if _ , err := db .questions .InsertOne (context .Background (), question ); err != nil {
47
+ logger .Log .Error ("Error adding question" , err .Error ())
48
+ return http .StatusBadGateway , err
49
+ }
50
+
51
+ db .IncrementNextQuestionId (question .ID + 1 , logger )
52
+ return http .StatusOK , nil
53
+ }
54
+
55
+ func (db * QuestionDB ) UpsertQuestion (logger * common.Logger , question * common.Question ) (int , error ) {
56
+
57
+ filter := bson.D {bson.E {Key : "id" , Value : question .ID }}
58
+ setter := bson.M {"$set" : question }
59
+ upsert := options .Update ().SetUpsert (true )
60
+
61
+ _ , err := db .questions .UpdateOne (context .Background (), filter , setter , upsert )
62
+
63
+ if err != nil {
64
+ logger .Log .Error ("Error while upserting question" , err .Error ())
65
+ return http .StatusBadGateway , err
66
+ }
67
+
68
+ return http .StatusOK , nil
69
+ }
70
+
71
+ func (db * QuestionDB ) DeleteQuestion (logger * common.Logger , id int ) (int , error ) {
72
+ deleteStatus , err := db .questions .DeleteOne (context .Background (), bson.D {bson.E {Key : "id" , Value : id }})
73
+
74
+ if err != nil {
75
+ logger .Log .Error ("Error deleting question" , err .Error ())
76
+ return http .StatusBadGateway , err
77
+ } else if deleteStatus .DeletedCount == 0 {
78
+ msg := fmt .Sprintf ("Question with ID %d not found when deleting question" , id )
79
+ logger .Log .Warn (msg )
80
+ return http .StatusNotFound , errors .New (msg )
81
+ }
82
+
83
+ return http .StatusNoContent , nil
84
+ }
0 commit comments