This commit is contained in:
jiayuqi7813
2022-05-21 23:29:23 +08:00
parent b8859af6d8
commit 7cb171bc90
83 changed files with 1687 additions and 0 deletions

39
api/notice.go Normal file
View File

@@ -0,0 +1,39 @@
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(&notices); 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()
}