Skip to content

Commit eabc247

Browse files
fix json body
1 parent da48919 commit eabc247

File tree

4 files changed

+124
-74
lines changed

4 files changed

+124
-74
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${fileDirname}"
13+
}
14+
]
15+
}

.vscode/settings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"go.delveConfig": {
3+
"GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
4+
"GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
5+
},
6+
"go.gopath": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
7+
"gopls": {
8+
"build.env": {
9+
"GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
10+
"GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
11+
},
12+
"dev.env": {
13+
"GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
14+
"GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
15+
}
16+
},
17+
"go.terminal.activateEnvironment": false,
18+
"go.toolsEnvVars": {
19+
"GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
20+
"GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
21+
},
22+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func main() {
9292
router := gin.Default()
9393

9494
router.GET("/", helloCall)
95+
router.POST("/add", mongoconnect.PostMarks)
9596
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
9697

9798

mongoconnect/config.go

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,118 @@
11
package mongoconnect
22

33
import (
4-
"fmt"
54
"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"
1114
)
1215

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+
}
1329

1430
// This is a user defined method to close resources.
1531
// This method closes mongoDB connection and cancel context.
1632
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+
}()
3248
}
3349

34-
// This is a user defined method that returns mongo.Client,
50+
// This is a user defined method that returns mongo.Client,
3551
// context.Context, context.CancelFunc and error.
3652
// mongo.Client will be used for further database operation.
3753
// 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
3955
// resource associated with it.
4056

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
5268
}
5369

54-
// This is a user defined method that accepts
70+
// This is a user defined method that accepts
5571
// mongo.Client and context.Context
5672
// 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
6884
}
69-
85+
7086
// insertOne is a user defined method, used to insert
7187
// documents into collection returns result of InsertOne
7288
// and error if any.
7389
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-
}
8490

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
9099
}
91100

92101
// for testing added
93102

94103
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy