59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"health-go/config"
|
|
"health-go/models"
|
|
"net/http"
|
|
)
|
|
|
|
func FetchHospitalList(c *gin.Context) {
|
|
var hosList []models.Hospital
|
|
result := config.DB.Table("hospital").Find(&hosList)
|
|
if result.Error != nil {
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"code": 500,
|
|
"msg": "内部服务器错误",
|
|
"data": nil,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "ok",
|
|
"data": hosList,
|
|
})
|
|
}
|
|
|
|
func AddHospital(c *gin.Context) {
|
|
var newHospital models.Hospital
|
|
if err := c.ShouldBindJSON(&newHospital); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "无效的请求参数",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
result := config.DB.Create(newHospital)
|
|
fmt.Println(newHospital)
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"code": 500,
|
|
"msg": "内部服务器错误",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "添加医院成功",
|
|
"data": "",
|
|
})
|
|
}
|