我希望看到visits
每个GET
请求的增量,/foo
但它仍然是1
. 我在这里做错了什么?
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Item model
type Item struct {
gorm.Model
UID int64 `gorm:"primaryKey;autoIncrement"`
Name string `gorm:"index;not null"`
Visits int32 `gorm:"default 0"`
}
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
db.AutoMigrate(&Item{})
db.Create(&Item{
Name: "foo",
})
app := fiber.New(fiber.Config{})
app.Get("/:name", func(c *fiber.Ctx) error {
var i Item
db.First(&i, "name = ?", c.Params("name"))
if i.Name == "" {
return c.Status(fiber.StatusNotFound).JSON(&fiber.Map{
"message": "Not found",
})
}
db.Model(&i).Update("visits", i.Visits+1)
return c.JSON(i)
})
log.Println("Listening...")
log.Fatal(app.Listen(":3000"))
}