Last active
February 13, 2022 07:12
-
-
Save devmnj/08d7eb6dfa2e5daf7ca59a7ecf20cfc3 to your computer and use it in GitHub Desktop.
GO Product API using GORM and Fiber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import | |
| ("github.com/gofiber/fiber/v2" | |
| "example/REST_API/product" | |
| "example/REST_API/database" | |
| "fmt" | |
| "log" | |
| "gorm.io/gorm" | |
| "gorm.io/driver/sqlite" | |
| ) | |
| func index(c *fiber.Ctx) error { | |
| return c.SendString("<h1>This is a Fiber-GO API</h1>") | |
| } | |
| func setupRoutes(app *fiber.App){ | |
| app.Get("/",index ) | |
| app.Get("/api/v1/products",product.GetProducts ) | |
| app.Get("/api/v1/products/:id",product.GetProduct ) | |
| app.Post("/api/v1/demoproducts",product.DemoProduct ) | |
| app.Post("/api/v1/products",product.AddProduct ) | |
| app.Delete("/api/v1/products/:id",product.DelProduct ) | |
| } | |
| func initDB(){ | |
| var err error | |
| database.DBConn, err = gorm.Open(sqlite.Open("products.db"), &gorm.Config{}) | |
| if err != nil { | |
| panic("failed to connect database") | |
| } | |
| fmt.Println("Database connection successfully Opened !") | |
| database.DBConn.AutoMigrate(product.Product{}) | |
| fmt.Println("Migration completed successfully") | |
| } | |
| func main(){ | |
| app:= fiber.New() | |
| initDB() | |
| setupRoutes(app) | |
| fmt.Println("Server starting in a minute") | |
| log.Fatal(app.Listen(":3000")) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package product | |
| import ( | |
| "github.com/gofiber/fiber/v2" | |
| "example/REST_API/database" | |
| // "gorm.io/driver/sqlite" | |
| "gorm.io/gorm" | |
| ) | |
| type Product struct{ | |
| gorm.Model | |
| Name string `json:"name"` | |
| Category string `json:category` | |
| Price int `json:"price"` | |
| } | |
| func GetProducts(c *fiber.Ctx) error { | |
| db:=database.DBConn | |
| var products []Product | |
| db.Find(&products) | |
| return c.JSON(products) | |
| } | |
| func GetProduct(c *fiber.Ctx) error { | |
| db:=database.DBConn | |
| id:=c.Params("id") | |
| var product Product | |
| db.Find(&product,id) | |
| return c.JSON(product) | |
| } | |
| func DemoProduct(c *fiber.Ctx) error { | |
| db:= database.DBConn | |
| var product Product | |
| product.Name="Mac Mini" | |
| product.Category="Computer" | |
| product.Price=10000 | |
| db.Create(&product) | |
| return c.JSON(product) | |
| } | |
| func DelProduct(c *fiber.Ctx) error { | |
| db:=database.DBConn | |
| id:=c.Params("id") | |
| var product Product | |
| db.Find(&product,id) | |
| if product.Name==""{ | |
| c.Status(500).SendString("No Product located") | |
| } | |
| db.Delete(product) | |
| return c.SendString("Product Deleted") | |
| } | |
| func AddProduct(c *fiber.Ctx) error { | |
| db:= database.DBConn | |
| product:=new (Product) | |
| if err:=c.BodyParser(product); err!=nil{ | |
| c.Status(503) | |
| } | |
| db.Create(&product) | |
| return c.JSON(product) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment