Skip to content

Instantly share code, notes, and snippets.

@Goodnessuc
Last active January 18, 2024 09:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Goodnessuc/e4fdc78b04965e991efc440f50705ece to your computer and use it in GitHub Desktop.
Save Goodnessuc/e4fdc78b04965e991efc440f50705ece to your computer and use it in GitHub Desktop.
CRUD API in Go using the Gin FrameWork - Article for Earthly Technologies
package main
import (
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"net/http"
)
type Companies struct {
Name string `json:"name"`
Created string `json:"created"`
Product string `json:"product"`
}
var (
db *gorm.DB
err error
)
func DBConnection() error {
db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
return err
}
err = db.AutoMigrate(&Companies{})
if err != nil {
return err
}
return nil
}
func GetCompany(ctx *gin.Context) {
var company Companies
name := ctx.Param("company")
if err := db.Where("name= ?", name).First(&company).Error; err != nil {
ctx.JSON(http.StatusNotFound, gin.H{"Failed": "Company not found"})
return
}
ctx.JSON(http.StatusOK, company)
}
func PostCompany(ctx *gin.Context) {
var company Companies
if err := ctx.ShouldBindJSON(&company); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error Decoding JSON"})
return
}
newCompany := Companies{
Name: company.Name,
Created: company.Created,
Product: company.Product,
}
if err := db.Create(&newCompany).Error; err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Database Migration Error"})
}
}
func UpdateCompany(ctx *gin.Context) {
var company Companies
name := ctx.Param("company")
if err = db.Where("name = ?", name).First(&company).Error; err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
err = ctx.BindJSON(&company)
if err != nil {
return
}
db.Save(&company)
ctx.JSON(http.StatusOK, company)
}
func DeleteCompany(ctx *gin.Context) {
var company Companies
name := ctx.Param("company")
if err := db.Where("name = ?", name).Delete(&company).Error; err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "Company Deleted"})
}
func main() {
err := DBConnection()
if err != nil {
log.Fatal("Database connection error", err)
}
router := gin.Default()
router.GET("api/v1/:company", GetCompany)
router.POST("api/v1/company", PostCompany)
router.PUT("api/v1/:company", UpdateCompany)
router.DELETE("api/v1/:company", DeleteCompany)
log.Fatal(router.Run(":8080"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment