daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Sep 28th, 2018 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "github.com/astaxie/beego/orm"
  5.     "./models"
  6.     "github.com/gin-gonic/gin"
  7.     "net/http"
  8. )
  9.  
  10. var ORM orm.Ormer
  11.  
  12. func init() {
  13.     models.ConnectToDb()
  14.     ORM = models.GetOrmObject()
  15. }
  16.  
  17. func main () {
  18.     router := gin.Default()
  19.     router.POST("/createShoe", createShoe)
  20.     router.GET("/readShoes", readShoes)
  21.     //router.PUT("/updateShoe", updateShoe)
  22.     router.Run(":8080")
  23. }
  24.  
  25. func createShoe(c *gin.Context) {
  26.     var NewShoe models.Shoes
  27.     c.BindJSON(&NewShoe)
  28.     _, err := ORM.Insert(&NewShoe)
  29.     if err == nil {
  30.         c.JSON(http.StatusOK, gin.H{
  31.             "status": http.StatusOK,
  32.             "shoeName": NewShoe.Name,
  33.             "trueToSizeData": NewShoe.TrueToSizeData,
  34.             "trueToSizeCalc": NewShoe.TrueToSizeCalc})
  35.     } else {
  36.         c.JSON(http.StatusInternalServerError,
  37.             gin.H{"status":http.StatusInternalServerError, "error":"Failed to create the entry"})
  38.  
  39.     }
  40. }
  41.  
  42. func readShoes(c *gin.Context) {
  43.     var shoe []models.Shoes
  44.     _, err := ORM.QueryTable("Shoes").All(&shoe)
  45.     if err == nil {
  46.         c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "shoes":&shoe})
  47.     } else {
  48.         c.JSON(http.StatusInternalServerError,
  49.             gin.H{"status": http.StatusInternalServerError, "error": "failed to read shoes"})
  50.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top