package api import ( "log" "strconv" "time" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) type Goods struct { Id int64 `json:"id"` Name string `json:"name"` Description string `json:"description"` SupplierId int64 `json:"supplier_id"` MarketId int64 `json:"market_id"` Quantity int64 `json:"quantity"` Price string `json:"price"` } func handleGetGoods(c *gin.Context) { marketId, err := strconv.ParseInt(c.Param("marketid"), 10, 64) if err != nil { c.AbortWithError(500, err) return } log.Println("select", marketId) rows, err := db.Query(`select id, name, description, quantity, price from goods where market_id = $1 order by name, description`, marketId) if err != nil { c.AbortWithError(500, err) return } ret := make([]Goods, 0) var g Goods for rows.Next() { err = rows.Scan(&g.Id, &g.Name, &g.Description, &g.Quantity, &g.Price) if err != nil { c.AbortWithError(500, err) return } ret = append(ret, g) } c.JSON(200, gin.H{ "goods": ret, }) } func handleBuy(c *gin.Context) { session := sessions.Default(c) userId := session.Get("userid").(int64) goodsId := c.Param("goodsid") tx, err := db.Begin() if err != nil { c.AbortWithError(500, err) return } _, err = tx.Exec(`insert into purchase (user_id, goods_id) values ($1, $2)`, userId, goodsId) if err != nil { tx.Rollback() c.AbortWithError(500, err) return } _, err = tx.Exec(`update goods set quantity = quantity - 1 where id = $1`, goodsId) if err != nil { tx.Rollback() c.AbortWithError(500, err) return } tx.Commit() c.JSON(200, gin.H{}) } func handleGetPurchaseHistory(c *gin.Context) { session := sessions.Default(c) userId := session.Get("userid").(int64) rows, err := db.Query(` select p.id, p.quantity, p.purchased_time, g.name, g.price from purchase p join goods g on p.goods_id = g.id where p.user_id = $1 order by p.purchased_time desc `, userId) if err != nil { c.AbortWithError(500, err) return } type Ret struct { Id int64 `json:"id"` Quantity int64 `json:"quantity"` PurchasedTime time.Time `json:"purchased_time"` Goods Goods `json:"goods"` } ret := make([]Ret, 0) for rows.Next() { var i Ret err = rows.Scan(&i.Id, &i.Quantity, &i.PurchasedTime, &i.Goods.Name, &i.Goods.Price) if err != nil { c.AbortWithError(500, err) return } ret = append(ret, i) } c.JSON(200, ret) } func handleDeletePurchaseHistory(c *gin.Context) { id, err := strconv.ParseInt(c.Param("purchaseid"), 10, 64) if err != nil { c.AbortWithError(500, err) return } _, err = db.Exec(`delete from purchase where id = $1`, id) if err != nil { c.AbortWithError(500, err) return } c.JSON(200, gin.H{}) } func handleGetGodsBySupplier(c *gin.Context) { session := sessions.Default(c) userId := session.Get("userid").(int64) rows, err := db.Query(`select id, name, description, quantity, price from goods where supplier_id = $1 order by name, description`, userId) if err != nil { c.AbortWithError(500, err) return } ret := make([]Goods, 0) for rows.Next() { var g Goods err = rows.Scan(&g.Id, &g.Name, &g.Description, &g.Quantity, &g.Price) if err != nil { c.AbortWithError(500, err) return } ret = append(ret, g) } c.JSON(200, ret) } func handleCreateGoods(c *gin.Context) { session := sessions.Default(c) userId := session.Get("userid").(int64) g := &Goods{} err := c.BindJSON(g) if err != nil { c.AbortWithError(500, err) return } g.SupplierId = userId _, err = db.Exec(`insert into goods (name, description, supplier_id, market_id, quantity, price) values ($1, $2, $3, $4, $5, $6)`, g.Name, g.Description, g.SupplierId, g.MarketId, g.Quantity,g.Price) if err != nil { c.AbortWithError(500, err) return } c.JSON(200, gin.H{}) }