/
edumas.go
396 lines (331 loc) · 12 KB
/
edumas.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package edumasbackend
import (
"context"
"errors"
"fmt"
"os"
"github.com/aiteung/atdb"
"github.com/whatsauth/watoken"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// crud
func GetAllDocs(db *mongo.Database, col string, docs interface{}) interface{} {
collection := db.Collection(col)
filter := bson.M{}
cursor, err := collection.Find(context.TODO(), filter)
if err != nil {
return fmt.Errorf("error GetAllDocs %s: %s", col, err)
}
err = cursor.All(context.TODO(), &docs)
if err != nil {
return err
}
return docs
}
func UpdateOneDoc(id primitive.ObjectID, db *mongo.Database, col string, doc interface{}) (err error) {
filter := bson.M{"_id": id}
result, err := db.Collection(col).UpdateOne(context.Background(), filter, bson.M{"$set": doc})
if err != nil {
return fmt.Errorf("error update: %v", err)
}
if result.ModifiedCount == 0 {
err = fmt.Errorf("tidak ada data yang diubah")
return
}
return nil
}
func DeleteOneDoc(_id primitive.ObjectID, db *mongo.Database, col string) error {
collection := db.Collection(col)
filter := bson.M{"_id": _id}
result, err := collection.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
}
// Admin
func CreateNewAdminRole(mongoconn *mongo.Database, collection string, admindata Admin) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPass(admindata.Password)
if err != nil {
return err
}
admindata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoconn, collection, admindata)
}
// User
func CreateNewUserRole(mongoconn *mongo.Database, collection string, userdata User) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPass(userdata.Password)
if err != nil {
return err
}
userdata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoconn, collection, userdata)
}
func CreateNewUserRoleNew(mongoconn *mongo.Database, collection string, userdata UserNew) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPass(userdata.Password)
if err != nil {
return err
}
userdata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoconn, collection, userdata)
}
//user
func CreateUserAndAddToken(privateKeyEnv string, mongoconn *mongo.Database, collection string, userdata User) error {
// Hash the password before storing it
hashedPassword, err := HashPass(userdata.Password)
if err != nil {
return err
}
userdata.Password = hashedPassword
// Create a token for the user
tokenstring, err := watoken.Encode(userdata.Username, os.Getenv(privateKeyEnv))
if err != nil {
return err
}
userdata.Token = tokenstring
// Insert the user data into the MongoDB collection
if err := atdb.InsertOneDoc(mongoconn, collection, userdata.Username); err != nil {
return nil // Mengembalikan kesalahan yang dikembalikan oleh atdb.InsertOneDoc
}
// Return nil to indicate success
return nil
}
//admin
func CreateAdminAndAddToken(privateKeyEnv string, mongoconn *mongo.Database, collection string, admindata Admin) error {
// Hash the password before storing it
hashedPassword, err := HashPass(admindata.Password)
if err != nil {
return err
}
admindata.Password = hashedPassword
// Create a token for the admin
tokenstring, err := watoken.Encode(admindata.Username, os.Getenv(privateKeyEnv))
if err != nil {
return err
}
admindata.Token = tokenstring
// Insert the admin data into the MongoDB collection
if err := atdb.InsertOneDoc(mongoconn, collection, admindata.Username); err != nil {
return nil // Mengembalikan kesalahan yang dikembalikan oleh atdb.InsertOneDoc
}
// Return nil to indicate success
return nil
}
func CreateResponse(status bool, message string, data interface{}) Response {
response := Response{
Status: status,
Message: message,
Data: data,
}
return response
}
func CreateUser(mongoconn *mongo.Database, collection string, userdata User) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPass(userdata.Password)
if err != nil {
return err
}
privateKey, publicKey := watoken.GenerateKey()
userid := userdata.Username
tokenstring, err := watoken.Encode(userid, privateKey)
if err != nil {
fmt.Println(err)
}
fmt.Println(tokenstring)
// decode token to get userid
useridstring := watoken.DecodeGetId(publicKey, tokenstring)
if useridstring == "" {
fmt.Println("expire token")
}
fmt.Println(useridstring)
userdata.Private = privateKey
userdata.Public = publicKey
userdata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoconn, collection, userdata)
}
func CreateAdmin(mongoconn *mongo.Database, collection string, admindata Admin) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPass(admindata.Password)
if err != nil {
return err
}
privateKey, publicKey := watoken.GenerateKey()
adminid := admindata.Username
tokenstring, err := watoken.Encode(adminid, privateKey)
if err != nil {
fmt.Println(err)
}
fmt.Println(tokenstring)
// decode token to get userid
adminidstring := watoken.DecodeGetId(publicKey, tokenstring)
if adminidstring == "" {
fmt.Println("expire token")
}
fmt.Println(adminidstring)
admindata.Private = privateKey
admindata.Public = publicKey
admindata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoconn, collection, admindata)
}
func GetAllUser(mongoconn *mongo.Database, collection string) []UserNew {
user := atdb.GetAllDoc[[]UserNew](mongoconn, collection)
return user
}
func FindUserUser(mongoconn *mongo.Database, collection string, userdata User) User {
filter := bson.M{
"username": userdata.Username,
}
return atdb.GetOneDoc[User](mongoconn, collection, filter)
}
func Deleteuser(mongoconn *mongo.Database, collection string, userdata2 UserNew) interface{} {
filter := bson.M{"username": userdata2.Username}
return atdb.DeleteOneDoc(mongoconn, collection, filter)
}
func UpdatedUser(mongoconn *mongo.Database, collection string, filter bson.M, userdata2 UserNew) interface{} {
updatedFilter := bson.M{"username": userdata2.Username}
return atdb.ReplaceOneDoc(mongoconn, collection, updatedFilter, userdata2)
}
func EditUser(mongoenv *mongo.Database, collname string, datauser UserNew) interface{} {
filter := bson.M{"username": datauser.Username}
return atdb.ReplaceOneDoc(mongoenv, collname, filter, datauser)
}
func GetUserFromID(db *mongo.Database, col string, _id primitive.ObjectID) (*UserNew, error) {
cols := db.Collection(col)
filter := bson.M{"_id": _id}
userlist := new(UserNew)
err := cols.FindOne(context.Background(), filter).Decode(userlist)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, fmt.Errorf("no data found for ID %s", _id.Hex())
}
return nil, fmt.Errorf("error retrieving data for ID %s: %s", _id.Hex(), err.Error())
}
return userlist, nil
}
// Report
func CreateNewReport(mongoconn *mongo.Database, collection string, reportdata Report) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, reportdata)
}
// Report function
func CreateReport(mongoconn *mongo.Database, collection string, reportdata Report) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, reportdata)
}
// Report function
func insertReport(mongoconn *mongo.Database, collection string, reportdata Report) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, reportdata)
}
func DeleteReport(mongoconn *mongo.Database, collection string, reportdata Report) interface{} {
filter := bson.M{"nik": reportdata.Nik}
return atdb.DeleteOneDoc(mongoconn, collection, filter)
}
func UpdatedReport(mongoconn *mongo.Database, collection string, filter bson.M, reportdata Report) interface{} {
updatedFilter := bson.M{"nik": reportdata.Nik}
return atdb.ReplaceOneDoc(mongoconn, collection, updatedFilter, reportdata)
}
func GetAllReport(mongoconn *mongo.Database, collection string) []Report {
report := atdb.GetAllDoc[[]Report](mongoconn, collection)
return report
}
func FindReport(mongoconn *mongo.Database, collection string, reportdata Report) Report {
filter := bson.M{
"nik": reportdata.Nik,
}
return atdb.GetOneDoc[Report](mongoconn, collection, filter)
}
func GetAllReportID(mongoconn *mongo.Database, collection string, reportdata Report) Report {
filter := bson.M{
"nik": reportdata.Nik,
}
reportID := atdb.GetOneDoc[Report](mongoconn, collection, filter)
return reportID
}
func GetOneReportNik(mongoconn *mongo.Database, collection string, reportdata Report) Report {
filter := bson.M{"nik": reportdata.Nik}
return atdb.GetOneDoc[Report](mongoconn, collection, filter)
}
func GetOneReport(mongoconn *mongo.Database, collection string, reportdata Report) Report {
filter := bson.M{"nik": reportdata.Nik}
return atdb.GetOneDoc[Report](mongoconn, collection, filter)
}
func GetReportFromID(db *mongo.Database, col string, _id primitive.ObjectID) (*Report, error) {
cols := db.Collection(col)
filter := bson.M{"_id": _id}
reportlist := new(Report)
err := cols.FindOne(context.Background(), filter).Decode(reportlist)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, fmt.Errorf("no data found for ID %s", _id.Hex())
}
return nil, fmt.Errorf("error retrieving data for ID %s: %s", _id.Hex(), err.Error())
}
return reportlist, nil
}
func GetReportFromIDs(db *mongo.Database, col string, _id int) (*Report, error) {
cols := db.Collection(col)
filter := bson.M{"nik": _id}
reportlist := new(Report)
err := cols.FindOne(context.Background(), filter).Decode(reportlist)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, fmt.Errorf("no data found for ID %s", _id)
}
return nil, fmt.Errorf("error retrieving data for ID %s: %s", _id, err.Error())
}
return reportlist, nil
}
// func GetOneReportData(mongoconn *mongo.Database, colname, Nik string) (dest Report) {
// filter := bson.M{"nik": Nik}
// dest = atdb.GetOneDoc[Report](mongoconn, colname, filter)
// return
// }
func GetOneReportData(mongoconn *mongo.Database, collname string, reportdata Report) Report {
filter := bson.M{"nik": reportdata.Nik}
return atdb.GetOneDoc[Report](mongoconn, collname, filter)
}
// Function Tanggapan
// Tanggapan
func CreateNewTanggapan(mongoconn *mongo.Database, collection string, tanggapandata Tanggapan) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, tanggapandata)
}
// Tanggapan function
func insertTanggapan(mongoconn *mongo.Database, collection string, tanggapandata Tanggapan) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, tanggapandata)
}
func DeleteTanggapan(mongoconn *mongo.Database, collection string, tanggapandata Tanggapan) interface{} {
filter := bson.M{"nik": tanggapandata.Nik}
return atdb.DeleteOneDoc(mongoconn, collection, filter)
}
func UpdatedTanggapan(mongoconn *mongo.Database, collection string, filter bson.M, tanggapandata Tanggapan) interface{} {
updatedFilter := bson.M{"nik": tanggapandata.Nik}
return atdb.ReplaceOneDoc(mongoconn, collection, updatedFilter, tanggapandata)
}
func GetAllTanggapan(mongoconn *mongo.Database, collection string) []Tanggapan {
tanggapan := atdb.GetAllDoc[[]Tanggapan](mongoconn, collection)
return tanggapan
}
func GetOneTanggapan(mongoconn *mongo.Database, collection string, tanggapandata Tanggapan) interface{} {
filter := bson.M{"nik": tanggapandata.Nik}
return atdb.GetOneDoc[Report](mongoconn, collection, filter)
}
func GetAllTanggapanID(mongoconn *mongo.Database, collection string, tanggapandata Tanggapan) Tanggapan {
filter := bson.M{
"nik": tanggapandata.Nik,
"description": tanggapandata.Description,
"daterespons": tanggapandata.DateRespons,
}
tanggapanID := atdb.GetOneDoc[Tanggapan](mongoconn, collection, filter)
return tanggapanID
}