feat(hospital): add delete & modify apis for hospital
This commit is contained in:
		| @ -15,7 +15,7 @@ func FetchHospitalList(c *gin.Context) { | |||||||
| 		if result.Error != nil { | 		if result.Error != nil { | ||||||
| 			c.JSON(http.StatusInternalServerError, gin.H{ | 			c.JSON(http.StatusInternalServerError, gin.H{ | ||||||
| 				"code": 500, | 				"code": 500, | ||||||
| 				"msg":  "内部服务器错误", | 				"msg":  "获取医院列表失败", | ||||||
| 				"data": nil, | 				"data": nil, | ||||||
| 			}) | 			}) | ||||||
| 		} | 		} | ||||||
| @ -24,7 +24,7 @@ func FetchHospitalList(c *gin.Context) { | |||||||
|  |  | ||||||
| 	c.JSON(http.StatusOK, gin.H{ | 	c.JSON(http.StatusOK, gin.H{ | ||||||
| 		"code": 200, | 		"code": 200, | ||||||
| 		"msg":  "ok", | 		"msg":  "获取医院列表成功", | ||||||
| 		"data": hosList, | 		"data": hosList, | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| @ -44,7 +44,7 @@ func AddHospital(c *gin.Context) { | |||||||
| 	if result.Error != nil { | 	if result.Error != nil { | ||||||
| 		c.JSON(http.StatusInternalServerError, gin.H{ | 		c.JSON(http.StatusInternalServerError, gin.H{ | ||||||
| 			"code": 500, | 			"code": 500, | ||||||
| 			"msg":  "内部服务器错误", | 			"msg":  "添加医院失败", | ||||||
| 			"data": nil, | 			"data": nil, | ||||||
| 		}) | 		}) | ||||||
| 		return | 		return | ||||||
| @ -53,6 +53,67 @@ func AddHospital(c *gin.Context) { | |||||||
| 	c.JSON(http.StatusOK, gin.H{ | 	c.JSON(http.StatusOK, gin.H{ | ||||||
| 		"code": 200, | 		"code": 200, | ||||||
| 		"msg":  "添加医院成功", | 		"msg":  "添加医院成功", | ||||||
| 		"data": "", | 		"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, | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,9 +1,9 @@ | |||||||
| package models | package models | ||||||
|  |  | ||||||
| type Hospital struct { | type Hospital struct { | ||||||
| 	HospitalID   int    `gorm:"column:hospital_id" json:"hospital_id"` | 	HospitalID   int    `gorm:"column:hospital_id" json:"hospital_id" binding:"required"` | ||||||
| 	HospitalName string `gorm:"column:hospital_name" json:"hospital_name"` | 	HospitalName string `gorm:"column:hospital_name" json:"hospital_name" binding:"required"` | ||||||
| 	Address      string `gorm:"column:address" json:"address"` | 	Address      string `gorm:"column:address" json:"address" binding:"required"` | ||||||
| } | } | ||||||
|  |  | ||||||
| func (Hospital) TableName() string { | func (Hospital) TableName() string { | ||||||
|  | |||||||
| @ -17,6 +17,8 @@ func SetupRouter() *gin.Engine { | |||||||
| 	r.GET("/fetchAllRec", handlers.FetchAllRecords) | 	r.GET("/fetchAllRec", handlers.FetchAllRecords) | ||||||
|  |  | ||||||
| 	// hospital | 	// hospital | ||||||
| 	r.POST("/api/addHos", handlers.AddHospital) | 	r.POST("/api/addHos", handlers.AddHospital)         // add hospital | ||||||
|  | 	r.POST("/api/modifyHos", handlers.ModifyHospital)   // modify hospital | ||||||
|  | 	r.DELETE("/api/deleteHos", handlers.DeleteHospital) // delete hospital | ||||||
| 	return r | 	return r | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user