Files
wx_service/internal/smoke/handler/smoke_profile_handler.go
T
nepiedg dc54c4e934 Add user profile management for smoking data
- Introduced new API endpoints `GET /api/v1/smoke/profile` and `PUT /api/v1/smoke/profile` for retrieving and updating user smoking profiles.
- Added a new database table `fa_smoke_user_profile` to store user-specific smoking data, including daily smoking habits and motivations.
- Updated the smoke handler and service to integrate user profile data into AI advice generation.
- Enhanced documentation to reflect the new user profile features and their usage.
2026-01-20 02:37:20 +00:00

100 lines
3.1 KiB
Go

package handler
import (
"errors"
"io"
"net/http"
"github.com/gin-gonic/gin"
"wx_service/internal/middleware"
"wx_service/internal/model"
smokeservice "wx_service/internal/smoke/service"
)
type upsertSmokeProfileRequest struct {
BaselineCigsPerDay *int `json:"baseline_cigs_per_day"`
SmokingYears *float64 `json:"smoking_years"`
PackPriceCent *int `json:"pack_price_cent"`
SmokeMotivations *[]string `json:"smoke_motivations"`
QuitMotivations *[]string `json:"quit_motivations"`
WakeUpTime *string `json:"wake_up_time"`
SleepTime *string `json:"sleep_time"`
}
func (h *SmokeHandler) GetProfile(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
view, err := h.smokeProfileService.GetView(c.Request.Context(), int(user.ID))
if err != nil {
if errors.Is(err, smokeservice.ErrSmokeProfileInvalidTime) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "作息时间格式错误,应为 HH:MM"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取基础信息失败,请稍后重试"))
return
}
c.JSON(http.StatusOK, model.Success(view))
}
func (h *SmokeHandler) UpsertProfile(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
var req upsertSmokeProfileRequest
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
if req.BaselineCigsPerDay != nil {
if *req.BaselineCigsPerDay < 0 || *req.BaselineCigsPerDay > 300 {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "baseline_cigs_per_day 应在 0~300"))
return
}
}
if req.SmokingYears != nil {
if *req.SmokingYears < 0 || *req.SmokingYears > 80 {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "smoking_years 应在 0~80"))
return
}
}
if req.PackPriceCent != nil {
if *req.PackPriceCent < 0 || *req.PackPriceCent > 1000000 {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "pack_price_cent 应在 0~1000000"))
return
}
}
view, err := h.smokeProfileService.Upsert(c.Request.Context(), int(user.ID), smokeservice.UpsertSmokeProfileRequest{
BaselineCigsPerDay: req.BaselineCigsPerDay,
SmokingYears: req.SmokingYears,
PackPriceCent: req.PackPriceCent,
SmokeMotivations: req.SmokeMotivations,
QuitMotivations: req.QuitMotivations,
WakeUpTime: req.WakeUpTime,
SleepTime: req.SleepTime,
})
if err != nil {
if errors.Is(err, smokeservice.ErrSmokeProfileInvalidTime) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "作息时间格式错误,应为 HH:MM"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "保存基础信息失败,请稍后重试"))
return
}
c.JSON(http.StatusOK, model.Success(view))
}