diff --git a/handler/handler.go b/handler/handler.go index add478b..706eaa7 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -1,8 +1,8 @@ package handler import ( - "log" "database/sql" + "log" "github.com/gofiber/fiber" @@ -11,7 +11,6 @@ import ( "github.com/firebase007/go-rest-api-with-fiber/database" ) - // GetAllProducts from db func GetAllProducts(c *fiber.Ctx) { @@ -20,8 +19,8 @@ func GetAllProducts(c *fiber.Ctx) { if err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, - "error": err, - }) + "error": err, + }) return } @@ -36,8 +35,8 @@ func GetAllProducts(c *fiber.Ctx) { if err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, - "error": err, - }) + "error": err, + }) return } @@ -48,18 +47,17 @@ func GetAllProducts(c *fiber.Ctx) { // Return Products in JSON format if err := c.JSON(&fiber.Map{ "success": true, - "product": result, + "product": result, "message": "All product returned successfully", - }); err != nil { + }); err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) return } } - // GetSingleProduct from db func GetSingleProduct(c *fiber.Ctx) { @@ -72,7 +70,7 @@ func GetSingleProduct(c *fiber.Ctx) { c.Status(500).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) return } @@ -80,39 +78,38 @@ func GetSingleProduct(c *fiber.Ctx) { // iterate through the values of the row for row.Next() { - switch err := row.Scan(&id, &product.Amount, &product.Name, &product.Description, &product.Category ); err { + switch err := row.Scan(&id, &product.Amount, &product.Name, &product.Description, &product.Category); err { case sql.ErrNoRows: - log.Println("No rows were returned!") - c.Status(500).JSON(&fiber.Map{ + log.Println("No rows were returned!") + c.Status(500).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) case nil: - log.Println(product.Name, product.Description, product.Category, product.Amount) + log.Println(product.Name, product.Description, product.Category, product.Amount) default: // panic(err) - c.Status(500).JSON(&fiber.Map{ + c.Status(500).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) + } + } -} - // return product in JSON format if err := c.JSON(&fiber.Map{ "success": false, "message": "Successfully fetched product", "product": product, - }); err != nil { + }); err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, - "message": err, - }) + "message": err, + }) return } - } // CreateProduct handler @@ -127,64 +124,145 @@ func CreateProduct(c *fiber.Ctx) { c.Status(400).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) return } // Insert Product into database - res, err := database.DB.Query("INSERT INTO products (name, description, category, amount) VALUES ($1, $2, $3, $4)" , p.Name, p.Description, p.Category, p.Amount ) + res, err := database.DB.Query("INSERT INTO products (name, description, category, amount) VALUES ($1, $2, $3, $4)", p.Name, p.Description, p.Category, p.Amount) if err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, "message": err, - }) + }) return } // Print result log.Println(res) - + // Return Product in JSON format if err := c.JSON(&fiber.Map{ "success": true, "message": "Product successfully created", "product": p, - }); err != nil { + }); err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, - "message": "Error creating product", - }) + "message": "Error creating product", + }) return } } - -// DeleteProduct from db +// DeleteProduct from db func DeleteProduct(c *fiber.Ctx) { - id := c.Params("id") + id := c.Params("id") - // query product table in database - res, err := database.DB.Query("DELETE FROM products WHERE id = $1", id) - if err != nil { - c.Status(500).JSON(&fiber.Map{ - "success": false, - "error": err, - }) - return - } + // query product table in database + res, err := database.DB.Query("DELETE FROM products WHERE id = $1", id) + if err != nil { + c.Status(500).JSON(&fiber.Map{ + "success": false, + "error": err, + }) + return + } + + // Print result + log.Println(res) + + // return product in JSON format + if err := c.JSON(&fiber.Map{ + "success": true, + "message": "product deleted successfully", + }); err != nil { + c.Status(500).JSON(&fiber.Map{ + "success": false, + "error": err, + }) + return + } +} + +// UpdateProduct handler +func UpdateProduct(c *fiber.Ctx) { - // Print result - log.Println(res) + id := c.Params("id") + p := new(model.Product) - // return product in JSON format - if err := c.JSON(&fiber.Map{ - "success": true, - "message": "product deleted successfully", - }); err != nil { + // Parse the request body into the product struct + if err := c.BodyParser(p); err != nil { + log.Println(err) + c.Status(400).JSON(&fiber.Map{ + "success": false, + "message": err.Error(), + }) + return + } + + // Update the product in the database + res, err := database.DB.Query("UPDATE products SET name = $1, description = $2, category = $3, amount = $4 WHERE id = $5", p.Name, p.Description, p.Category, p.Amount, id) + if err != nil { + log.Println(err) + c.Status(500).JSON(&fiber.Map{ + "success": false, + "message": err.Error(), + }) + return + } + log.Println(res) + + // Return success response + if err := c.JSON(&fiber.Map{ + "success": true, + "message": "Product successfully updated", + "product": p, + }); err != nil { + c.Status(500).JSON(&fiber.Map{ + "success": false, + "message": "Failed to return updated product", + }) + } +} + +// GetProductsByCategory returns all products in a given category +func GetProductsByCategory(c *fiber.Ctx) { + category := c.Params("category") + result := model.Products{} + + // Query the database + rows, err := database.DB.Query("SELECT name, description, category, amount FROM products WHERE category = $1", category) + if err != nil { + c.Status(500).JSON(&fiber.Map{ + "success": false, + "message": err.Error(), + }) + return + } + defer rows.Close() + + for rows.Next() { + product := model.Product{} + if err := rows.Scan(&product.Name, &product.Description, &product.Category, &product.Amount); err != nil { c.Status(500).JSON(&fiber.Map{ "success": false, - "error": err, - }) + "message": err.Error(), + }) return } -} \ No newline at end of file + result.Products = append(result.Products, product) + } + + // Return the result + if err := c.JSON(&fiber.Map{ + "success": true, + "message": "Products by category fetched successfully", + "product": result, + }); err != nil { + c.Status(500).JSON(&fiber.Map{ + "success": false, + "message": "Failed to return category products", + }) + } +} diff --git a/router/route.go b/router/route.go index 40fd0be..e1e3289 100644 --- a/router/route.go +++ b/router/route.go @@ -1,25 +1,24 @@ package router - import ( "github.com/firebase007/go-rest-api-with-fiber/handler" "github.com/firebase007/go-rest-api-with-fiber/middleware" "github.com/gofiber/fiber" - ) // SetupRoutes func -func SetupRoutes (app *fiber.App) { - +func SetupRoutes(app *fiber.App) { + // Middleware - api := app.Group("/api", middleware.AuthReq()) - + api := app.Group("/api", middleware.AuthReq()) + // routes api.Get("/", handler.GetAllProducts) api.Get("/:id", handler.GetSingleProduct) api.Post("/", handler.CreateProduct) api.Delete("/:id", handler.DeleteProduct) - -} \ No newline at end of file + api.Put("/api/product/:id", handler.UpdateProduct) + api.Get("/product/category/:category", handler.GetProductsByCategory) +}