package api import ( "github.com/gin-gonic/gin" . "main.go/type" db "main.go/database" ) // GetAllNotices 获取所有的公告 func GetAllNotices(c *gin.Context) { var notices []Notice if err := getAllNotices(¬ices); err != nil { c.JSON(200, gin.H{"code": 400, "msg": "Get all notices failure!"}) return } c.JSON(200, gin.H{"code": 200, "data": notices}) } func getAllNotices(notices *[]Notice) error { Link() DB := db.DBsnctf command := "SELECT id, title, content, created_at FROM notice ORDER BY created_at ASC;" rows, err := DB.Raw(command).Rows() if err != nil { return err } defer rows.Close() for rows.Next() { var b Notice err = rows.Scan(&b.ID, &b.Title, &b.Content, &b.CreatedAt) if err != nil { return err } *notices = append(*notices, b) } return rows.Err() }