-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
173 lines (156 loc) · 4.02 KB
/
db.go
File metadata and controls
173 lines (156 loc) · 4.02 KB
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
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type Data map[string]interface{}
//TODO
//For checking the fields are valid or not
var fields = map[string]bool{
"firstName":true,
"lastName":true,
"middleName":true,
"fatherMiddleName":true,
"fatherFirstName":true,
"fatherLastName":true,
"age":true,
"fullName":true,
"fatherFullName":true,
"boardName":true,
"stateOfDomicile":true,
"yearOfPassing":true,
"rollNumber":true,
"address":true,
"state":true,
"district":true,
"city":true,
"pincode":true,
"houseNo":true,
"village":true,
"gender":true,
"homeDistrict":true,
"dob":true,
}
type Storage interface {
InsertApplication(ctx context.Context,appilcation *Application)error
FetchAll(ctx context.Context,filters Data)([]PostApplication,error)
Delete(ctx context.Context, id string)error
GetApplicationById(ctx context.Context,id string)(*PostApplication,error)
UpdateApplication(ctx context.Context, application Data,id string)error
}
type mongoStore struct{
database *mongo.Database
}
func (store *mongoStore)InsertApplication(ctx context.Context,application *Application)error{
if !application.ValidateGender() {
return errors.New("only male and female genders are acceptable")
}
postApplication:=application.NewApplicationPost()
postApplication.Gender = strings.ToLower(application.Gender)
coll:=store.database.Collection("application")
_,err:=coll.InsertOne(ctx,postApplication)
if err!=nil{
return err
}
return nil
}
func(store *mongoStore)FetchAll(ctx context.Context,filters Data)([]PostApplication,error){
coll:=store.database.Collection("application")
bsonFilters:=makeBson(filters)
cursor,err:=coll.Find(ctx,bsonFilters)
if err!=nil{
return nil,err
}
var applications []PostApplication
if err:=cursor.All(ctx,&applications);err!=nil{
return nil,err
}
if applications==nil{
return nil,errors.New("the filters you provide does not exists check your fields again")
}
return applications,nil
}
func (store *mongoStore)Delete(ctx context.Context ,id string)error{
filter:=bson.M{"id":id}
coll:=store.database.Collection("application")
deletedDocumentNum,err:=coll.DeleteOne(ctx,filter)
if deletedDocumentNum.DeletedCount==0{
return errors.New("the id referencing to the document in not correct such document does not exists")
}
if err!=nil{
return err
}
return nil
}
func(store *mongoStore)GetApplicationById(ctx context.Context,id string)(*PostApplication,error){
var application PostApplication
coll:=store.database.Collection("application")
filters:=bson.M{"id":id}
result:=coll.FindOne(ctx,filters)
err:=result.Decode(&application)
if err!=nil{
return nil,err
}
return &application,nil
}
func(store *mongoStore)UpdateApplication(ctx context.Context,filters Data,id string)error{
coll:=store.database.Collection("application")
_,err:=store.GetApplicationById(ctx,id)
if err!=nil{
return err
}
if !checkFields(filters){
return errors.New("the filters you provide does not exists check your fields again")
}
options:=bson.M{"id":id}
fields:=makeBson(filters)
update:=bson.M{
"$set":fields,
}
_,err=coll.UpdateOne(ctx,options,update)
if err!=nil{
return err
}
return nil
}
func NewMongoStore(ctx context.Context) (*mongoStore,error){
godotenv.Load()
mongoUrl:=os.Getenv("MONGO_URL")
client,err:=mongo.Connect(ctx,options.Client().ApplyURI(mongoUrl))
if err !=nil{
return nil,err
}
if err:=client.Ping(ctx,readpref.Primary());err!=nil{
return nil,err
}
fmt.Println("Connected to the database")
db:=client.Database("exam-application")
return &mongoStore{
database: db,
},nil
}
func makeBson(filters Data)bson.M{
result:=bson.M{}
for k,v:=range filters{
lowerCaseKey := strings.ToLower(k)
result[lowerCaseKey] = v
}
return result
}
func checkFields(data Data)bool{
for k,_:= range data{
ok:=fields[k]
if !ok{
return false
}
}
return true
}