refactor: adjust project structure

This commit is contained in:
2024-11-14 01:15:55 +08:00
parent c219d81b89
commit 58232c403e
19 changed files with 316 additions and 128 deletions

108
handler/HospitalHandler.go Normal file
View File

@ -0,0 +1,108 @@
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,
})
}
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "获取医院列表成功",
"data": hosList,
})
}
func AddHospital(c *gin.Context) {
var newHospital model.Hospital
if err := c.ShouldBindJSON(&newHospital); err != nil {
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,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "添加医院成功",
"data": nil,
})
}
func ModifyHospital(c *gin.Context) {
var hos model.Hospital
if err := c.ShouldBindJSON(&hos); err != nil {
util.ReturnInvalid(c)
return
}
if ok, err := hospital.Modify(hos); ok {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "修改医院信息成功",
"data": nil,
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "修改医院信息失败",
"data": err,
})
return
}
}
// DeleteHospital
// @Accept json
// @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 {
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,
})
}

195
handler/RecordHandler.go Normal file
View File

@ -0,0 +1,195 @@
package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"health-go/config"
"health-go/model"
"health-go/util"
"net/http"
"time"
)
// FetchAllRecords 每个用户全部化验记录
func FetchAllRecords(c *gin.Context) {
var records []model.Check
result := config.DB.Where("user_id = ?", c.Query("user_id")).Find(&records)
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": "ok",
"data": records,
})
}
func InsertRecord(c *gin.Context) {
var newRecord model.UserInput
if err := c.ShouldBindJSON(&newRecord); err != nil {
fmt.Println(newRecord.Date)
util.ReturnInvalid(c)
return
}
date, _ := convertDate(newRecord.Date)
fmt.Println(date)
//ok, checkID := prepareInsert(1, date, newRecord.Hospital)
//
//if ok {
// var newItems []model.CheckItem
//for fieldName, itemID := range checkItemMap {
// switch fieldName {
// case "ca125Value":
// if newRecord.Ca125 != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Ca125,
// })
// }
// case "ca199Value":
// if newRecord.Ca199 != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Ca199,
// })
// }
// case "ceaValue":
// if newRecord.Cea != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Cea,
// })
// }
// case "ca153Value":
// if newRecord.Ca153 != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Ca153,
// })
// }
// case "ca724Value":
// if newRecord.Ca724 != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Ca724,
// })
// }
// case "he4Value":
// if newRecord.He4 != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.He4,
// })
// }
// case "rbcValue":
// if newRecord.Rbc != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Rbc,
// })
// }
// case "hbValue":
// if newRecord.Hb != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Hb,
// })
// }
// case "wbcValue":
// if newRecord.Wbc != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Wbc,
// })
// }
// case "pltValue":
// if newRecord.Plt != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Plt,
// })
// }
// case "neutValue":
// if newRecord.Neut != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Neut,
// })
// }
// case "weightValue":
// if newRecord.Weight != -1 {
// newItems = append(newItems, model.CheckItem{
// CheckID: checkID,
// ItemID: itemID,
// ItemValue: newRecord.Weight,
// })
// }
// }
//}
//result := config.DB.Table("check_item")
//if result.Error != nil{
// c.JSON(http.StatusOK, gin.H{
// "code":500,
// "msg": "插入失败",
// "data": nil,
// })
//}
// c.JSON(http.StatusOK, gin.H{
// "code": 200,
// "msg": "添加记录成功",
// "data": nil,
// })
//} else {
// c.JSON(http.StatusOK, gin.H{
// "code": 200,
// "msg": "存在相同日期记录, 插入失败",
// "data": nil,
// })
//}
}
func prepareInsert(userID int, date time.Time, hospital int) (pre bool, id int) {
var aRec model.Check
result := config.DB.Model("checks").
Where(
"check_date = ? AND user_id = ?",
date.Format("2006-01-02"),
userID).Find(&aRec)
if result.RowsAffected == 0 {
var newRec model.Check
newRec.UserID = 1
newRec.CheckDate = date
newRec.Hospital = hospital
newRec.InsertTime = time.Now()
newRec.UpdateTime = time.Now()
re := config.DB.Table("checks").Create(&newRec)
fmt.Println(re.RowsAffected)
return true, aRec.CheckID
}
return false, aRec.CheckID
}
func convertDate(dateStr string) (time.Time, error) {
return time.Parse("2006-01-02", dateStr)
}

39
handler/UserHandler.go Normal file
View File

@ -0,0 +1,39 @@
package handler
import (
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"health-go/config"
"health-go/model"
"net/http"
)
func FirstUser(c *gin.Context) {
var user model.User
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,
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "内部服务器错误",
"data": nil,
})
}
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "ok",
"data": map[string]interface{}{
"first_user": user,
},
})
}

13
handler/defaultHandler.go Normal file
View File

@ -0,0 +1,13 @@
package handler
import (
"github.com/gin-gonic/gin"
)
func TestPage(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"msg": "test",
"data": map[string]string{"test": "test"},
})
}