Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth-docker-postgres-jwt/handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "github.com/gofiber/fiber/v2"

// Hello handle api status
func Hello(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"status": "success", "message": "Hello i'm ok!", "data": nil})
return c.JSON(fiber.Map{"status": "success", "message": "Hello I'm ok!", "data": nil})
}
119 changes: 34 additions & 85 deletions mongodb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,9 @@ func Connect() error {
return err
}

defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()

db := client.Database(dbName)

if err != nil {
return err
}

mg = MongoInstance{
Client: client,
Db: db,
Db: client.Database(dbName),
}

return nil
Expand All @@ -77,144 +65,105 @@ func main() {
// Get all employee records from MongoDB
// Docs: https://docs.mongodb.com/manual/reference/command/find/
app.Get("/employee", func(c *fiber.Ctx) error {
// get all records as a cursor
query := bson.D{{}}
cursor, err := mg.Db.Collection("employees").Find(c.Context(), query)
cursor, err := mg.Db.Collection("employees").Find(c.Context(), bson.D{})
if err != nil {
return c.Status(500).SendString(err.Error())
}
defer cursor.Close(c.Context())

var employees []Employee = make([]Employee, 0)

// iterate the cursor and decode each item into an Employee
var employees []Employee
if err := cursor.All(c.Context(), &employees); err != nil {
return c.Status(500).SendString(err.Error())
}
// return employees list in JSON format
return c.JSON(employees)
})

// Get once employee records from MongoDB
// Docs: https://www.mongodb.com/blog/post/quick-start-golang--mongodb--how-to-read-documents
app.Get("/employee/:id", func(c *fiber.Ctx) error {
// get id by params
params := c.Params("id")

_id, err := primitive.ObjectIDFromHex(params)
id, err := primitive.ObjectIDFromHex(c.Params("id"))
if err != nil {
return c.Status(500).SendString(err.Error())
return c.SendStatus(400)
}

filter := bson.D{{Key: "_id", Value: _id}}

var result Employee

if err := mg.Db.Collection("employees").FindOne(c.Context(), filter).Decode(&result); err != nil {
return c.Status(500).SendString("Something went wrong.")
var employee Employee
err = mg.Db.Collection("employees").FindOne(c.Context(), bson.M{"_id": id}).Decode(&employee)
if err != nil {
if err == mongo.ErrNoDocuments {
return c.SendStatus(404)
}
return c.SendStatus(500)
}

return c.Status(fiber.StatusOK).JSON(result)
return c.JSON(employee)
})

// Insert a new employee into MongoDB
// Docs: https://docs.mongodb.com/manual/reference/command/insert/
app.Post("/employee", func(c *fiber.Ctx) error {
collection := mg.Db.Collection("employees")

// New Employee struct
employee := new(Employee)
// Parse body into struct
if err := c.BodyParser(employee); err != nil {
var employee Employee
if err := c.BodyParser(&employee); err != nil {
return c.Status(400).SendString(err.Error())
}

// force MongoDB to always set its own generated ObjectIDs
employee.ID = ""

// insert the record
insertionResult, err := collection.InsertOne(c.Context(), employee)
result, err := mg.Db.Collection("employees").InsertOne(c.Context(), employee)
if err != nil {
return c.Status(500).SendString(err.Error())
}

// get the just inserted record in order to return it as response
filter := bson.D{{Key: "_id", Value: insertionResult.InsertedID}}
createdRecord := collection.FindOne(c.Context(), filter)

// decode the Mongo record into Employee
createdEmployee := &Employee{}
createdRecord.Decode(createdEmployee)

// return the created Employee in JSON format
return c.Status(201).JSON(createdEmployee)
employee.ID = result.InsertedID.(primitive.ObjectID).Hex()
return c.Status(201).JSON(employee)
})

// Update an employee record in MongoDB
// Docs: https://docs.mongodb.com/manual/reference/command/findAndModify/
app.Put("/employee/:id", func(c *fiber.Ctx) error {
idParam := c.Params("id")
employeeID, err := primitive.ObjectIDFromHex(idParam)
// the provided ID might be invalid ObjectID
id, err := primitive.ObjectIDFromHex(c.Params("id"))
if err != nil {
return c.SendStatus(400)
}

employee := new(Employee)
// Parse body into struct
if err := c.BodyParser(employee); err != nil {
var employee Employee
if err := c.BodyParser(&employee); err != nil {
return c.Status(400).SendString(err.Error())
}

// Find the employee and update its data
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},
},
},
}
err = mg.Db.Collection("employees").FindOneAndUpdate(c.Context(), query, update).Err()
update := bson.M{"$set": bson.M{
"name": employee.Name,
"age": employee.Age,
"salary": employee.Salary,
}}

err = mg.Db.Collection("employees").FindOneAndUpdate(c.Context(), bson.M{"_id": id}, update).Err()
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return c.SendStatus(404)
}
return c.SendStatus(500)
}

// return the updated employee
employee.ID = idParam
return c.Status(200).JSON(employee)
employee.ID = c.Params("id")
return c.JSON(employee)
})

// Delete an employee from MongoDB
// Docs: https://docs.mongodb.com/manual/reference/command/delete/
app.Delete("/employee/:id", func(c *fiber.Ctx) error {
employeeID, err := primitive.ObjectIDFromHex(
c.Params("id"),
)
// the provided ID might be invalid ObjectID
id, err := primitive.ObjectIDFromHex(c.Params("id"))
if err != nil {
return c.SendStatus(400)
}

// find and delete the employee with the given ID
query := bson.D{{Key: "_id", Value: employeeID}}
result, err := mg.Db.Collection("employees").DeleteOne(c.Context(), &query)
result, err := mg.Db.Collection("employees").DeleteOne(c.Context(), bson.M{"_id": id})
if err != nil {
return c.SendStatus(500)
}

// the employee might not exist
if result.DeletedCount < 1 {
if result.DeletedCount == 0 {
return c.SendStatus(404)
}

// the record was deleted
return c.SendStatus(204)
})

Expand Down