-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (132 loc) · 3.61 KB
/
main.go
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
package main
import (
"context"
"log"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoInstance struct {
Client *mongo.Client
Db *mongo.Database
}
var mg MongoInstance
// var dbName, mongoURI string
// dbName := goDotEnvVariable("DBNAME")
type Employee struct {
ID string `json:"id, omitempty" bson:"_id, omitempty`
Name string `json:"name"`
Salary float64 `json:"salary"`
Age float64 `json:"age"`
Gender string `json:"gender"`
}
func Connect() error {
//load env
godotenv.Load(".env")
dbName := os.Getenv("DBNAME")
mongoURI := os.Getenv("MONG0URI") + dbName
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
err = client.Connect(ctx)
db := client.Database(dbName)
if err != nil {
return err
}
mg = MongoInstance{
Client: client,
Db: db,
}
return nil
}
func cancel() {
panic("unimplemented")
}
func main() {
if err := Connect(); err != nil {
log.Fatal(err)
}
app := fiber.New()
app.Get("/employee", func(c *fiber.Ctx) error {
query := bson.D{{}}
cursor, err := mg.Db.Collection("employees").Find(c.Context(), query)
if err != nil {
return c.Status(500).SendString(err.Error())
}
var employees []Employee = make([]Employee, 0)
if err := cursor.All(c.Context(), &employees); err != nil {
return c.Status(500).SendString(err.Error())
}
return c.JSON(employees)
})
app.Post("/employee", func(c *fiber.Ctx) error {
collection := mg.Db.Collection("employees")
employee := new(Employee)
if err := c.BodyParser(employee); err != nil {
return c.Status(400).SendString(err.Error())
}
employee.ID = ""
insertResult, err := collection.InsertOne(c.Context(), employee)
if err != nil {
return c.Status(500).SendString(err.Error())
}
filter := bson.D{{Key: "_id", Value: insertResult.InsertedID}}
createdRecord := collection.FindOne(c.Context(), filter)
createdEmployee := &Employee{}
createdRecord.Decode(createdEmployee)
return c.Status(201).JSON(createdEmployee)
})
app.Put("/emplyee/:id", func(c *fiber.Ctx) error {
idParam := c.Params("id")
employeeID, err := primitive.ObjectIDFromHex(idParam)
if err != nil {
return c.SendStatus(400)
}
employee := new(Employee)
if err := c.BodyParser(employee); err != nil {
return c.Status(400).SendString(err.Error())
}
query := bson.D{{Key: "_id", Value: employeeID}}
update := bson.D{
{
Key: "$set",
Value: bson.D{
{Key: "name", Value: employee.Name},
{Key: "age", Value: employee.Age},
{Key: "salary", Value: employee.Salary},
{Key: "gender", Value: employee.Gender},
},
},
}
err = mg.Db.Collection("employees").FindOneAndUpdate(c.Context(), query, update).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return c.SendStatus(400)
}
return c.SendStatus(500)
}
employee.ID = idParam
return c.Status(200).JSON(employee)
})
app.Delete("emplyee/:id", func(c *fiber.Ctx) error {
employeeID, err := primitive.ObjectIDFromHex(c.Params("id"),)
if err != nil {
return c.SendStatus(400)
}
query := bson.D{{Key:"_id", Value: employeeID}}
result, err := mg.Db.Collection("employees").DeleteOne(c.Context(), &query)
if err != nil {
return c.SendStatus(500)
}
if result.DeletedCount < 1{
return c.SendStatus(404)
}
return c.Status(200).JSON("record deleted")
})
log.Fatal(app.Listen(":3000"))
}