Enhance smoking tracking API with new features and improvements

- Added a new API endpoint `GET /api/v1/smoke/home` to consolidate core modules for the home dashboard, reducing the need for multiple requests.
- Updated the `smoke` routes to include the new home endpoint and improved user profile management with the addition of a `quit_date` field.
- Enhanced the algorithm for calculating daily targets and next smoke suggestions, ensuring accurate future time handling and user-specific recommendations.
- Improved API documentation to reflect new endpoints, response formats, and detailed field descriptions for better clarity and usability.
- Refactored user authentication handling in various handlers to streamline the process and ensure consistent error responses.
This commit is contained in:
nepiedg
2026-01-29 17:16:35 +00:00
parent 3154365ab2
commit 9200600b1c
21 changed files with 703 additions and 178 deletions
+23 -11
View File
@@ -4,6 +4,8 @@ import (
"errors"
"io"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
@@ -22,14 +24,12 @@ type upsertSmokeProfileRequest struct {
WakeUpTime *string `json:"wake_up_time"`
SleepTime *string `json:"sleep_time"`
QuitDate *string `json:"quit_date"`
}
func (h *SmokeHandler) GetProfile(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
user := middleware.MustCurrentUser(c)
view, err := h.smokeProfileService.GetView(c.Request.Context(), int(user.ID))
if err != nil {
@@ -45,11 +45,7 @@ func (h *SmokeHandler) GetProfile(c *gin.Context) {
}
func (h *SmokeHandler) UpsertProfile(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
user := middleware.MustCurrentUser(c)
var req upsertSmokeProfileRequest
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
@@ -76,6 +72,21 @@ func (h *SmokeHandler) UpsertProfile(c *gin.Context) {
}
}
quitDateProvided := false
var quitDate *time.Time
if req.QuitDate != nil {
quitDateProvided = true
value := strings.TrimSpace(*req.QuitDate)
if value != "" {
parsed, err := time.ParseInLocation(dateLayout, value, time.Local)
if err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "quit_date 格式错误,应为 YYYY-MM-DD"))
return
}
quitDate = &parsed
}
}
view, err := h.smokeProfileService.Upsert(c.Request.Context(), int(user.ID), smokeservice.UpsertSmokeProfileRequest{
BaselineCigsPerDay: req.BaselineCigsPerDay,
SmokingYears: req.SmokingYears,
@@ -84,6 +95,8 @@ func (h *SmokeHandler) UpsertProfile(c *gin.Context) {
QuitMotivations: req.QuitMotivations,
WakeUpTime: req.WakeUpTime,
SleepTime: req.SleepTime,
QuitDateProvided: quitDateProvided,
QuitDate: quitDate,
})
if err != nil {
if errors.Is(err, smokeservice.ErrSmokeProfileInvalidTime) {
@@ -96,4 +109,3 @@ func (h *SmokeHandler) UpsertProfile(c *gin.Context) {
c.JSON(http.StatusOK, model.Success(view))
}