/
edumas.go
176 lines (150 loc) · 4.92 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
package backenduser
import (
"context"
"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
}
// 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)
}
//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
}
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)
}
// 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 GetAllReportID(mongoconn *mongo.Database, collection string, reportdata Report) Report {
filter := bson.M{
"nik": reportdata.Nik,
"title": reportdata.Title,
"description": reportdata.Description,
"dateOccurred": reportdata.DateOccurred,
"image": reportdata.Image,
"status": reportdata.Status,
}
reportID := atdb.GetOneDoc[Report](mongoconn, collection, filter)
return reportID
}