From edf9fd3555af9989c468f911a2e9a766512e27fd Mon Sep 17 00:00:00 2001 From: stewartmbofana Date: Tue, 14 Oct 2025 06:41:01 +0200 Subject: [PATCH 1/3] Chore: Optimize MySql Recipe --- mysql/main.go | 116 ++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 65 deletions(-) diff --git a/mysql/main.go b/mysql/main.go index 1dfbf9dd69..dd3680a6df 100644 --- a/mysql/main.go +++ b/mysql/main.go @@ -7,6 +7,7 @@ import ( "database/sql" "fmt" "log" + "net/http" _ "github.com/go-sql-driver/mysql" "github.com/gofiber/fiber/v2" @@ -17,8 +18,6 @@ var db *sql.DB // Database settings const ( - host = "localhost" - port = 5432 // Default port user = "root" password = "password" dbname = "fiber_demo" @@ -32,10 +31,7 @@ type Employee struct { Age int `json:"age"` } -// Employees struct -type Employees struct { - Employees []Employee `json:"employees"` -} + // Connect function func Connect() error { @@ -60,96 +56,86 @@ func main() { // Create a Fiber app app := fiber.New() - // Get all records from MySQL app.Get("/employee", func(c *fiber.Ctx) error { - // Get Employee list from database - rows, err := db.Query("SELECT id, name, salary, age FROM employees order by id") + rows, err := db.Query("SELECT id, name, salary, age FROM employees ORDER BY id") if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } defer rows.Close() - result := Employees{} + var employees []Employee for rows.Next() { - employee := Employee{} - if err := rows.Scan(&employee.ID, &employee.Name, &employee.Salary, &employee.Age); err != nil { - return err // Exit if we get an error + var emp Employee + if err := rows.Scan(&emp.ID, &emp.Name, &emp.Salary, &emp.Age); err != nil { + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } + employees = append(employees, emp) + } + return c.JSON(employees) + }) - // Append Employee to Employees - result.Employees = append(result.Employees, employee) + app.Get("/employee/:id", func(c *fiber.Ctx) error { + id := c.Params("id") + var emp Employee + err := db.QueryRow("SELECT id, name, salary, age FROM employees WHERE id = ?", id).Scan(&emp.ID, &emp.Name, &emp.Salary, &emp.Age) + if err != nil { + if err == sql.ErrNoRows { + return c.SendStatus(http.StatusNotFound) + } + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } - // Return Employees in JSON format - return c.JSON(result) + return c.JSON(emp) }) - // Add record into MySQL app.Post("/employee", func(c *fiber.Ctx) error { - // New Employee struct - u := new(Employee) - - // Parse body into struct - if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + var emp Employee + if err := c.BodyParser(&emp); err != nil { + return c.Status(http.StatusBadRequest).SendString(err.Error()) } - // Insert Employee into database - res, err := db.Query("INSERT INTO employees (NAME, SALARY, AGE) VALUES (?, ?, ?)", u.Name, u.Salary, u.Age) + result, err := db.Exec("INSERT INTO employees (name, salary, age) VALUES (?, ?, ?)", emp.Name, emp.Salary, emp.Age) if err != nil { - return err + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } - // Print result - log.Println(res) - - // Return Employee in JSON format - return c.JSON(u) + id, _ := result.LastInsertId() + emp.ID = int(id) + return c.Status(http.StatusCreated).JSON(emp) }) - // Update record into MySQL - app.Put("/employee", func(c *fiber.Ctx) error { - // New Employee struct - u := new(Employee) - - // Parse body into struct - if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + app.Put("/employee/:id", func(c *fiber.Ctx) error { + id := c.Params("id") + var emp Employee + if err := c.BodyParser(&emp); err != nil { + return c.Status(http.StatusBadRequest).SendString(err.Error()) } - // Update Employee record in database - res, err := db.Query("UPDATE employees SET name=?,salary=?,age=? WHERE id=?", u.Name, u.Salary, u.Age, u.ID) + result, err := db.Exec("UPDATE employees SET name=?, salary=?, age=? WHERE id=?", emp.Name, emp.Salary, emp.Age, id) if err != nil { - return err + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } - // Print result - log.Println(res) + rowsAffected, _ := result.RowsAffected() + if rowsAffected == 0 { + return c.SendStatus(http.StatusNotFound) + } - // Return Employee in JSON format - return c.Status(201).JSON(u) + return c.JSON(emp) }) - // Delete record from MySQL - app.Delete("/employee", func(c *fiber.Ctx) error { - // New Employee struct - u := new(Employee) - - // Parse body into struct - if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) - } - - // Delete Employee from database - res, err := db.Query("DELETE FROM employees WHERE id = ?", u.ID) + app.Delete("/employee/:id", func(c *fiber.Ctx) error { + id := c.Params("id") + result, err := db.Exec("DELETE FROM employees WHERE id = ?", id) if err != nil { - return err + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } - // Print result - log.Println(res) + rowsAffected, _ := result.RowsAffected() + if rowsAffected == 0 { + return c.SendStatus(http.StatusNotFound) + } - // Return Employee in JSON format - return c.JSON("Deleted") + return c.SendStatus(http.StatusNoContent) }) log.Fatal(app.Listen(":3000")) From fc88e8380afdc0449d6f8d9c1885a1aace83f985 Mon Sep 17 00:00:00 2001 From: stewartmbofana Date: Tue, 14 Oct 2025 06:56:38 +0200 Subject: [PATCH 2/3] Review Changes --- mysql/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mysql/main.go b/mysql/main.go index dd3680a6df..5c732fd9b6 100644 --- a/mysql/main.go +++ b/mysql/main.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "net/http" + "strconv" _ "github.com/go-sql-driver/mysql" "github.com/gofiber/fiber/v2" @@ -31,8 +32,6 @@ type Employee struct { Age int `json:"age"` } - - // Connect function func Connect() error { var err error @@ -71,6 +70,9 @@ func main() { } employees = append(employees, emp) } + if err := rows.Err(); err != nil { + return c.Status(http.StatusInternalServerError).SendString(err.Error()) + } return c.JSON(employees) }) @@ -84,6 +86,7 @@ func main() { } return c.Status(http.StatusInternalServerError).SendString(err.Error()) } + return c.JSON(emp) }) @@ -100,11 +103,16 @@ func main() { id, _ := result.LastInsertId() emp.ID = int(id) + return c.Status(http.StatusCreated).JSON(emp) }) app.Put("/employee/:id", func(c *fiber.Ctx) error { - id := c.Params("id") + idParam := c.Params("id") + id, err := strconv.Atoi(idParam) + if err != nil { + return c.Status(http.StatusBadRequest).SendString("invalid employee id") + } var emp Employee if err := c.BodyParser(&emp); err != nil { return c.Status(http.StatusBadRequest).SendString(err.Error()) @@ -124,7 +132,11 @@ func main() { }) app.Delete("/employee/:id", func(c *fiber.Ctx) error { - id := c.Params("id") + idParam := c.Params("id") + id, err := strconv.Atoi(idParam) + if err != nil { + return c.Status(http.StatusBadRequest).SendString("invalid employee id") + } result, err := db.Exec("DELETE FROM employees WHERE id = ?", id) if err != nil { return c.Status(http.StatusInternalServerError).SendString(err.Error()) From 97b27aed110276a2f0a05daaeae1f8225adf79ae Mon Sep 17 00:00:00 2001 From: stewartmbofana Date: Tue, 14 Oct 2025 07:02:48 +0200 Subject: [PATCH 3/3] Review change --- mysql/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mysql/main.go b/mysql/main.go index 5c732fd9b6..d887ed1074 100644 --- a/mysql/main.go +++ b/mysql/main.go @@ -77,9 +77,13 @@ func main() { }) app.Get("/employee/:id", func(c *fiber.Ctx) error { - id := c.Params("id") + idParam := c.Params("id") + id, err := strconv.Atoi(idParam) + if err != nil { + return c.Status(http.StatusBadRequest).SendString("invalid employee id") + } var emp Employee - err := db.QueryRow("SELECT id, name, salary, age FROM employees WHERE id = ?", id).Scan(&emp.ID, &emp.Name, &emp.Salary, &emp.Age) + err = db.QueryRow("SELECT id, name, salary, age FROM employees WHERE id = ?", id).Scan(&emp.ID, &emp.Name, &emp.Salary, &emp.Age) if err != nil { if err == sql.ErrNoRows { return c.SendStatus(http.StatusNotFound)