healthapp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

301 lines
9.1 KiB

package handler
import (
"strconv"
"health-ai/internal/api/middleware"
"health-ai/internal/service"
"health-ai/pkg/response"
"github.com/gin-gonic/gin"
)
type HealthHandler struct {
healthService *service.HealthService
}
func NewHealthHandler() *HealthHandler {
return &HealthHandler{
healthService: service.NewHealthService(),
}
}
// GetHealthProfile 获取完整健康档案
// @Summary 获取完整健康档案
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=service.HealthProfileResponse}
// @Router /api/user/health-profile [get]
func (h *HealthHandler) GetHealthProfile(c *gin.Context) {
userID := middleware.GetUserID(c)
profile, err := h.healthService.GetHealthProfile(userID)
if err != nil {
response.Error(c, 500, err.Error())
return
}
response.Success(c, profile)
}
// GetBasicProfile 获取基础档案
// @Summary 获取基础档案
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=model.HealthProfile}
// @Router /api/user/basic-profile [get]
func (h *HealthHandler) GetBasicProfile(c *gin.Context) {
userID := middleware.GetUserID(c)
profile, err := h.healthService.GetBasicProfile(userID)
if err != nil {
response.Error(c, 404, err.Error())
return
}
response.Success(c, profile)
}
// GetLifestyle 获取生活习惯
// @Summary 获取生活习惯
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=model.LifestyleInfo}
// @Router /api/user/lifestyle [get]
func (h *HealthHandler) GetLifestyle(c *gin.Context) {
userID := middleware.GetUserID(c)
lifestyle, err := h.healthService.GetLifestyle(userID)
if err != nil {
response.Error(c, 404, err.Error())
return
}
response.Success(c, lifestyle)
}
// GetMedicalHistory 获取病史列表
// @Summary 获取病史列表
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=[]model.MedicalHistory}
// @Router /api/user/medical-history [get]
func (h *HealthHandler) GetMedicalHistory(c *gin.Context) {
userID := middleware.GetUserID(c)
history, err := h.healthService.GetMedicalHistory(userID)
if err != nil {
response.Error(c, 404, err.Error())
return
}
response.Success(c, history)
}
// GetFamilyHistory 获取家族病史列表
// @Summary 获取家族病史列表
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=[]model.FamilyHistory}
// @Router /api/user/family-history [get]
func (h *HealthHandler) GetFamilyHistory(c *gin.Context) {
userID := middleware.GetUserID(c)
history, err := h.healthService.GetFamilyHistory(userID)
if err != nil {
response.Error(c, 404, err.Error())
return
}
response.Success(c, history)
}
// GetAllergyRecords 获取过敏记录列表
// @Summary 获取过敏记录列表
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} response.Response{data=[]model.AllergyRecord}
// @Router /api/user/allergy-records [get]
func (h *HealthHandler) GetAllergyRecords(c *gin.Context) {
userID := middleware.GetUserID(c)
records, err := h.healthService.GetAllergyRecords(userID)
if err != nil {
response.Error(c, 404, err.Error())
return
}
response.Success(c, records)
}
// DeleteMedicalHistory 删除病史记录
// @Summary 删除病史记录
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param id path int true "记录ID"
// @Success 200 {object} response.Response
// @Router /api/user/medical-history/{id} [delete]
func (h *HealthHandler) DeleteMedicalHistory(c *gin.Context) {
userID := middleware.GetUserID(c)
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
response.BadRequest(c, "无效的ID")
return
}
if err := h.healthService.DeleteMedicalHistory(userID, uint(id)); err != nil {
response.Error(c, 500, err.Error())
return
}
response.SuccessWithMessage(c, "删除成功", nil)
}
// DeleteFamilyHistory 删除家族病史记录
// @Summary 删除家族病史记录
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param id path int true "记录ID"
// @Success 200 {object} response.Response
// @Router /api/user/family-history/{id} [delete]
func (h *HealthHandler) DeleteFamilyHistory(c *gin.Context) {
userID := middleware.GetUserID(c)
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
response.BadRequest(c, "无效的ID")
return
}
if err := h.healthService.DeleteFamilyHistory(userID, uint(id)); err != nil {
response.Error(c, 500, err.Error())
return
}
response.SuccessWithMessage(c, "删除成功", nil)
}
// DeleteAllergyRecord 删除过敏记录
// @Summary 删除过敏记录
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param id path int true "记录ID"
// @Success 200 {object} response.Response
// @Router /api/user/allergy-records/{id} [delete]
func (h *HealthHandler) DeleteAllergyRecord(c *gin.Context) {
userID := middleware.GetUserID(c)
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
response.BadRequest(c, "无效的ID")
return
}
if err := h.healthService.DeleteAllergyRecord(userID, uint(id)); err != nil {
response.Error(c, 500, err.Error())
return
}
response.SuccessWithMessage(c, "删除成功", nil)
}
// UpdateHealthProfileRequest 更新健康档案请求
type UpdateHealthProfileRequest struct {
Name string `json:"name"`
BirthDate string `json:"birth_date"`
Gender string `json:"gender"`
Height float64 `json:"height"`
Weight float64 `json:"weight"`
BloodType string `json:"blood_type"`
Occupation string `json:"occupation"`
MaritalStatus string `json:"marital_status"`
Region string `json:"region"`
}
// UpdateHealthProfile 更新健康档案
// @Summary 更新健康档案
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param request body UpdateHealthProfileRequest true "健康档案信息"
// @Success 200 {object} response.Response
// @Router /api/user/health-profile [put]
func (h *HealthHandler) UpdateHealthProfile(c *gin.Context) {
userID := middleware.GetUserID(c)
var req UpdateHealthProfileRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误: "+err.Error())
return
}
profile, err := h.healthService.UpdateHealthProfile(userID, &service.UpdateHealthProfileInput{
Name: req.Name,
BirthDate: req.BirthDate,
Gender: req.Gender,
Height: req.Height,
Weight: req.Weight,
BloodType: req.BloodType,
Occupation: req.Occupation,
MaritalStatus: req.MaritalStatus,
Region: req.Region,
})
if err != nil {
response.Error(c, 500, err.Error())
return
}
response.Success(c, profile)
}
// UpdateLifestyleRequest 更新生活习惯请求
type UpdateLifestyleRequest struct {
SleepTime string `json:"sleep_time"`
WakeTime string `json:"wake_time"`
SleepQuality string `json:"sleep_quality"`
MealRegularity string `json:"meal_regularity"`
DietPreference string `json:"diet_preference"`
DailyWaterML int `json:"daily_water_ml"`
ExerciseFrequency string `json:"exercise_frequency"`
ExerciseType string `json:"exercise_type"`
ExerciseDurationMin int `json:"exercise_duration_min"`
IsSmoker *bool `json:"is_smoker"`
AlcoholFrequency string `json:"alcohol_frequency"`
}
// UpdateLifestyle 更新生活习惯
// @Summary 更新生活习惯
// @Tags 健康档案
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param request body UpdateLifestyleRequest true "生活习惯信息"
// @Success 200 {object} response.Response
// @Router /api/user/lifestyle [put]
func (h *HealthHandler) UpdateLifestyle(c *gin.Context) {
userID := middleware.GetUserID(c)
var req UpdateLifestyleRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误: "+err.Error())
return
}
lifestyle, err := h.healthService.UpdateLifestyle(userID, &service.UpdateLifestyleInput{
SleepTime: req.SleepTime,
WakeTime: req.WakeTime,
SleepQuality: req.SleepQuality,
MealRegularity: req.MealRegularity,
DietPreference: req.DietPreference,
DailyWaterML: req.DailyWaterML,
ExerciseFrequency: req.ExerciseFrequency,
ExerciseType: req.ExerciseType,
ExerciseDurationMin: req.ExerciseDurationMin,
IsSmoker: req.IsSmoker,
AlcoholFrequency: req.AlcoholFrequency,
})
if err != nil {
response.Error(c, 500, err.Error())
return
}
response.Success(c, lifestyle)
}