/
controller.go
362 lines (316 loc) · 10.2 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package module
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/aiteung/atdb"
"github.com/stefanymelda/be_kuesioner/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var MongoString string = os.Getenv("MONGOSTRING")
var MongoInfo = atdb.DBInfo{
DBString: MongoString,
DBName: "data_db",
}
var MongoConn = atdb.MongoConnect(MongoInfo)
func InsertOneDoc(db *mongo.Database, collection string, doc interface{}) (insertedID interface{}) {
insertResult, err := db.Collection(collection).InsertOne(context.TODO(), doc)
if err != nil {
fmt.Printf("InsertOneDoc: %v\n", err)
}
return insertResult.InsertedID
}
// func InsertKuesioner(db *mongo.Database, col string, lat float64, long float64, lokasi string, email string, status string, biodata model.Responden) (InsertedID interface{}) {
// var kuesioner model.Kuesioner
// kuesioner.Latitude = lat
// kuesioner.Longitude = long
// kuesioner.Location = lokasi
// kuesioner.Email = email
// kuesioner.Datetime = primitive.NewDateTimeFromTime(time.Now().UTC())
// kuesioner.Status = status
// kuesioner.Biodata = biodata
// return InsertOneDoc(db, col, kuesioner)
// }
func InsertResponden(db *mongo.Database, col string, nama string, jenis_kelamin string, usia int, email string, phone_number string) (InsertedID interface{}) {
var responden model.Responden
responden.Nama = nama
responden.Jenis_kelamin = jenis_kelamin
responden.Usia = usia
responden.Email = email
responden.Phone_number = phone_number
// responden.Jam_pengisian = jam_pengisian
// responden.Hari_pengisian = hari_pengisian
return InsertOneDoc(db, col, responden)
}
// func InsertJamPengisian(db *mongo.Database, col string, durasi int, jam_mulai string, jam_selesai string, deskripsi string) (InsertedID interface{}) {
// var jampengisian model.JamPengisian
// jampengisian.Durasi = durasi
// jampengisian.Jam_mulai = jam_mulai
// jampengisian.Jam_selesai = jam_selesai
// jampengisian.Deskripsi = deskripsi
// return InsertOneDoc(db, col, jampengisian)
// }
func InsertLokasi(db *mongo.Database, col string, nama string, kategori string) (InsertedID interface{}) {
var lokasi model.Lokasi
lokasi.Nama = nama
lokasi.Kategori = kategori
return InsertOneDoc(db, col, lokasi)
}
func InsertQuestion(db *mongo.Database, col string, nomor int, text string, options string) (InsertedID interface{}) {
var question model.Question
question.Nomor = nomor
question.Text = text
question.Options = options
return InsertOneDoc(db, col, question)
}
func InsertAnswer(db *mongo.Database, col string, question_nomor int, text2 string) (InsertedID interface{}) {
var answer model.Answer
answer.Question_Nomor = question_nomor
answer.Text2 = text2
return InsertOneDoc(db, col, answer)
}
func InsertSurvey(db *mongo.Database, col string, kode int, title string, soal model.Question) (InsertedID interface{}) {
var survey model.Survey
survey.Kode = kode
survey.Title = title
survey.Soal = soal
return InsertOneDoc(db, col, survey)
}
func GetAllSurvey(db *mongo.Database, col string) (data []model.Survey) {
data_survey := db.Collection(col)
filter := bson.M{}
cursor, err := data_survey.Find(context.TODO(), filter)
if err != nil {
fmt.Println("GetALLData :", err)
}
err = cursor.All(context.TODO(), &data)
if err != nil {
fmt.Println(err)
}
return
}
func GetKuesionerFromStatus(status string, db *mongo.Database, col string) (ksr model.Kuesioner) {
kuesioner := db.Collection(col)
filter := bson.M{"status": status}
err := kuesioner.FindOne(context.TODO(), filter).Decode(&ksr)
if err != nil {
fmt.Printf("getKuesionerFromStatus: %v\n", err)
}
return ksr
}
func GetRespondenFromUsia(usia int, db *mongo.Database, col string) (rsp model.Responden) {
responden := db.Collection(col)
filter := bson.M{"usia": usia}
err := responden.FindOne(context.TODO(), filter).Decode(&rsp)
if err != nil {
fmt.Printf("getRespondenFromUsia: %v\n", err)
}
return rsp
}
// func GetJamPengisianFromDurasi(durasi int, db *mongo.Database, col string) (jp model.JamPengisian) {
// jampengisian := db.Collection(col)
// filter := bson.M{"durasi": durasi}
// err := jampengisian.FindOne(context.TODO(), filter).Decode(&jp)
// if err != nil {
// fmt.Printf("getJamPengisianFromDurasi: %v\n", err)
// }
// return jp
// }
func GetLokasiFromNama(nama string, db *mongo.Database, col string) (lks model.Lokasi) {
lokasi := db.Collection(col)
filter := bson.M{"nama": nama}
err := lokasi.FindOne(context.TODO(), filter).Decode(&lks)
if err != nil {
fmt.Printf("getLokasiFromNama: %v\n", err)
}
return lks
}
func GetAllKuesionerFromEmail(email string, db *mongo.Database, col string) (aksr []model.Kuesioner) {
kuesioner := db.Collection(col)
filter := bson.M{"email": email}
cursor, err := kuesioner.Find(context.TODO(), filter)
if err != nil {
fmt.Printf("GetAllKuesionerFromEmail: %v\n", err)
}
err = cursor.All(context.TODO(), &aksr)
if err != nil{
fmt.Println(err)
}
return
}
func GetAllKuesioner(db *mongo.Database, col string) (data []model.Kuesioner) {
data_kuesioner := db.Collection(col)
filter := bson.M{}
cursor, err := data_kuesioner.Find(context.TODO(), filter)
if err != nil {
fmt.Println("GetALLData :", err)
}
err = cursor.All(context.TODO(), &data)
if err != nil {
fmt.Println(err)
}
return
}
//TUGASBESAR
//KUESIONER
func InsertKuesioner(db *mongo.Database, col string, lat float64, long float64, lokasi string, email string, status string, biodata model.Responden) (insertedID primitive.ObjectID, err error) {
kuesioner := bson.M{
"longitude": long,
"latitude": lat,
"location": lokasi,
"email": email,
"datetime": primitive.NewDateTimeFromTime(time.Now().UTC()),
"status" : status,
"biodata": biodata,
}
result, err := db.Collection(col).InsertOne(context.Background(), kuesioner)
if err != nil {
fmt.Printf("InsertKuesioner: %v\n", err)
return
}
insertedID = result.InsertedID.(primitive.ObjectID)
return insertedID, nil
}
func UpdateKuesioner(db *mongo.Database, col string, id primitive.ObjectID, lat float64, long float64, lokasi string, email string, status string, biodata model.Responden) (err error) {
filter := bson.M{"_id": id}
update := bson.M{
"$set": bson.M{
"longitude": long,
"latitude": lat,
"location": lokasi,
"email": email,
"status": status,
"biodata": biodata,
},
}
result, err := db.Collection(col).UpdateOne(context.Background(), filter, update)
if err != nil {
fmt.Printf("UpdateKuesioner: %v\n", err)
return
}
if result.ModifiedCount == 0 {
err = errors.New("No data has been changed with the specified ID")
return
}
return nil
}
func GetKuesionerFromID(_id primitive.ObjectID, db *mongo.Database, col string) (ksr model.Kuesioner, errs error) {
responden := db.Collection(col)
filter := bson.M{"_id": _id}
err := responden.FindOne(context.TODO(), filter).Decode(&ksr)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return ksr, fmt.Errorf("no data found for ID %s", _id)
}
return ksr, fmt.Errorf("error retrieving data for ID %s: %s", _id, err.Error())
}
return ksr, nil
}
func DeleteKuesionerByID(_id primitive.ObjectID, db *mongo.Database, col string) error {
responden := db.Collection(col)
filter := bson.M{"_id": _id}
result, err := responden.DeleteOne(context.TODO(), filter)
if err != nil {
return fmt.Errorf("error deleting data for ID %s: %s", _id, err.Error())
}
if result.DeletedCount == 0 {
return fmt.Errorf("data with ID %s not found", _id)
}
return nil
}
//ENDKUESIONER
//SURVEY
func InsertSurvey1(db *mongo.Database, col string, kode int, title string, soal model.Question) (insertedID primitive.ObjectID, err error) {
survey := bson.M{
"kode": kode,
"title": title,
"soal": soal,
}
result, err := db.Collection(col).InsertOne(context.Background(), survey)
if err != nil {
fmt.Printf("InsertSurvey1: %v\n", err)
return
}
insertedID = result.InsertedID.(primitive.ObjectID)
return insertedID, nil
}
func UpdateSurvey(db *mongo.Database, id primitive.ObjectID, col string, kode int, title string, soal model.Question) (err error) {
filter := bson.M{"_id": id}
update := bson.M{
"$set": bson.M{
"kode": kode,
"title": title,
"soal": soal,
},
}
result, err := db.Collection(col).UpdateOne(context.Background(), filter, update)
if err != nil {
fmt.Printf("UpdateSurvey: %v\n", err)
return
}
if result.ModifiedCount == 0 {
err = errors.New("No data has been changed with the specified ID")
return
}
return nil
}
func GetSurveyFromID(_id primitive.ObjectID, db *mongo.Database, col string) (ksr model.Survey, errs error) {
survey := db.Collection(col)
filter := bson.M{"_id": _id}
err := survey.FindOne(context.TODO(), filter).Decode(&ksr)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return ksr, fmt.Errorf("no data found for ID %s", _id)
}
return ksr, fmt.Errorf("error retrieving data for ID %s: %s", _id, err.Error())
}
return ksr, nil
}
func DeleteSurveyByID(_id primitive.ObjectID, db *mongo.Database, col string) error {
question := db.Collection(col)
filter := bson.M{"_id": _id}
result, err := question.DeleteOne(context.TODO(), filter)
if err != nil {
return fmt.Errorf("error deleting data for ID %s: %s", _id, err.Error())
}
if result.DeletedCount == 0 {
return fmt.Errorf("data with ID %s not found", _id)
}
return nil
}
//ENDSURVEY
//ENDTUGASBESAR
func InsertAdmin(db *mongo.Database, col string, username string, password string) (insertedID primitive.ObjectID, err error) {
admin := bson.M{
"username": username,
"password": password,
}
result, err := db.Collection(col).InsertOne(context.Background(), admin)
if err != nil {
fmt.Printf("InsertAdmin: %v\n", err)
return
}
insertedID = result.InsertedID.(primitive.ObjectID)
return insertedID, nil
}
//LogAdmin
func LogAdmin(db *mongo.Database, col string, username string, password string) (authenticated bool, err error) {
filter := bson.M{
"username": username,
"password": password,
}
result, err := db.Collection(col).CountDocuments(context.Background(), filter)
if err != nil {
fmt.Printf("LogAdmin: %v\n", err)
return false, err
}
if result == 1 {
return true, nil
}
return false, nil
}
//ENDLogAdmin