-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
222 lines (190 loc) · 6.75 KB
/
mongo.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
package peda
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"os"
"github.com/aiteung/atdb"
"github.com/whatsauth/watoken"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func SetConnection(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
return atdb.MongoConnect(DBmongoinfo)
}
func CompareUsername(mongoenv *mongo.Database, collname, username string) bool {
filter := bson.M{"username": username}
err := atdb.GetOneDoc[User](mongoenv, collname, filter)
users := err.Username
if users == "" {
return false
}
return true
}
func GetAllBangunanLineString(mongoenv *mongo.Database, collname string) []GeoJson {
lokasi := atdb.GetAllDoc[[]GeoJson](mongoenv, collname)
return lokasi
}
func GetAllKegiatan(mongoenv *mongo.Database, collname string) []Kegiatan {
kegiatan := atdb.GetAllDoc[[]Kegiatan](mongoenv, collname)
return kegiatan
}
func GetAllJadwal(mongoenv *mongo.Database, collname string) []Jadwal {
lokasi := atdb.GetAllDoc[[]Jadwal](mongoenv, collname)
return lokasi
}
func PostPoint(mongoconn *mongo.Database, collection string, pointdata GeoJsonPoint) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, pointdata)
}
func PostLinestring(mongoconn *mongo.Database, collection string, linestringdata GeoJsonLineString) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, linestringdata)
}
func PostPolygon(mongoconn *mongo.Database, collection string, polygondata GeoJsonPolygon) interface{} {
return atdb.InsertOneDoc(mongoconn, collection, polygondata)
}
func MemasukkanKoordinat(MongoConn *mongo.Database, colname string, coordinate []float64, name, volume, tipe string) (InsertedID interface{}) {
req := new(Coordinate)
req.Type = tipe
req.Coordinates = coordinate
req.Name = name
ins := atdb.InsertOneDoc(MongoConn, colname, req)
return ins
}
func GetNameAndPassowrd(mongoenv *mongo.Database, collname string) []User {
user := atdb.GetAllDoc[[]User](mongoenv, collname)
return user
}
func GetAllUser(mongoenv *mongo.Database, collname string) []User {
user := atdb.GetAllDoc[[]User](mongoenv, collname)
return user
}
func CreateNewUserRole(mongoenv *mongo.Database, collname string, userdata User) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPassword(userdata.Password)
if err != nil {
return err
}
userdata.Password = hashedPassword
// Insert the user data into the database
return atdb.InsertOneDoc(mongoenv, collname, userdata)
}
func CreateUserAndAddedToeken(privatekey string, mongoenv *mongo.Database, collname string, userdata User) interface{} {
// Hash the password before storing it
hashedPassword, err := HashPassword(userdata.Password)
if err != nil {
return err
}
userdata.Password = hashedPassword
// Insert the user data into the database
atdb.InsertOneDoc(mongoenv, collname, userdata)
// Create a token for the user
tokenstring, err := watoken.Encode(userdata.Username, os.Getenv(privatekey))
if err != nil {
return err
}
userdata.Token = tokenstring
// Update the user data in the database
return atdb.ReplaceOneDoc(mongoenv, collname, bson.M{"username": userdata.Username}, userdata)
}
func DeleteUser(mongoenv *mongo.Database, collname string, userdata User) interface{} {
filter := bson.M{"username": userdata.Username}
return atdb.DeleteOneDoc(mongoenv, collname, filter)
}
func ReplaceOneDoc(mongoenv *mongo.Database, collname string, filter bson.M, userdata User) interface{} {
return atdb.ReplaceOneDoc(mongoenv, collname, filter, userdata)
}
func FindUser(mongoenv *mongo.Database, collname string, userdata User) User {
filter := bson.M{"username": userdata.Username}
return atdb.GetOneDoc[User](mongoenv, collname, filter)
}
func FindUserUser(mongoenv *mongo.Database, collname string, userdata User) User {
filter := bson.M{
"username": userdata.Username,
}
return atdb.GetOneDoc[User](mongoenv, collname, filter)
}
func IsPasswordValid(mongoenv *mongo.Database, collname string, userdata User) bool {
filter := bson.M{"username": userdata.Username}
res := atdb.GetOneDoc[User](mongoenv, collname, filter)
hashChecker := CheckPasswordHash(userdata.Password, res.Password)
return hashChecker
}
func InsertUserdata(mongoenv *mongo.Database, collname, username, role, password string) (InsertedID interface{}) {
req := new(User)
req.Username = username
req.Password = password
req.Role = role
return atdb.InsertOneDoc(mongoenv, collname, req)
}
func usernameExists(mongoenv, dbname string, userdata User) bool {
mconn := SetConnection(mongoenv, dbname).Collection("user")
filter := bson.M{"username": userdata.Username}
var user User
err := mconn.FindOne(context.Background(), filter).Decode(&user)
return err == nil
}
func PostStructWithToken[T any](tokenkey string, tokenvalue string, structname interface{}, urltarget string) (result T, errormessage string) {
client := http.Client{}
mJson, _ := json.Marshal(structname)
req, err := http.NewRequest("POST", urltarget, bytes.NewBuffer(mJson))
if err != nil {
errormessage = "http.NewRequest Got error :" + err.Error()
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add(tokenkey, tokenvalue)
resp, err := client.Do(req)
if err != nil {
errormessage = "client.Do(req) Error occured. Error is :" + err.Error()
return
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
errormessage = "Error Read Data data from request." + err.Error()
return
}
if er := json.Unmarshal(respBody, &result); er != nil {
errormessage = string(respBody) + "Error Unmarshal from Response : " + er.Error()
}
return
}
func CreateResponse(status bool, message string, data interface{}) Jaja {
response := Jaja{
Status: status,
Message: message,
Data: data,
}
return response
}
//-----------------------test buat project, nanti hapus kalo udah
func GetAllBerita(mongoenv *mongo.Database, collname string) []Berita {
berita := atdb.GetAllDoc[[]Berita](mongoenv, collname)
return berita
}
func FindBerita(mongoenv *mongo.Database, collname string, databerita Berita) Berita {
filter := bson.M{"id": databerita.ID}
return atdb.GetOneDoc[Berita](mongoenv, collname, filter)
}
func idBeritaExists(mongoenv, dbname string, databerita Berita) bool {
mconn := SetConnection(mongoenv, dbname).Collection("berita")
filter := bson.M{"id": databerita.ID}
var berita Berita
err := mconn.FindOne(context.Background(), filter).Decode(&berita)
return err == nil
}
func InsertBerita(mongoenv *mongo.Database, collname, id, kategori, judul, preview, konten string) (InsertedID interface{}) {
req := new(Berita)
req.ID = id
req.Kategori = kategori
req.Judul = judul
req.Preview = preview
req.Konten = konten
return atdb.InsertOneDoc(mongoenv, collname, req)
}