refactor: set http return code to static
This commit is contained in:
parent
70d7229dd8
commit
c64d80951c
@ -1,34 +1,19 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"health-go/config"
|
||||
"health-go/model"
|
||||
"health-go/service/hospital"
|
||||
"health-go/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func FetchHospitalList(c *gin.Context) {
|
||||
var hosList []model.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,
|
||||
})
|
||||
}
|
||||
if res, err := hospital.FetchAll(); err != nil {
|
||||
util.ReturnJson(c, 500, "获取医院列表失败", err)
|
||||
return
|
||||
} else {
|
||||
util.ReturnJson(c, 200, "获取医院列表成功", res)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "获取医院列表成功",
|
||||
"data": hosList,
|
||||
})
|
||||
}
|
||||
|
||||
func AddHospital(c *gin.Context) {
|
||||
@ -37,24 +22,12 @@ func AddHospital(c *gin.Context) {
|
||||
util.ReturnInvalid(c)
|
||||
return
|
||||
}
|
||||
|
||||
util.ReturnJson(c, http.StatusOK, 400, "invalid params", nil)
|
||||
result := config.DB.Create(newHospital)
|
||||
fmt.Println(newHospital)
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"msg": "添加医院失败",
|
||||
"data": nil,
|
||||
})
|
||||
if ok, err := hospital.Add(newHospital); ok {
|
||||
util.ReturnJson(c, 200, "添加医院成功", nil)
|
||||
} else {
|
||||
util.ReturnJson(c, 500, "添加医院失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "添加医院成功",
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
||||
func ModifyHospital(c *gin.Context) {
|
||||
@ -64,20 +37,11 @@ func ModifyHospital(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if ok, err := hospital.Modify(hos); ok {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "修改医院信息成功",
|
||||
"data": nil,
|
||||
})
|
||||
util.ReturnJson(c, 200, "修改医院信息成功", nil)
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"msg": "修改医院信息失败",
|
||||
"data": err,
|
||||
})
|
||||
util.ReturnJson(c, 500, "修改医院信息失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// DeleteHospital
|
||||
@ -85,24 +49,15 @@ func ModifyHospital(c *gin.Context) {
|
||||
// @Produce json
|
||||
// @Param hos_id query int true "医院id" "maxlength(100)"
|
||||
func DeleteHospital(c *gin.Context) {
|
||||
var hospital model.Hospital
|
||||
if err := c.ShouldBindJSON(&hospital); err != nil {
|
||||
var hos model.Hospital
|
||||
if err := c.ShouldBindJSON(&hos); err != nil {
|
||||
util.ReturnInvalid(c)
|
||||
return
|
||||
}
|
||||
result := config.DB.Delete(&hospital)
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"msg": "删除指定医院失败",
|
||||
"data": nil,
|
||||
})
|
||||
if ok, err := hospital.Delete(hos.HospitalID); ok {
|
||||
util.ReturnJson(c, 200, "删除指定医院成功", nil)
|
||||
} else {
|
||||
util.ReturnJson(c, 500, "删除指定医院失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "删除指定医院成功",
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"health-go/config"
|
||||
"health-go/model"
|
||||
"health-go/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -14,11 +15,7 @@ func FirstUser(c *gin.Context) {
|
||||
result := config.DB.Where("user_name = ?", "Biid").First(&user)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"code": 404,
|
||||
"msg": "用户未找到",
|
||||
"data": nil,
|
||||
})
|
||||
util.ReturnJson(c, 404, "用户未找到", nil)
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
|
45
service/hospital/hospital.go
Normal file
45
service/hospital/hospital.go
Normal file
@ -0,0 +1,45 @@
|
||||
package hospital
|
||||
|
||||
import (
|
||||
"health-go/config"
|
||||
"health-go/model"
|
||||
)
|
||||
|
||||
func Modify(hospital model.Hospital) (bool, error) {
|
||||
result := config.DB.
|
||||
Table("hospital").
|
||||
Where("hospital_id = ?", hospital.HospitalID).
|
||||
Updates(map[string]interface{}{
|
||||
"hospital_name": hospital.HospitalName,
|
||||
"address": hospital.Address,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func FetchAll() ([]model.Hospital, error) {
|
||||
var hospitals []model.Hospital
|
||||
result := config.DB.Table("hospital").Find(&hospitals)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return hospitals, nil
|
||||
}
|
||||
|
||||
func Add(hospital model.Hospital) (bool, error) {
|
||||
result := config.DB.Table("hospital").Create(&hospital)
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func Delete(hospitalID int) (bool, error) {
|
||||
result := config.DB.Table("hospital").Where("hospital_id = ?", hospitalID).Delete(&model.Hospital{})
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return true, nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package hospital
|
||||
|
||||
import (
|
||||
"health-go/config"
|
||||
"health-go/model"
|
||||
)
|
||||
|
||||
func Modify(hospital model.Hospital) (bool, error) {
|
||||
result := config.DB.
|
||||
Table("hospital").
|
||||
Where("hospital_id = ?", hospital.HospitalID).
|
||||
Updates(map[string]interface{}{
|
||||
"hospital_name": hospital.HospitalName,
|
||||
"address": hospital.Address,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return true, nil
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package util
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ReturnJson(c *gin.Context, httpCode int, code int, msg string, data interface{}) {
|
||||
c.JSON(httpCode, gin.H{
|
||||
func ReturnJson(c *gin.Context, code int, msg string, data interface{}) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": msg,
|
||||
"data": data,
|
||||
|
Loading…
Reference in New Issue
Block a user