1
- package routes
1
+ package handlers
2
2
3
3
import (
4
4
"context"
@@ -63,12 +63,14 @@ func NewEntry(c *gin.Context) {
63
63
}
64
64
65
65
type Entry struct {
66
- Name string `bson:"name"`
67
- Data []struct {
68
- ID int `json:"id"`
69
- Item interface {} `json:"item"`
70
- } `bson:"data"`
71
- NextID int `bson:"next_id"`
66
+ ID int `json:"id"`
67
+ Data interface {} `json:"data"`
68
+ }
69
+
70
+ type Collection struct {
71
+ Name string `bson:"name"`
72
+ Data []Entry `bson:"data"`
73
+ NextID int `bson:"next_id"`
72
74
}
73
75
74
76
mongodb_hostname := configs .Env .Mongodb_Hostname
@@ -91,7 +93,7 @@ func NewEntry(c *gin.Context) {
91
93
var err error
92
94
93
95
if true {
94
- // Parse Data -> Unmarshal JSON
96
+ // If Object field is set, parse the JSON data
95
97
err = json .Unmarshal ([]byte (userRequest .Data ), & newData )
96
98
if err != nil {
97
99
msg := fmt .Sprintf ("Error parsing JSON: %s" , err )
@@ -100,33 +102,48 @@ func NewEntry(c *gin.Context) {
100
102
}
101
103
}
102
104
103
- existingEntry := Entry {}
105
+ existingCollection := Collection {}
104
106
105
- err = collection .FindOne (ctx , bson.M {"name" : userRequest .Collection }).Decode (& existingEntry )
107
+ err = collection .FindOne (ctx , bson.M {"name" : userRequest .Collection }).Decode (& existingCollection )
106
108
107
109
var nextID int
108
-
109
110
if err == nil {
110
111
// Entry exists, update its value
111
- nextID = existingEntry .NextID
112
+ nextID = existingCollection .NextID
112
113
} else {
113
114
nextID = 0
114
115
}
115
116
116
- newEntry := struct {
117
- ID int `json:"id"`
118
- Item interface {} `json:"item"`
119
- }{
120
- ID : nextID ,
121
- Item : newData ,
122
- }
117
+ if err == nil {
118
+ // Entry exists, update its value
119
+ newEntry := Entry {
120
+ ID : nextID ,
121
+ Data : newData ,
122
+ }
123
123
124
- update := bson.M {
125
- "$push" : bson.M {"data" : newEntry },
126
- "$set" : bson.M {"next_id" : nextID + 1 },
127
- }
124
+ update := bson.M {"$push" : bson.M {"data" : newEntry }, "$set" : bson.M {"next_id" : nextID + 1 }}
125
+ _ , err := collection .UpdateOne (ctx , bson.M {"name" : userRequest .Collection }, update )
128
126
129
- _ , err = collection .UpdateOne (ctx , bson.M {"name" : userRequest .Collection }, update )
127
+ if err != nil {
128
+ c .JSON (http .StatusOK , gin.H {
129
+ "message" : "Error updating value" ,
130
+ "code" : 1 })
131
+ }
132
+ } else {
133
+ // Entry doesn't exist, create a new one
134
+ newEntry := Collection {
135
+ Name : userRequest .Collection ,
136
+ Data : []Entry {
137
+ {
138
+ ID : 0 ,
139
+ Data : newData ,
140
+ },
141
+ },
142
+ NextID : 1 ,
143
+ }
144
+
145
+ _ , err = collection .InsertOne (ctx , newEntry )
146
+ }
130
147
131
148
if err != nil {
132
149
c .JSON (http .StatusOK , gin.H {
@@ -139,3 +156,44 @@ func NewEntry(c *gin.Context) {
139
156
"message" : fmt .Sprintf ("Successfully Created/Updated Entry in %s" , userRequest .Collection ),
140
157
"code" : 0 })
141
158
}
159
+
160
+ func GetEntries (c * gin.Context ) {
161
+ type UserRequest struct {
162
+ DBName string `json:"db_name"`
163
+ Artifact string `json:"artifact"`
164
+ Collection string `json:"collection"`
165
+ }
166
+
167
+ type Entry struct {
168
+ Name string `bson:"name"`
169
+ Data []struct {
170
+ ID int `json:"id"`
171
+ Data interface {} `json:"data"`
172
+ } `bson:"data"`
173
+ }
174
+
175
+ var userRequest UserRequest
176
+ if err := c .BindJSON (& userRequest ); err != nil {
177
+ c .JSON (http .StatusBadRequest , gin.H {"error" : "Invalid request body" })
178
+ return
179
+ }
180
+
181
+ client := mongodb .GetClient ()
182
+ db := client .Database (userRequest .DBName )
183
+ collection := db .Collection (userRequest .Artifact )
184
+
185
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
186
+ defer cancel ()
187
+
188
+ entry := Entry {}
189
+ fmt .Println (userRequest )
190
+ if err := collection .FindOne (ctx , bson.M {"name" : userRequest .Collection }).Decode (& entry ); err != nil {
191
+ fmt .Println (err )
192
+ c .JSON (http .StatusBadRequest , gin.H {"error" : "Invalid request body" })
193
+ return
194
+ }
195
+
196
+ c .JSON (http .StatusOK , gin.H {
197
+ "data" : entry .Data ,
198
+ "code" : 0 })
199
+ }
0 commit comments