Skip to content

new update hendler #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: new
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
184 changes: 131 additions & 53 deletions handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package handler

import (
"log"
"database/sql"
"log"

"github.com/gofiber/fiber"

Expand All @@ -11,7 +11,6 @@ import (
"github.com/firebase007/go-rest-api-with-fiber/database"
)


// GetAllProducts from db
func GetAllProducts(c *fiber.Ctx) {

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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) {

Expand All @@ -72,47 +70,46 @@ func GetSingleProduct(c *fiber.Ctx) {
c.Status(500).JSON(&fiber.Map{
"success": false,
"message": err,
})
})
return
}

defer row.Close()

// 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
Expand All @@ -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
}
}
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",
})
}
}
15 changes: 7 additions & 8 deletions router/route.go
Original file line number Diff line number Diff line change
@@ -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)

}
api.Put("/api/product/:id", handler.UpdateProduct)
api.Get("/product/category/:category", handler.GetProductsByCategory)
}