-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
211 lines (187 loc) · 5.97 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
package module
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"github.com/bimbingankonseling/bekeekons/model"
"github.com/badoux/checkmail"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/crypto/argon2"
)
// var MongoString string = os.Getenv("MONGOSTRING")
func MongoConnect(MongoString, dbname string) *mongo.Database {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(os.Getenv(MongoString)))
if err != nil {
fmt.Printf("MongoConnect: %v\n", err)
}
return client.Database(dbname)
}
// 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 InsertOneDoc(db *mongo.Database, col string, doc interface{}) (insertedID primitive.ObjectID, err error) {
result, err := db.Collection(col).InsertOne(context.Background(), doc)
if err != nil {
return insertedID, fmt.Errorf("kesalahan server : insert")
}
insertedID = result.InsertedID.(primitive.ObjectID)
return insertedID, nil
}
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
}
// signup
// func SignUpRegistrasi(db *mongo.Database, insertedDoc model.Registrasi) error {
// objectId := primitive.NewObjectID()
// if insertedDoc.NamaLengkap == "" || insertedDoc.NomorHP == "" || insertedDoc.TanggalLahir == "" || insertedDoc.Alamat == "" || insertedDoc.NIM == "" {
// return fmt.Errorf("Dimohon untuk melengkapi data")
// }
// registrasi := bson.M{
// "namalengkap": insertedDoc.NamaLengkap,
// "nomorhp": insertedDoc.NomorHP,
// "tanggallahir": insertedDoc.TanggalLahir,
// "alamat": insertedDoc.Alamat,
// "nim": insertedDoc.NIM,
// }
// _, err = InsertOneDoc(db, "user", user)
// if err != nil {
// return fmt.Errorf("kesalahan server")
// }
// _, err = InsertOneDoc(db, "registrasi", registrasi)
// if err != nil {
// return fmt.Errorf("kesalahan server")
// }
// return nil
// }
// login
func LogIn(db *mongo.Database, insertedDoc model.User) (user model.User, err error) {
if insertedDoc.Username == "" || insertedDoc.Password == "" {
return user, fmt.Errorf("Dimohon untuk melengkapi data")
}
if err = checkmail.ValidateFormat(insertedDoc.Username); err != nil {
return user, fmt.Errorf("Username tidak valid")
}
existsDoc, err := GetUserFromEmail(insertedDoc.Username, db)
if err != nil {
return
}
hash := argon2.IDKey([]byte(insertedDoc.Password))
if hex.EncodeToString(hash) != existsDoc.Password {
return user, fmt.Errorf("password salah")
}
return existsDoc, nil
}
//Reservasi
func InsertReservasi(iduser primitive.ObjectID, db *mongo.Database, insertedDoc model.Reservasi) error {
if insertedDoc.Nama == "" || insertedDoc.Notelp == "" || insertedDoc.TTL == "" || insertedDoc.Status == "" || insertedDoc.Keluhan == "" {
return fmt.Errorf("mohon untuk melengkapi data")
}
reser := bson.M{
"nama": insertedDoc.Nama,
"notelp": insertedDoc.Notelp,
"ttl": insertedDoc.TTL,
"status": insertedDoc.Status,
"keluhan": insertedDoc.Keluhan,
}
_, err := InsertOneDoc(db, "reservasi", reser)
if err != nil {
return fmt.Errorf("error saat menyimpan data reservasi: %s", err)
}
return nil
}
func UpdateReservasi(idparam, iduser primitive.ObjectID, db *mongo.Database, insertedDoc model.Reservasi) error {
_, err := GetReservasiFromID(idparam, db)
if err != nil {
return err
}
if insertedDoc.Nama == "" || insertedDoc.Notelp == "" || insertedDoc.TTL == "" || insertedDoc.Status == "" || insertedDoc.Keluhan == "" {
return fmt.Errorf("mohon untuk melengkapi data")
}
reser := bson.M{
"nama": insertedDoc.Nama,
"notelp": insertedDoc.Notelp,
"ttl": insertedDoc.TTL,
"status": insertedDoc.Status,
"keluhan": insertedDoc.Keluhan,
}
err = UpdateOneDoc(idparam, db, "reservasi", reser)
if err != nil {
return err
}
return nil
}
func DeleteReservasi(idparam, iduser primitive.ObjectID, db *mongo.Database) error {
_, err := GetReservasiFromID(idparam, db)
if err != nil {
return err
}
err = DeleteOneDoc(idparam, db, "reservasi")
if err != nil {
return err
}
return nil
}
func GetAllReservasi(db *mongo.Database) (tiket []model.Reservasi, err error) {
collection := db.Collection("reservasi")
filter := bson.M{}
cursor, err := collection.Find(context.TODO(), filter)
if err != nil {
return reservasi, fmt.Errorf("error GetAllReservasi mongo: %s", err)
}
err = cursor.All(context.TODO(), &tiket)
if err != nil {
return reservasi, fmt.Errorf("error GetAllReservasi context: %s", err)
}
return reservasi, nil
}
func GetReservasiFromID(_id primitive.ObjectID, db *mongo.Database) (doc model.Reservasi, err error) {
collection := db.Collection("reservasi")
filter := bson.M{"_id": _id}
err = collection.FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
if err == mongo.ErrNoDocuments {
return doc, fmt.Errorf("_id tidak ditemukan")
}
return doc, fmt.Errorf("kesalahan server")
}
return doc, nil
}