Created
September 7, 2022 14:45
-
-
Save Brymes/c34809ada9d7613a401b29abd294059d to your computer and use it in GitHub Desktop.
Sample Gin Application
This file contains 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/gin-gonic/gin" | |
"gorm.io/driver/sqlite" | |
"gorm.io/gorm" | |
"log" | |
"net/http" | |
) | |
type Companies struct { | |
Name string `gorm:"primary_key" json:"name"` | |
Created string `json:"created"` | |
Product string `json:"product"` | |
} | |
var DBInstance *gorm.DB | |
func DBConnection() error { | |
DBInstance, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) | |
if err != nil { | |
return err | |
} | |
err = DBInstance.AutoMigrate(&Companies{}) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func GetCompany(ctx *gin.Context) { | |
var company Companies | |
name := ctx.Param("company") | |
if err := DBInstance.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 := DBInstance.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 := DBInstance.Where("company = ?", name).First(&company).Error; err != nil { | |
ctx.JSON(http.StatusNotFound, gin.H{"error": "Company doesn't exist "}) | |
return | |
} | |
if err := ctx.ShouldBindJSON(&company); err != nil { | |
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
return | |
} | |
if err := DBInstance.Model(&company).Updates(Companies{ | |
Name: company.Name, | |
Created: company.Created, | |
Product: company.Product, | |
}).Error; err != nil { | |
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | |
return | |
} | |
ctx.JSON(http.StatusOK, company) | |
} | |
func DeleteCompany(ctx *gin.Context) { | |
var company Companies | |
name := ctx.Param("company") | |
if err := DBInstance.Where("company = ?", name).First(&company).Error; err != nil { | |
ctx.JSON(http.StatusNotFound, gin.H{"error": "company not found!"}) | |
return | |
} | |
if err := DBInstance.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.Println("Error creating Database Connection") | |
log.Fatalln(err) | |
} | |
router := gin.Default() | |
router.GET("api/v1/:company", GetCompany) | |
router.POST("/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