From 4295edb2163c49824f5b0f8029a08d69571386d1 Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Fri, 19 Jan 2024 11:10:22 +0530
Subject: [PATCH 01/12] go mongo pkg

---
 main.go                |  9 +++---
 mongoconnect/config.go | 67 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100644 mongoconnect/config.go

diff --git a/main.go b/main.go
index c9f7f90..1fd1e5e 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
 	"context"
     "time"
  
+    "github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
     "go.mongodb.org/mongo-driver/mongo"
     "go.mongodb.org/mongo-driver/mongo/options"
     "go.mongodb.org/mongo-driver/mongo/readpref"
@@ -17,6 +18,7 @@ import (
 	swaggerFiles "github.com/swaggo/files"
 	ginSwagger "github.com/swaggo/gin-swagger"
 	_ "github.com/vinodnextcoder/golang-mongo-server/docs"
+  
 )
 
 // @title           Swagger Example API
@@ -42,18 +44,17 @@ func main() {
 
   // Get Client, Context, CancelFunc and 
     // err from connect method.
-    client, ctx, cancel, err := connect("mongodb://localhost:27017/test")
+    client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
     if err != nil {
         panic(err)
     }
      
     // Release resource when the main
     // function is returned.
-    defer close(client, ctx, cancel)
+    defer mongoconnect.Close(client, ctx, cancel)
 	
-     
     // Ping mongoDB with Ping method
-    ping(client, ctx)
+    mongoconnect.Ping(client, ctx)
 
   port := os.Getenv("PORT")
 
diff --git a/mongoconnect/config.go b/mongoconnect/config.go
new file mode 100644
index 0000000..859b234
--- /dev/null
+++ b/mongoconnect/config.go
@@ -0,0 +1,67 @@
+package mongoconnect
+
+import (
+	"fmt"
+	"context"
+    "time"
+    "go.mongodb.org/mongo-driver/mongo"
+    "go.mongodb.org/mongo-driver/mongo/options"
+    "go.mongodb.org/mongo-driver/mongo/readpref"
+)
+
+
+// This is a user defined method to close resources.
+// This method closes mongoDB connection and cancel context.
+func Close(client *mongo.Client, ctx context.Context,
+	cancel context.CancelFunc){
+	 
+// CancelFunc to cancel to context
+defer cancel()
+
+// client provides a method to close 
+// a mongoDB connection.
+defer func(){
+
+ // client.Disconnect method also has deadline.
+ // returns error if any,
+ if err := client.Disconnect(ctx); err != nil{
+	 panic(err)
+ }
+}()
+}
+
+// This is a user defined method that returns mongo.Client, 
+// context.Context, context.CancelFunc and error.
+// mongo.Client will be used for further database operation.
+// context.Context will be used set deadlines for process.
+// context.CancelFunc will be used to cancel context and 
+// resource associated with it.
+
+func Connect(uri string)(*mongo.Client, context.Context, 
+				   context.CancelFunc, error) {
+					
+// ctx will be used to set deadline for process, here 
+// deadline will of 30 seconds.
+ctx, cancel := context.WithTimeout(context.Background(), 
+								30 * time.Second)
+
+// mongo.Connect return mongo.Client method
+client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
+return client, ctx, cancel, err
+}
+
+// This is a user defined method that accepts 
+// mongo.Client and context.Context
+// This method used to ping the mongoDB, return error if any.
+func Ping(client *mongo.Client, ctx context.Context) error{
+
+// mongo.Client has Ping to ping mongoDB, deadline of 
+// the Ping method will be determined by cxt
+// Ping method return error if any occurred, then
+// the error can be handled.
+if err := client.Ping(ctx, readpref.Primary()); err != nil {
+ return err
+}
+fmt.Println("connected successfully")
+return nil
+}

From b6552a7387e54bb3553e8b9027560087b6a5cea9 Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Sat, 20 Jan 2024 20:00:47 +0530
Subject: [PATCH 02/12] db golang added

---
 .env.dev               |  3 +-
 main.go                | 98 ++++++++++++++++--------------------------
 mongoconnect/config.go | 16 +++++++
 3 files changed, 55 insertions(+), 62 deletions(-)

diff --git a/.env.dev b/.env.dev
index 9726515..532716d 100644
--- a/.env.dev
+++ b/.env.dev
@@ -1,3 +1,4 @@
 S3_BUCKET=YOURS3BUCKET
 SECRET_KEY=YOURSECRETKEYGOESHERE
-PORT=3001
\ No newline at end of file
+PORT=3001
+DBNAME=test
\ No newline at end of file
diff --git a/main.go b/main.go
index 1fd1e5e..2b6be31 100644
--- a/main.go
+++ b/main.go
@@ -5,13 +5,13 @@ import (
 	"log"
 	"net/http"
 	"os"
-	"context"
-    "time"
- 
+	// "context"
+    // "time"
+    "go.mongodb.org/mongo-driver/bson"
     "github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
-    "go.mongodb.org/mongo-driver/mongo"
-    "go.mongodb.org/mongo-driver/mongo/options"
-    "go.mongodb.org/mongo-driver/mongo/readpref"
+    // "go.mongodb.org/mongo-driver/mongo"
+    // "go.mongodb.org/mongo-driver/mongo/options"
+    // "go.mongodb.org/mongo-driver/mongo/readpref"
 
     "github.com/joho/godotenv"
 	"github.com/gin-gonic/gin"
@@ -56,6 +56,37 @@ func main() {
     // Ping mongoDB with Ping method
     mongoconnect.Ping(client, ctx)
 
+    
+    // Create  a object of type interface to  store
+    // the bson values, that  we are inserting into database.
+    var document interface{}
+     
+     
+    document = bson.D{
+        {"rollNo", 175},
+        {"maths", 80},
+        {"science", 90},
+        {"computer", 95},
+    }
+     
+    dbname := os.Getenv("DBNAME")
+    // insertOne accepts client , context, database
+    // name collection name and an interface that 
+    // will be inserted into the  collection.
+    // insertOne returns an error and a result of 
+    // insert in a single document into the collection.
+    insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
+     
+    // handle the error
+    if err != nil {
+        panic(err)
+    }
+     
+    // print the insertion id of the document, 
+    // if it is inserted.
+    fmt.Println("Result of InsertOne")
+    fmt.Println(insertOneResult.InsertedID)
+
   port := os.Getenv("PORT")
 
 	router := gin.Default()
@@ -81,58 +112,3 @@ func main() {
 func helloCall(c *gin.Context) {
 	c.JSON(http.StatusOK, gin.H{"message": "Hello, You created a Web App!"})
 }
-// This is a user defined method to close resources.
-// This method closes mongoDB connection and cancel context.
-func close(client *mongo.Client, ctx context.Context,
-	cancel context.CancelFunc){
-	 
-// CancelFunc to cancel to context
-defer cancel()
-
-// client provides a method to close 
-// a mongoDB connection.
-defer func(){
-
- // client.Disconnect method also has deadline.
- // returns error if any,
- if err := client.Disconnect(ctx); err != nil{
-	 panic(err)
- }
-}()
-}
-
-// This is a user defined method that returns mongo.Client, 
-// context.Context, context.CancelFunc and error.
-// mongo.Client will be used for further database operation.
-// context.Context will be used set deadlines for process.
-// context.CancelFunc will be used to cancel context and 
-// resource associated with it.
-
-func connect(uri string)(*mongo.Client, context.Context, 
-				   context.CancelFunc, error) {
-					
-// ctx will be used to set deadline for process, here 
-// deadline will of 30 seconds.
-ctx, cancel := context.WithTimeout(context.Background(), 
-								30 * time.Second)
-
-// mongo.Connect return mongo.Client method
-client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
-return client, ctx, cancel, err
-}
-
-// This is a user defined method that accepts 
-// mongo.Client and context.Context
-// This method used to ping the mongoDB, return error if any.
-func ping(client *mongo.Client, ctx context.Context) error{
-
-// mongo.Client has Ping to ping mongoDB, deadline of 
-// the Ping method will be determined by cxt
-// Ping method return error if any occurred, then
-// the error can be handled.
-if err := client.Ping(ctx, readpref.Primary()); err != nil {
- return err
-}
-fmt.Println("connected successfully")
-return nil
-}
diff --git a/mongoconnect/config.go b/mongoconnect/config.go
index 859b234..a7163eb 100644
--- a/mongoconnect/config.go
+++ b/mongoconnect/config.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"context"
     "time"
+    // "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"
@@ -65,3 +66,18 @@ if err := client.Ping(ctx, readpref.Primary()); err != nil {
 fmt.Println("connected successfully")
 return nil
 }
+ 
+// insertOne is a user defined method, used to insert
+// documents into collection returns result of InsertOne
+// and error if any.
+func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
+ 
+    // select database and collection ith Client.Database method 
+    // and Database.Collection method
+    collection := client.Database(dataBase).Collection(col)
+     
+    // InsertOne accept two argument of type Context 
+    // and of empty interface   
+    result, err := collection.InsertOne(ctx, doc)
+    return result, err
+}
\ No newline at end of file

From da4891943d0ab6d516c8c28022b17220df10b153 Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Mon, 22 Jan 2024 12:30:55 +0530
Subject: [PATCH 03/12] config

---
 mongoconnect/config.go | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/mongoconnect/config.go b/mongoconnect/config.go
index a7163eb..3662a57 100644
--- a/mongoconnect/config.go
+++ b/mongoconnect/config.go
@@ -80,4 +80,27 @@ func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string,
     // and of empty interface   
     result, err := collection.InsertOne(ctx, doc)
     return result, err
+}
+
+type marks struct {
+    rollNo     int  `json:"rollno"`
+    maths  int  `json:"maths"`
+    science int  `json:"science"`
+    computer  int `json:"computer"`
+}
+
+//  for testing added
+
+func PostMarks(c *gin.Context) {
+    var newMarks marks
+
+    // Call BindJSON to bind the received JSON to
+    // newAlbum.
+    if err := c.BindJSON(&newMarks); err != nil {
+        return
+    }
+
+    // Add the new album to the slice.
+    albums = append(albums, newMarks)
+    c.IndentedJSON(http.StatusCreated, newMarks)
 }
\ No newline at end of file

From eabc247e13cc6e0bd7cfba5dcb559bebe2a5ae0c Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Mon, 22 Jan 2024 23:04:42 +0530
Subject: [PATCH 04/12] fix json body

---
 .vscode/launch.json    |  15 ++++
 .vscode/settings.json  |  22 ++++++
 main.go                |   1 +
 mongoconnect/config.go | 160 ++++++++++++++++++++++-------------------
 4 files changed, 124 insertions(+), 74 deletions(-)
 create mode 100644 .vscode/launch.json
 create mode 100644 .vscode/settings.json

diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..608d3c6
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,15 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Launch Package",
+            "type": "go",
+            "request": "launch",
+            "mode": "auto",
+            "program": "${fileDirname}"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..d2b0065
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,22 @@
+{
+    "go.delveConfig": {
+        "GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
+        "GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
+    },
+    "go.gopath": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
+    "gopls": {
+        "build.env": {
+            "GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
+            "GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
+        },
+        "dev.env": {
+            "GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
+            "GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
+        }
+    },
+    "go.terminal.activateEnvironment": false,
+    "go.toolsEnvVars": {
+        "GOPATH": "C:\\Users\\vinod_khetade\\Documents\\Go-work",
+        "GOMODCACHE": "C:\\Users\\vinod_khetade\\Documents\\Go-work\\pkg\\mod"
+    },
+}
\ No newline at end of file
diff --git a/main.go b/main.go
index 2b6be31..07e1ce3 100644
--- a/main.go
+++ b/main.go
@@ -92,6 +92,7 @@ func main() {
 	router := gin.Default()
 
 	router.GET("/", helloCall)
+  router.POST("/add", mongoconnect.PostMarks)
 	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 
 
diff --git a/mongoconnect/config.go b/mongoconnect/config.go
index 3662a57..4c3d0ba 100644
--- a/mongoconnect/config.go
+++ b/mongoconnect/config.go
@@ -1,106 +1,118 @@
 package mongoconnect
 
 import (
-	"fmt"
 	"context"
-    "time"
-    // "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"
+	"fmt"
+	"net/http"
+	"time"
+
+	// "go.mongodb.org/mongo-driver/bson"
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/mongo"
+	"go.mongodb.org/mongo-driver/mongo/options"
+	"go.mongodb.org/mongo-driver/mongo/readpref"
 )
 
+type marksData struct {
+	rollNo   int
+	maths    int
+	science  int
+	computer int
+}
+
+// albums slice to seed record album data.
+var usermarks = []marksData{
+	{rollNo: 2, maths: 2, science: 2, computer: 56},
+	{rollNo: 32, maths: 2, science: 2, computer: 17},
+	{rollNo: 3, maths: 1, science: 2, computer: 39},
+}
 
 // This is a user defined method to close resources.
 // This method closes mongoDB connection and cancel context.
 func Close(client *mongo.Client, ctx context.Context,
-	cancel context.CancelFunc){
-	 
-// CancelFunc to cancel to context
-defer cancel()
-
-// client provides a method to close 
-// a mongoDB connection.
-defer func(){
-
- // client.Disconnect method also has deadline.
- // returns error if any,
- if err := client.Disconnect(ctx); err != nil{
-	 panic(err)
- }
-}()
+	cancel context.CancelFunc) {
+
+	// CancelFunc to cancel to context
+	defer cancel()
+
+	// client provides a method to close
+	// a mongoDB connection.
+	defer func() {
+
+		// client.Disconnect method also has deadline.
+		// returns error if any,
+		if err := client.Disconnect(ctx); err != nil {
+			panic(err)
+		}
+	}()
 }
 
-// This is a user defined method that returns mongo.Client, 
+// This is a user defined method that returns mongo.Client,
 // context.Context, context.CancelFunc and error.
 // mongo.Client will be used for further database operation.
 // context.Context will be used set deadlines for process.
-// context.CancelFunc will be used to cancel context and 
+// context.CancelFunc will be used to cancel context and
 // resource associated with it.
 
-func Connect(uri string)(*mongo.Client, context.Context, 
-				   context.CancelFunc, error) {
-					
-// ctx will be used to set deadline for process, here 
-// deadline will of 30 seconds.
-ctx, cancel := context.WithTimeout(context.Background(), 
-								30 * time.Second)
-
-// mongo.Connect return mongo.Client method
-client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
-return client, ctx, cancel, err
+func Connect(uri string) (*mongo.Client, context.Context,
+	context.CancelFunc, error) {
+
+	// ctx will be used to set deadline for process, here
+	// deadline will of 30 seconds.
+	ctx, cancel := context.WithTimeout(context.Background(),
+		30*time.Second)
+
+	// mongo.Connect return mongo.Client method
+	client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
+	return client, ctx, cancel, err
 }
 
-// This is a user defined method that accepts 
+// This is a user defined method that accepts
 // mongo.Client and context.Context
 // This method used to ping the mongoDB, return error if any.
-func Ping(client *mongo.Client, ctx context.Context) error{
-
-// mongo.Client has Ping to ping mongoDB, deadline of 
-// the Ping method will be determined by cxt
-// Ping method return error if any occurred, then
-// the error can be handled.
-if err := client.Ping(ctx, readpref.Primary()); err != nil {
- return err
-}
-fmt.Println("connected successfully")
-return nil
+func Ping(client *mongo.Client, ctx context.Context) error {
+
+	// mongo.Client has Ping to ping mongoDB, deadline of
+	// the Ping method will be determined by cxt
+	// Ping method return error if any occurred, then
+	// the error can be handled.
+	if err := client.Ping(ctx, readpref.Primary()); err != nil {
+		return err
+	}
+	fmt.Println("connected successfully")
+	return nil
 }
- 
+
 // insertOne is a user defined method, used to insert
 // documents into collection returns result of InsertOne
 // and error if any.
 func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
- 
-    // select database and collection ith Client.Database method 
-    // and Database.Collection method
-    collection := client.Database(dataBase).Collection(col)
-     
-    // InsertOne accept two argument of type Context 
-    // and of empty interface   
-    result, err := collection.InsertOne(ctx, doc)
-    return result, err
-}
 
-type marks struct {
-    rollNo     int  `json:"rollno"`
-    maths  int  `json:"maths"`
-    science int  `json:"science"`
-    computer  int `json:"computer"`
+	// select database and collection ith Client.Database method
+	// and Database.Collection method
+	collection := client.Database(dataBase).Collection(col)
+
+	// InsertOne accept two argument of type Context
+	// and of empty interface
+	result, err := collection.InsertOne(ctx, doc)
+	return result, err
 }
 
 //  for testing added
 
 func PostMarks(c *gin.Context) {
-    var newMarks marks
-
-    // Call BindJSON to bind the received JSON to
-    // newAlbum.
-    if err := c.BindJSON(&newMarks); err != nil {
-        return
-    }
-
-    // Add the new album to the slice.
-    albums = append(albums, newMarks)
-    c.IndentedJSON(http.StatusCreated, newMarks)
-}
\ No newline at end of file
+
+	// Get the request body
+
+	// Decode the request body into a JSON object
+	var jsonBody map[string]interface{}
+	err := c.BindJSON(&jsonBody)
+	if err != nil {
+		// Handle error
+		return
+	}
+
+	// Use the JSON object
+	fmt.Println(jsonBody)
+	c.IndentedJSON(http.StatusCreated, jsonBody)
+}

From 6d2e80dffa8cd90804af9ecf68e182bda09c791e Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Tue, 23 Jan 2024 00:03:54 +0530
Subject: [PATCH 05/12] mongodb connect

---
 main.go                | 125 ++++++++++++++++++++---------------------
 mongoconnect/config.go |  42 ++++++++++++++
 2 files changed, 102 insertions(+), 65 deletions(-)

diff --git a/main.go b/main.go
index 07e1ce3..0d1d07c 100644
--- a/main.go
+++ b/main.go
@@ -5,20 +5,20 @@ import (
 	"log"
 	"net/http"
 	"os"
+
 	// "context"
-    // "time"
-    "go.mongodb.org/mongo-driver/bson"
-    "github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
-    // "go.mongodb.org/mongo-driver/mongo"
-    // "go.mongodb.org/mongo-driver/mongo/options"
-    // "go.mongodb.org/mongo-driver/mongo/readpref"
-
-    "github.com/joho/godotenv"
+	// "time"
+	"github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
+
+	// "go.mongodb.org/mongo-driver/mongo"
+	// "go.mongodb.org/mongo-driver/mongo/options"
+	// "go.mongodb.org/mongo-driver/mongo/readpref"
+
 	"github.com/gin-gonic/gin"
+	"github.com/joho/godotenv"
 	swaggerFiles "github.com/swaggo/files"
 	ginSwagger "github.com/swaggo/gin-swagger"
 	_ "github.com/vinodnextcoder/golang-mongo-server/docs"
-  
 )
 
 // @title           Swagger Example API
@@ -37,70 +37,65 @@ import (
 func main() {
 
 	err := godotenv.Load(".env.dev")
-	
-  if err != nil {
-    log.Fatal("Error loading .env file")
-  }
-
-  // Get Client, Context, CancelFunc and 
-    // err from connect method.
-    client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
-    if err != nil {
-        panic(err)
-    }
-     
-    // Release resource when the main
-    // function is returned.
-    defer mongoconnect.Close(client, ctx, cancel)
-	
-    // Ping mongoDB with Ping method
-    mongoconnect.Ping(client, ctx)
-
-    
-    // Create  a object of type interface to  store
-    // the bson values, that  we are inserting into database.
-    var document interface{}
-     
-     
-    document = bson.D{
-        {"rollNo", 175},
-        {"maths", 80},
-        {"science", 90},
-        {"computer", 95},
-    }
-     
-    dbname := os.Getenv("DBNAME")
-    // insertOne accepts client , context, database
-    // name collection name and an interface that 
-    // will be inserted into the  collection.
-    // insertOne returns an error and a result of 
-    // insert in a single document into the collection.
-    insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
-     
-    // handle the error
-    if err != nil {
-        panic(err)
-    }
-     
-    // print the insertion id of the document, 
-    // if it is inserted.
-    fmt.Println("Result of InsertOne")
-    fmt.Println(insertOneResult.InsertedID)
-
-  port := os.Getenv("PORT")
+
+	if err != nil {
+		log.Fatal("Error loading .env file")
+	}
+
+	// Get Client, Context, CancelFunc and
+	// err from connect method.
+	client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
+	if err != nil {
+		panic(err)
+	}
+
+	// Release resource when the main
+	// function is returned.
+	// defer mongoconnect.Close(client, ctx, cancel)
+
+	// // Ping mongoDB with Ping method
+	// // mongoconnect.Ping(client, ctx)
+
+	// // Create  a object of type interface to  store
+	// // the bson values, that  we are inserting into database.
+	// var document interface{}
+
+	// document = bson.D{
+	// 	{"rollNo", 175},
+	// 	{"maths", 80},
+	// 	{"science", 90},
+	// 	{"computer", 95},
+	// }
+
+	// dbname := os.Getenv("DBNAME")
+	// // insertOne accepts client , context, database
+	// // name collection name and an interface that
+	// // will be inserted into the  collection.
+	// // insertOne returns an error and a result of
+	// // insert in a single document into the collection.
+	// insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
+
+	// handle the error
+	if err != nil {
+		panic(err)
+	}
+
+	// print the insertion id of the document,
+	// if it is inserted.
+	fmt.Println("Result of InsertOne", client, ctx, cancel)
+	// fmt.Println(insertOneResult.InsertedID)
+
+	port := os.Getenv("PORT")
 
 	router := gin.Default()
 
 	router.GET("/", helloCall)
-  router.POST("/add", mongoconnect.PostMarks)
+	router.POST("/add", mongoconnect.PostMarks)
 	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 
-
-	router.Run("0.0.0.0:"+port)
+	router.Run("0.0.0.0:" + port)
 }
 
-
-
 // helloCall godoc
 // @Summary hellow example
 // @Schemes
diff --git a/mongoconnect/config.go b/mongoconnect/config.go
index 4c3d0ba..7ff456f 100644
--- a/mongoconnect/config.go
+++ b/mongoconnect/config.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"net/http"
+	"os"
 	"time"
 
 	// "go.mongodb.org/mongo-driver/bson"
@@ -98,6 +99,25 @@ func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string,
 	return result, err
 }
 
+func Insertdata1(dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
+
+	// connect mongodb and insert records
+	client, ctx, cancel, err := Connect("mongodb://localhost:27017/test")
+	if err != nil {
+		panic(err)
+	}
+
+	// select database and collection ith Client.Database method
+	// and Database.Collection method
+	fmt.Println("Result of InsertOne", cancel)
+	collection := client.Database(dataBase).Collection(col)
+
+	// InsertOne accept two argument of type Context
+	// and of empty interface
+	result, err := collection.InsertOne(ctx, doc)
+	return result, err
+}
+
 //  for testing added
 
 func PostMarks(c *gin.Context) {
@@ -111,7 +131,29 @@ func PostMarks(c *gin.Context) {
 		// Handle error
 		return
 	}
+	Connect("mongodb://localhost:27017/test")
+	dbname := os.Getenv("DBNAME")
+	// var client mongo.Client
+	// var ctx context.Context
+	// insertOne accepts client , context, database
+	// name collection name and an interface that
+	// will be inserted into the  collection.
+	// insertOne returns an error and a result of
+	// insert in a single document into the collection.
+
+	insertOneResult, err := Insertdata1(dbname, "marks", jsonBody)
+
+	fmt.Println(err)
+
+	// handle the error
+	if err != nil {
+		panic(err)
+	}
 
+	// print the insertion id of the document,
+	// if it is inserted.
+	fmt.Println("Result of InsertOne")
+	fmt.Println(insertOneResult.InsertedID)
 	// Use the JSON object
 	fmt.Println(jsonBody)
 	c.IndentedJSON(http.StatusCreated, jsonBody)

From 1a14d48fe89b3fdff9a2ee345371b3c6f3f6a72d Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Tue, 23 Jan 2024 13:52:57 +0530
Subject: [PATCH 06/12] go mongodb connect

---
 .env.dev                   |   3 +-
 configs/mongoconnection.go |  44 ++++++++++
 configs/mongoenv.go        |  16 ++++
 controllers/users.go       |  51 ++++++++++++
 main.go                    |  58 ++------------
 models/user_model.go       |  10 +++
 mongoconnect/config.go     | 160 -------------------------------------
 responses/user_response.go |   7 ++
 routes/routes.go           |  10 +++
 9 files changed, 145 insertions(+), 214 deletions(-)
 create mode 100644 configs/mongoconnection.go
 create mode 100644 configs/mongoenv.go
 create mode 100644 controllers/users.go
 create mode 100644 models/user_model.go
 delete mode 100644 mongoconnect/config.go
 create mode 100644 responses/user_response.go
 create mode 100644 routes/routes.go

diff --git a/.env.dev b/.env.dev
index 532716d..75cd6c1 100644
--- a/.env.dev
+++ b/.env.dev
@@ -1,4 +1,5 @@
 S3_BUCKET=YOURS3BUCKET
 SECRET_KEY=YOURSECRETKEYGOESHERE
 PORT=3001
-DBNAME=test
\ No newline at end of file
+DBNAME=test
+MONGOURL=mongodb://localhost:27017/test
\ No newline at end of file
diff --git a/configs/mongoconnection.go b/configs/mongoconnection.go
new file mode 100644
index 0000000..61ee9ea
--- /dev/null
+++ b/configs/mongoconnection.go
@@ -0,0 +1,44 @@
+package configs
+
+import (
+	"context"
+	"fmt"
+	"log"
+	"time"
+
+	"go.mongodb.org/mongo-driver/mongo"
+	"go.mongodb.org/mongo-driver/mongo/options"
+)
+
+func ConnectDB() *mongo.Client {
+	client, err := mongo.NewClient(options.Client().ApplyURI(EnvMongoURI()))
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
+	err = client.Connect(ctx)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	//ping the database
+	err = client.Ping(ctx, nil)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	fmt.Println("Connected to MongoDB 1")
+
+	return client
+}
+
+// Client instance
+var DB *mongo.Client = ConnectDB()
+
+// getting database collections
+func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
+	collection := client.Database("golangAPI").Collection(collectionName)
+
+	return collection
+}
diff --git a/configs/mongoenv.go b/configs/mongoenv.go
new file mode 100644
index 0000000..0371cdf
--- /dev/null
+++ b/configs/mongoenv.go
@@ -0,0 +1,16 @@
+package configs
+
+import (
+	"log"
+	"os"
+
+	"github.com/joho/godotenv"
+)
+
+func EnvMongoURI() string {
+	err := godotenv.Load(".env.dev")
+	if err != nil {
+		log.Fatal("Error loading .env file")
+	}
+	return os.Getenv("MONGOURL")
+}
diff --git a/controllers/users.go b/controllers/users.go
new file mode 100644
index 0000000..347e6f5
--- /dev/null
+++ b/controllers/users.go
@@ -0,0 +1,51 @@
+package markscontrollers
+
+import (
+	"context"
+	"net/http"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"github.com/vinodnextcoder/golang-mongo-server/configs"
+	"github.com/vinodnextcoder/golang-mongo-server/models"
+	"github.com/vinodnextcoder/golang-mongo-server/responses"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+	"go.mongodb.org/mongo-driver/mongo"
+)
+
+var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
+
+func CreateUser() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+		var user models.User
+		defer cancel()
+
+		//validate the request body
+		if err := c.BindJSON(&user); err != nil {
+			c.JSON(http.StatusBadRequest, responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		//use the validator library to validate required fields
+		// if validationErr := validate.Struct(&user); validationErr != nil {
+		// 	c.JSON(http.StatusBadRequest, responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"data": validationErr.Error()}})
+		// 	return
+		// }
+
+		newUser := models.User{
+			Id:       primitive.NewObjectID(),
+			Name:     user.Name,
+			Location: user.Location,
+			Title:    user.Title,
+		}
+
+		result, err := userCollection.InsertOne(ctx, newUser)
+		if err != nil {
+			c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		c.JSON(http.StatusCreated, responses.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"data": result}})
+	}
+}
diff --git a/main.go b/main.go
index 0d1d07c..09e7cc1 100644
--- a/main.go
+++ b/main.go
@@ -1,24 +1,16 @@
 package main
 
 import (
-	"fmt"
 	"log"
 	"net/http"
 	"os"
 
-	// "context"
-	// "time"
-	"github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
-
-	// "go.mongodb.org/mongo-driver/mongo"
-	// "go.mongodb.org/mongo-driver/mongo/options"
-	// "go.mongodb.org/mongo-driver/mongo/readpref"
-
 	"github.com/gin-gonic/gin"
 	"github.com/joho/godotenv"
 	swaggerFiles "github.com/swaggo/files"
 	ginSwagger "github.com/swaggo/gin-swagger"
 	_ "github.com/vinodnextcoder/golang-mongo-server/docs"
+	"github.com/vinodnextcoder/golang-mongo-server/routes"
 )
 
 // @title           Swagger Example API
@@ -41,56 +33,16 @@ func main() {
 	if err != nil {
 		log.Fatal("Error loading .env file")
 	}
-
-	// Get Client, Context, CancelFunc and
-	// err from connect method.
-	client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
-	if err != nil {
-		panic(err)
-	}
-
-	// Release resource when the main
-	// function is returned.
-	// defer mongoconnect.Close(client, ctx, cancel)
-
-	// // Ping mongoDB with Ping method
-	// // mongoconnect.Ping(client, ctx)
-
-	// // Create  a object of type interface to  store
-	// // the bson values, that  we are inserting into database.
-	// var document interface{}
-
-	// document = bson.D{
-	// 	{"rollNo", 175},
-	// 	{"maths", 80},
-	// 	{"science", 90},
-	// 	{"computer", 95},
-	// }
-
-	// dbname := os.Getenv("DBNAME")
-	// // insertOne accepts client , context, database
-	// // name collection name and an interface that
-	// // will be inserted into the  collection.
-	// // insertOne returns an error and a result of
-	// // insert in a single document into the collection.
-	// insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
-
-	// handle the error
-	if err != nil {
-		panic(err)
-	}
-
-	// print the insertion id of the document,
-	// if it is inserted.
-	fmt.Println("Result of InsertOne", client, ctx, cancel)
-	// fmt.Println(insertOneResult.InsertedID)
+	//run database
+	// configs.ConnectDB()
 
 	port := os.Getenv("PORT")
 
 	router := gin.Default()
 
 	router.GET("/", helloCall)
-	router.POST("/add", mongoconnect.PostMarks)
+	routes.UserRoute(router)
+
 	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 
 	router.Run("0.0.0.0:" + port)
diff --git a/models/user_model.go b/models/user_model.go
new file mode 100644
index 0000000..8288b36
--- /dev/null
+++ b/models/user_model.go
@@ -0,0 +1,10 @@
+package models
+
+import "go.mongodb.org/mongo-driver/bson/primitive"
+
+type User struct {
+	Id       primitive.ObjectID `json:"id,omitempty"`
+	Name     string             `json:"name,omitempty" validate:"required"`
+	Location string             `json:"location,omitempty" validate:"required"`
+	Title    string             `json:"title,omitempty" validate:"required"`
+}
diff --git a/mongoconnect/config.go b/mongoconnect/config.go
deleted file mode 100644
index 7ff456f..0000000
--- a/mongoconnect/config.go
+++ /dev/null
@@ -1,160 +0,0 @@
-package mongoconnect
-
-import (
-	"context"
-	"fmt"
-	"net/http"
-	"os"
-	"time"
-
-	// "go.mongodb.org/mongo-driver/bson"
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/mongo"
-	"go.mongodb.org/mongo-driver/mongo/options"
-	"go.mongodb.org/mongo-driver/mongo/readpref"
-)
-
-type marksData struct {
-	rollNo   int
-	maths    int
-	science  int
-	computer int
-}
-
-// albums slice to seed record album data.
-var usermarks = []marksData{
-	{rollNo: 2, maths: 2, science: 2, computer: 56},
-	{rollNo: 32, maths: 2, science: 2, computer: 17},
-	{rollNo: 3, maths: 1, science: 2, computer: 39},
-}
-
-// This is a user defined method to close resources.
-// This method closes mongoDB connection and cancel context.
-func Close(client *mongo.Client, ctx context.Context,
-	cancel context.CancelFunc) {
-
-	// CancelFunc to cancel to context
-	defer cancel()
-
-	// client provides a method to close
-	// a mongoDB connection.
-	defer func() {
-
-		// client.Disconnect method also has deadline.
-		// returns error if any,
-		if err := client.Disconnect(ctx); err != nil {
-			panic(err)
-		}
-	}()
-}
-
-// This is a user defined method that returns mongo.Client,
-// context.Context, context.CancelFunc and error.
-// mongo.Client will be used for further database operation.
-// context.Context will be used set deadlines for process.
-// context.CancelFunc will be used to cancel context and
-// resource associated with it.
-
-func Connect(uri string) (*mongo.Client, context.Context,
-	context.CancelFunc, error) {
-
-	// ctx will be used to set deadline for process, here
-	// deadline will of 30 seconds.
-	ctx, cancel := context.WithTimeout(context.Background(),
-		30*time.Second)
-
-	// mongo.Connect return mongo.Client method
-	client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
-	return client, ctx, cancel, err
-}
-
-// This is a user defined method that accepts
-// mongo.Client and context.Context
-// This method used to ping the mongoDB, return error if any.
-func Ping(client *mongo.Client, ctx context.Context) error {
-
-	// mongo.Client has Ping to ping mongoDB, deadline of
-	// the Ping method will be determined by cxt
-	// Ping method return error if any occurred, then
-	// the error can be handled.
-	if err := client.Ping(ctx, readpref.Primary()); err != nil {
-		return err
-	}
-	fmt.Println("connected successfully")
-	return nil
-}
-
-// insertOne is a user defined method, used to insert
-// documents into collection returns result of InsertOne
-// and error if any.
-func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
-
-	// select database and collection ith Client.Database method
-	// and Database.Collection method
-	collection := client.Database(dataBase).Collection(col)
-
-	// InsertOne accept two argument of type Context
-	// and of empty interface
-	result, err := collection.InsertOne(ctx, doc)
-	return result, err
-}
-
-func Insertdata1(dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
-
-	// connect mongodb and insert records
-	client, ctx, cancel, err := Connect("mongodb://localhost:27017/test")
-	if err != nil {
-		panic(err)
-	}
-
-	// select database and collection ith Client.Database method
-	// and Database.Collection method
-	fmt.Println("Result of InsertOne", cancel)
-	collection := client.Database(dataBase).Collection(col)
-
-	// InsertOne accept two argument of type Context
-	// and of empty interface
-	result, err := collection.InsertOne(ctx, doc)
-	return result, err
-}
-
-//  for testing added
-
-func PostMarks(c *gin.Context) {
-
-	// Get the request body
-
-	// Decode the request body into a JSON object
-	var jsonBody map[string]interface{}
-	err := c.BindJSON(&jsonBody)
-	if err != nil {
-		// Handle error
-		return
-	}
-	Connect("mongodb://localhost:27017/test")
-	dbname := os.Getenv("DBNAME")
-	// var client mongo.Client
-	// var ctx context.Context
-	// insertOne accepts client , context, database
-	// name collection name and an interface that
-	// will be inserted into the  collection.
-	// insertOne returns an error and a result of
-	// insert in a single document into the collection.
-
-	insertOneResult, err := Insertdata1(dbname, "marks", jsonBody)
-
-	fmt.Println(err)
-
-	// handle the error
-	if err != nil {
-		panic(err)
-	}
-
-	// print the insertion id of the document,
-	// if it is inserted.
-	fmt.Println("Result of InsertOne")
-	fmt.Println(insertOneResult.InsertedID)
-	// Use the JSON object
-	fmt.Println(jsonBody)
-	c.IndentedJSON(http.StatusCreated, jsonBody)
-}
diff --git a/responses/user_response.go b/responses/user_response.go
new file mode 100644
index 0000000..cdc7698
--- /dev/null
+++ b/responses/user_response.go
@@ -0,0 +1,7 @@
+package responses
+
+type UserResponse struct {
+	Status  int                    `json:"status"`
+	Message string                 `json:"message"`
+	Data    map[string]interface{} `json:"data"`
+}
diff --git a/routes/routes.go b/routes/routes.go
new file mode 100644
index 0000000..2bf8184
--- /dev/null
+++ b/routes/routes.go
@@ -0,0 +1,10 @@
+package routes
+
+import (
+	"github.com/gin-gonic/gin"
+	markscontrollers "github.com/vinodnextcoder/golang-mongo-server/controllers"
+)
+
+func UserRoute(router *gin.Engine) {
+	router.POST("/user", markscontrollers.CreateUser())
+}

From 010e57129777142b3c43593091999c489e35266d Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Tue, 23 Jan 2024 14:32:12 +0530
Subject: [PATCH 07/12] update comment

---
 controllers/users.go | 60 ++++++++++++++++++++++++++++++++++++++++++++
 routes/routes.go     |  2 ++
 2 files changed, 62 insertions(+)

diff --git a/controllers/users.go b/controllers/users.go
index 347e6f5..2e1e221 100644
--- a/controllers/users.go
+++ b/controllers/users.go
@@ -9,6 +9,7 @@ import (
 	"github.com/vinodnextcoder/golang-mongo-server/configs"
 	"github.com/vinodnextcoder/golang-mongo-server/models"
 	"github.com/vinodnextcoder/golang-mongo-server/responses"
+	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 	"go.mongodb.org/mongo-driver/mongo"
 )
@@ -49,3 +50,62 @@ func CreateUser() gin.HandlerFunc {
 		c.JSON(http.StatusCreated, responses.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"data": result}})
 	}
 }
+
+func GetAUser() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+		userId := c.Param("userId")
+		var user models.User
+		defer cancel()
+
+		objId, _ := primitive.ObjectIDFromHex(userId)
+
+		err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&user)
+
+		if err != nil {
+			c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		c.JSON(http.StatusOK, responses.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": user}})
+	}
+}
+
+func EditAUser() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+		userId := c.Param("userId")
+		var user models.User
+		defer cancel()
+
+		objId, _ := primitive.ObjectIDFromHex(userId)
+
+		//validate the request body
+		if err := c.BindJSON(&user); err != nil {
+			c.JSON(http.StatusBadRequest, responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		update := bson.M{"name": user.Name, "location": user.Location, "title": user.Title}
+
+		result, err := userCollection.UpdateOne(ctx, bson.M{"id": objId}, bson.M{"$set": update})
+
+		if err != nil {
+			c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		//get updated user details
+		var updatedUser models.User
+		if result.MatchedCount == 1 {
+			err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedUser)
+
+			if err != nil {
+				c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+				return
+			}
+		}
+
+		c.JSON(http.StatusOK, responses.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": updatedUser}})
+	}
+}
diff --git a/routes/routes.go b/routes/routes.go
index 2bf8184..c3723aa 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -7,4 +7,6 @@ import (
 
 func UserRoute(router *gin.Engine) {
 	router.POST("/user", markscontrollers.CreateUser())
+	router.GET("/user/:userId", markscontrollers.GetAUser())
+	router.PUT("/user/:userId", markscontrollers.EditAUser())
 }

From 060a0df099fed92c3a392c402f0068b913f62e4b Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Tue, 23 Jan 2024 14:40:28 +0530
Subject: [PATCH 08/12] crud operation user

---
 controllers/users.go | 58 ++++++++++++++++++++++++++++++++++++++++++++
 routes/routes.go     |  2 ++
 2 files changed, 60 insertions(+)

diff --git a/controllers/users.go b/controllers/users.go
index 2e1e221..3ab647f 100644
--- a/controllers/users.go
+++ b/controllers/users.go
@@ -109,3 +109,61 @@ func EditAUser() gin.HandlerFunc {
 		c.JSON(http.StatusOK, responses.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": updatedUser}})
 	}
 }
+
+func DeleteAUser() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+		userId := c.Param("userId")
+		defer cancel()
+
+		objId, _ := primitive.ObjectIDFromHex(userId)
+
+		result, err := userCollection.DeleteOne(ctx, bson.M{"id": objId})
+
+		if err != nil {
+			c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		if result.DeletedCount < 1 {
+			c.JSON(http.StatusNotFound,
+				responses.UserResponse{Status: http.StatusNotFound, Message: "error", Data: map[string]interface{}{"data": "User with specified ID not found!"}},
+			)
+			return
+		}
+
+		c.JSON(http.StatusOK,
+			responses.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": "User successfully deleted!"}},
+		)
+	}
+}
+
+func GetAllUsers() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+		var users []models.User
+		defer cancel()
+
+		results, err := userCollection.Find(ctx, bson.M{})
+
+		if err != nil {
+			c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			return
+		}
+
+		//reading from the db in an optimal way
+		defer results.Close(ctx)
+		for results.Next(ctx) {
+			var singleUser models.User
+			if err = results.Decode(&singleUser); err != nil {
+				c.JSON(http.StatusInternalServerError, responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
+			}
+
+			users = append(users, singleUser)
+		}
+
+		c.JSON(http.StatusOK,
+			responses.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": users}},
+		)
+	}
+}
diff --git a/routes/routes.go b/routes/routes.go
index c3723aa..98ff2d4 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -9,4 +9,6 @@ func UserRoute(router *gin.Engine) {
 	router.POST("/user", markscontrollers.CreateUser())
 	router.GET("/user/:userId", markscontrollers.GetAUser())
 	router.PUT("/user/:userId", markscontrollers.EditAUser())
+	router.DELETE("/user/:userId", markscontrollers.DeleteAUser())
+	router.GET("/users", markscontrollers.GetAllUsers())
 }

From 00c05683ca0d71e229724ebb59e183538dc4c7b4 Mon Sep 17 00:00:00 2001
From: vinod <vinodk2757@gmail.com>
Date: Tue, 23 Jan 2024 14:41:27 +0530
Subject: [PATCH 09/12] Create gin.postman_collection.json

---
 .../gin.postman_collection.json               | 159 ++++++++++++++++++
 1 file changed, 159 insertions(+)
 create mode 100644 postman_collection/gin.postman_collection.json

diff --git a/postman_collection/gin.postman_collection.json b/postman_collection/gin.postman_collection.json
new file mode 100644
index 0000000..b7fd4fc
--- /dev/null
+++ b/postman_collection/gin.postman_collection.json
@@ -0,0 +1,159 @@
+{
+	"info": {
+		"_postman_id": "fa2b0741-c606-46a2-a4fa-6943d969fc94",
+		"name": "gin",
+		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
+		"_exporter_id": "30399582"
+	},
+	"item": [
+		{
+			"name": "create",
+			"request": {
+				"method": "POST",
+				"header": [],
+				"body": {
+					"mode": "raw",
+					"raw": "{\r\n    \"Name\": \"vinod\",\r\n    \"Location\": \"ddd\",\r\n    \"Title\": \"vinod\"\r\n}",
+					"options": {
+						"raw": {
+							"language": "json"
+						}
+					}
+				},
+				"url": {
+					"raw": "http://localhost:3001/user",
+					"protocol": "http",
+					"host": [
+						"localhost"
+					],
+					"port": "3001",
+					"path": [
+						"user"
+					]
+				}
+			},
+			"response": []
+		},
+		{
+			"name": "update",
+			"request": {
+				"method": "PUT",
+				"header": [],
+				"body": {
+					"mode": "raw",
+					"raw": "{\r\n    \"Name\": \"vinod test\",\r\n    \"Location\": \"ddd test\",\r\n    \"Title\": \"vinod\"\r\n}",
+					"options": {
+						"raw": {
+							"language": "json"
+						}
+					}
+				},
+				"url": {
+					"raw": "http://localhost:3001/user/65af7714d994f3f0953c5cb5",
+					"protocol": "http",
+					"host": [
+						"localhost"
+					],
+					"port": "3001",
+					"path": [
+						"user",
+						"65af7714d994f3f0953c5cb5"
+					]
+				}
+			},
+			"response": []
+		},
+		{
+			"name": "get user",
+			"protocolProfileBehavior": {
+				"disableBodyPruning": true
+			},
+			"request": {
+				"method": "GET",
+				"header": [],
+				"body": {
+					"mode": "raw",
+					"raw": "{\r\n     \"rollNo\": 175,\r\n        \"maths\":80,\r\n        \"science\": 90,\r\n        \"computer\":95\r\n}",
+					"options": {
+						"raw": {
+							"language": "json"
+						}
+					}
+				},
+				"url": {
+					"raw": "http://localhost:3001/user/65af7714d994f3f0953c5cb5",
+					"protocol": "http",
+					"host": [
+						"localhost"
+					],
+					"port": "3001",
+					"path": [
+						"user",
+						"65af7714d994f3f0953c5cb5"
+					]
+				}
+			},
+			"response": []
+		},
+		{
+			"name": "delete",
+			"request": {
+				"method": "DELETE",
+				"header": [],
+				"body": {
+					"mode": "raw",
+					"raw": "{\r\n     \"rollNo\": 175,\r\n        \"maths\":80,\r\n        \"science\": 90,\r\n        \"computer\":95\r\n}",
+					"options": {
+						"raw": {
+							"language": "json"
+						}
+					}
+				},
+				"url": {
+					"raw": "http://localhost:3001/user/65af7714d994f3f0953c5cb5",
+					"protocol": "http",
+					"host": [
+						"localhost"
+					],
+					"port": "3001",
+					"path": [
+						"user",
+						"65af7714d994f3f0953c5cb5"
+					]
+				}
+			},
+			"response": []
+		},
+		{
+			"name": "get user list",
+			"protocolProfileBehavior": {
+				"disableBodyPruning": true
+			},
+			"request": {
+				"method": "GET",
+				"header": [],
+				"body": {
+					"mode": "raw",
+					"raw": "{\r\n     \"rollNo\": 175,\r\n        \"maths\":80,\r\n        \"science\": 90,\r\n        \"computer\":95\r\n}",
+					"options": {
+						"raw": {
+							"language": "json"
+						}
+					}
+				},
+				"url": {
+					"raw": "http://localhost:3001/users",
+					"protocol": "http",
+					"host": [
+						"localhost"
+					],
+					"port": "3001",
+					"path": [
+						"users"
+					]
+				}
+			},
+			"response": []
+		}
+	]
+}
\ No newline at end of file

From 2b168f5e046c0f325ad2336796b1f1ff2d215619 Mon Sep 17 00:00:00 2001
From: vinod <31404750+vinodnextcoder@users.noreply.github.com>
Date: Tue, 23 Jan 2024 14:44:04 +0530
Subject: [PATCH 10/12] Update README.md

---
 README.md | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/README.md b/README.md
index d466faa..5ebe319 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,8 @@ swag init
 
 You can run your Gin web application by executing the main.go file:
 
+postman collection import https://github.com/vinodnextcoder/go-mongo-server/tree/00c05683ca0d71e229724ebb59e183538dc4c7b4/postman_collection
+
 go run main.go
 
 ````

From b16be26935232a01368f704da5f36c2982c3db95 Mon Sep 17 00:00:00 2001
From: vinod <31404750+vinodnextcoder@users.noreply.github.com>
Date: Tue, 23 Jan 2024 14:44:55 +0530
Subject: [PATCH 11/12] Update README.md

---
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 5ebe319..2b91d30 100644
--- a/README.md
+++ b/README.md
@@ -25,11 +25,12 @@ swag init
 
 You can run your Gin web application by executing the main.go file:
 
-postman collection import https://github.com/vinodnextcoder/go-mongo-server/tree/00c05683ca0d71e229724ebb59e183538dc4c7b4/postman_collection
+
 
 go run main.go
 
 ````
+# postman collection import https://github.com/vinodnextcoder/go-mongo-server/postman_collection
 # to call api endpoint in postman
 
 ```

From 06cdaff33b4716f175b90fd8dd5a174e8af54c91 Mon Sep 17 00:00:00 2001
From: vinod <31404750+vinodnextcoder@users.noreply.github.com>
Date: Tue, 23 Jan 2024 14:45:55 +0530
Subject: [PATCH 12/12] Update README.md

---
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 2b91d30..48e1978 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,8 @@ You can run your Gin web application by executing the main.go file:
 go run main.go
 
 ````
-# postman collection import https://github.com/vinodnextcoder/go-mongo-server/postman_collection
+# postman collection import 
+from go-mongo-server/postman_collection
 # to call api endpoint in postman
 
 ```
