From bb580ff4886610444e7c066325453fc420b47fba Mon Sep 17 00:00:00 2001
From: Biiddd <1256773838@qq.com>
Date: Sun, 20 Oct 2024 17:16:53 +0800
Subject: [PATCH] feat(hospital): add delete & modify apis for hospital

---
 handlers/HospitalHandler.go | 69 ++++++++++++++++++++++++++++++++++---
 models/hospital.go          |  6 ++--
 routers/router.go           |  4 ++-
 3 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/handlers/HospitalHandler.go b/handlers/HospitalHandler.go
index 9a73a5f..d7f4dc4 100644
--- a/handlers/HospitalHandler.go
+++ b/handlers/HospitalHandler.go
@@ -15,7 +15,7 @@ func FetchHospitalList(c *gin.Context) {
 		if result.Error != nil {
 			c.JSON(http.StatusInternalServerError, gin.H{
 				"code": 500,
-				"msg":  "内部服务器错误",
+				"msg":  "获取医院列表失败",
 				"data": nil,
 			})
 		}
@@ -24,7 +24,7 @@ func FetchHospitalList(c *gin.Context) {
 
 	c.JSON(http.StatusOK, gin.H{
 		"code": 200,
-		"msg":  "ok",
+		"msg":  "获取医院列表成功",
 		"data": hosList,
 	})
 }
@@ -44,7 +44,7 @@ func AddHospital(c *gin.Context) {
 	if result.Error != nil {
 		c.JSON(http.StatusInternalServerError, gin.H{
 			"code": 500,
-			"msg":  "内部服务器错误",
+			"msg":  "添加医院失败",
 			"data": nil,
 		})
 		return
@@ -53,6 +53,67 @@ func AddHospital(c *gin.Context) {
 	c.JSON(http.StatusOK, gin.H{
 		"code": 200,
 		"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,
 	})
 }
diff --git a/models/hospital.go b/models/hospital.go
index 573270c..c97a056 100644
--- a/models/hospital.go
+++ b/models/hospital.go
@@ -1,9 +1,9 @@
 package models
 
 type Hospital struct {
-	HospitalID   int    `gorm:"column:hospital_id" json:"hospital_id"`
-	HospitalName string `gorm:"column:hospital_name" json:"hospital_name"`
-	Address      string `gorm:"column:address" json:"address"`
+	HospitalID   int    `gorm:"column:hospital_id" json:"hospital_id" binding:"required"`
+	HospitalName string `gorm:"column:hospital_name" json:"hospital_name" binding:"required"`
+	Address      string `gorm:"column:address" json:"address" binding:"required"`
 }
 
 func (Hospital) TableName() string {
diff --git a/routers/router.go b/routers/router.go
index 473ec0f..265bb8b 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -17,6 +17,8 @@ func SetupRouter() *gin.Engine {
 	r.GET("/fetchAllRec", handlers.FetchAllRecords)
 
 	// 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
 }