1
1
package mongoconnect
2
2
3
3
import (
4
- "fmt"
5
4
"context"
6
- "time"
7
- // "go.mongodb.org/mongo-driver/bson"
8
- "go.mongodb.org/mongo-driver/mongo"
9
- "go.mongodb.org/mongo-driver/mongo/options"
10
- "go.mongodb.org/mongo-driver/mongo/readpref"
5
+ "fmt"
6
+ "net/http"
7
+ "time"
8
+
9
+ // "go.mongodb.org/mongo-driver/bson"
10
+ "github.com/gin-gonic/gin"
11
+ "go.mongodb.org/mongo-driver/mongo"
12
+ "go.mongodb.org/mongo-driver/mongo/options"
13
+ "go.mongodb.org/mongo-driver/mongo/readpref"
11
14
)
12
15
16
+ type marksData struct {
17
+ rollNo int
18
+ maths int
19
+ science int
20
+ computer int
21
+ }
22
+
23
+ // albums slice to seed record album data.
24
+ var usermarks = []marksData {
25
+ {rollNo : 2 , maths : 2 , science : 2 , computer : 56 },
26
+ {rollNo : 32 , maths : 2 , science : 2 , computer : 17 },
27
+ {rollNo : 3 , maths : 1 , science : 2 , computer : 39 },
28
+ }
13
29
14
30
// This is a user defined method to close resources.
15
31
// This method closes mongoDB connection and cancel context.
16
32
func Close (client * mongo.Client , ctx context.Context ,
17
- cancel context.CancelFunc ){
18
-
19
- // CancelFunc to cancel to context
20
- defer cancel ()
21
-
22
- // client provides a method to close
23
- // a mongoDB connection.
24
- defer func (){
25
-
26
- // client.Disconnect method also has deadline.
27
- // returns error if any,
28
- if err := client .Disconnect (ctx ); err != nil {
29
- panic (err )
30
- }
31
- }()
33
+ cancel context.CancelFunc ) {
34
+
35
+ // CancelFunc to cancel to context
36
+ defer cancel ()
37
+
38
+ // client provides a method to close
39
+ // a mongoDB connection.
40
+ defer func () {
41
+
42
+ // client.Disconnect method also has deadline.
43
+ // returns error if any,
44
+ if err := client .Disconnect (ctx ); err != nil {
45
+ panic (err )
46
+ }
47
+ }()
32
48
}
33
49
34
- // This is a user defined method that returns mongo.Client,
50
+ // This is a user defined method that returns mongo.Client,
35
51
// context.Context, context.CancelFunc and error.
36
52
// mongo.Client will be used for further database operation.
37
53
// context.Context will be used set deadlines for process.
38
- // context.CancelFunc will be used to cancel context and
54
+ // context.CancelFunc will be used to cancel context and
39
55
// resource associated with it.
40
56
41
- func Connect (uri string )(* mongo.Client , context.Context ,
42
- context.CancelFunc , error ) {
43
-
44
- // ctx will be used to set deadline for process, here
45
- // deadline will of 30 seconds.
46
- ctx , cancel := context .WithTimeout (context .Background (),
47
- 30 * time .Second )
48
-
49
- // mongo.Connect return mongo.Client method
50
- client , err := mongo .Connect (ctx , options .Client ().ApplyURI (uri ))
51
- return client , ctx , cancel , err
57
+ func Connect (uri string ) (* mongo.Client , context.Context ,
58
+ context.CancelFunc , error ) {
59
+
60
+ // ctx will be used to set deadline for process, here
61
+ // deadline will of 30 seconds.
62
+ ctx , cancel := context .WithTimeout (context .Background (),
63
+ 30 * time .Second )
64
+
65
+ // mongo.Connect return mongo.Client method
66
+ client , err := mongo .Connect (ctx , options .Client ().ApplyURI (uri ))
67
+ return client , ctx , cancel , err
52
68
}
53
69
54
- // This is a user defined method that accepts
70
+ // This is a user defined method that accepts
55
71
// mongo.Client and context.Context
56
72
// This method used to ping the mongoDB, return error if any.
57
- func Ping (client * mongo.Client , ctx context.Context ) error {
58
-
59
- // mongo.Client has Ping to ping mongoDB, deadline of
60
- // the Ping method will be determined by cxt
61
- // Ping method return error if any occurred, then
62
- // the error can be handled.
63
- if err := client .Ping (ctx , readpref .Primary ()); err != nil {
64
- return err
65
- }
66
- fmt .Println ("connected successfully" )
67
- return nil
73
+ func Ping (client * mongo.Client , ctx context.Context ) error {
74
+
75
+ // mongo.Client has Ping to ping mongoDB, deadline of
76
+ // the Ping method will be determined by cxt
77
+ // Ping method return error if any occurred, then
78
+ // the error can be handled.
79
+ if err := client .Ping (ctx , readpref .Primary ()); err != nil {
80
+ return err
81
+ }
82
+ fmt .Println ("connected successfully" )
83
+ return nil
68
84
}
69
-
85
+
70
86
// insertOne is a user defined method, used to insert
71
87
// documents into collection returns result of InsertOne
72
88
// and error if any.
73
89
func Insertdata (client * mongo.Client , ctx context.Context , dataBase , col string , doc interface {}) (* mongo.InsertOneResult , error ) {
74
-
75
- // select database and collection ith Client.Database method
76
- // and Database.Collection method
77
- collection := client .Database (dataBase ).Collection (col )
78
-
79
- // InsertOne accept two argument of type Context
80
- // and of empty interface
81
- result , err := collection .InsertOne (ctx , doc )
82
- return result , err
83
- }
84
90
85
- type marks struct {
86
- rollNo int `json:"rollno"`
87
- maths int `json:"maths"`
88
- science int `json:"science"`
89
- computer int `json:"computer"`
91
+ // select database and collection ith Client.Database method
92
+ // and Database.Collection method
93
+ collection := client .Database (dataBase ).Collection (col )
94
+
95
+ // InsertOne accept two argument of type Context
96
+ // and of empty interface
97
+ result , err := collection .InsertOne (ctx , doc )
98
+ return result , err
90
99
}
91
100
92
101
// for testing added
93
102
94
103
func PostMarks (c * gin.Context ) {
95
- var newMarks marks
96
-
97
- // Call BindJSON to bind the received JSON to
98
- // newAlbum.
99
- if err := c .BindJSON (& newMarks ); err != nil {
100
- return
101
- }
102
-
103
- // Add the new album to the slice.
104
- albums = append (albums , newMarks )
105
- c .IndentedJSON (http .StatusCreated , newMarks )
106
- }
104
+
105
+ // Get the request body
106
+
107
+ // Decode the request body into a JSON object
108
+ var jsonBody map [string ]interface {}
109
+ err := c .BindJSON (& jsonBody )
110
+ if err != nil {
111
+ // Handle error
112
+ return
113
+ }
114
+
115
+ // Use the JSON object
116
+ fmt .Println (jsonBody )
117
+ c .IndentedJSON (http .StatusCreated , jsonBody )
118
+ }
0 commit comments