120 lines
2.4 KiB
Go
120 lines
2.4 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": "获取医院列表成功",
|
|
"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": nil,
|
|
})
|
|
}
|
|
|
|
func ModifyHospital(c *gin.Context) {
|
|
var hospital models.Hospital
|
|
if err := c.ShouldBindJSON(&hospital); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "无效的请求参数",
|
|
"data": err,
|
|
})
|
|
return
|
|
}
|
|
result := config.DB.
|
|
Model(&hospital).
|
|
Where("hospital_id = ?", hospital.HospitalID).
|
|
Updates(map[string]interface{}{
|
|
"hospital_name": hospital.HospitalName,
|
|
"address": hospital.Address,
|
|
})
|
|
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"code": 500,
|
|
"msg": "修改医院信息失败",
|
|
"data": result.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "修改医院信息成功",
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
func DeleteHospital(c *gin.Context) {
|
|
var hospital models.Hospital
|
|
if err := c.ShouldBindJSON(&hospital); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "无效的请求参数",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
result := config.DB.Delete(&hospital)
|
|
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": nil,
|
|
})
|
|
}
|