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