health-go/handler/HospitalHandler.go

109 lines
2.1 KiB
Go
Raw Normal View History

2024-11-14 01:15:55 +08:00
package handler
2024-10-17 23:52:10 +08:00
import (
"fmt"
"github.com/gin-gonic/gin"
"health-go/config"
2024-11-14 01:15:55 +08:00
"health-go/model"
"health-go/service/hospital"
"health-go/util"
2024-10-17 23:52:10 +08:00
"net/http"
)
func FetchHospitalList(c *gin.Context) {
2024-11-14 01:15:55 +08:00
var hosList []model.Hospital
2024-10-17 23:52:10 +08:00
result := config.DB.Table("hospital").Find(&hosList)
if result.Error != nil {
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "获取医院列表失败",
2024-10-17 23:52:10 +08:00
"data": nil,
})
}
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "获取医院列表成功",
2024-10-17 23:52:10 +08:00
"data": hosList,
})
}
func AddHospital(c *gin.Context) {
2024-11-14 01:15:55 +08:00
var newHospital model.Hospital
2024-10-17 23:52:10 +08:00
if err := c.ShouldBindJSON(&newHospital); err != nil {
2024-11-14 01:15:55 +08:00
util.ReturnInvalid(c)
2024-10-17 23:52:10 +08:00
return
}
2024-11-14 01:15:55 +08:00
util.ReturnJson(c, http.StatusOK, 400, "invalid params", nil)
2024-10-17 23:52:10 +08:00
result := config.DB.Create(newHospital)
fmt.Println(newHospital)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "添加医院失败",
2024-10-17 23:52:10 +08:00
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "添加医院成功",
"data": nil,
})
}
func ModifyHospital(c *gin.Context) {
2024-11-14 01:15:55 +08:00
var hos model.Hospital
if err := c.ShouldBindJSON(&hos); err != nil {
util.ReturnInvalid(c)
return
}
2024-11-14 01:15:55 +08:00
if ok, err := hospital.Modify(hos); ok {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "修改医院信息成功",
"data": nil,
})
2024-11-14 01:15:55 +08:00
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "修改医院信息失败",
2024-11-14 01:15:55 +08:00
"data": err,
})
return
}
}
2024-11-14 01:15:55 +08:00
// DeleteHospital
// @Accept json
// @Produce json
// @Param hos_id query int true "医院id" "maxlength(100)"
func DeleteHospital(c *gin.Context) {
2024-11-14 01:15:55 +08:00
var hospital model.Hospital
if err := c.ShouldBindJSON(&hospital); err != nil {
2024-11-14 01:15:55 +08:00
util.ReturnInvalid(c)
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,
2024-10-17 23:52:10 +08:00
})
}