feat(ECR-011): 昵称、首页贴士与档案 Self 唯一/合盘交叉校验
每账号仅一条 self(migration 40902);合盘只选 TA;账号昵称可改;首页穿衣/颜色/养生贴士。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -23,6 +23,7 @@ func (h *AuthHandler) Register(api *gin.RouterGroup) {
|
||||
g.POST("/login", h.Login)
|
||||
g.POST("/logout", h.Logout)
|
||||
g.GET("/me", h.Me)
|
||||
g.PATCH("/me", h.PatchMe)
|
||||
}
|
||||
|
||||
type authBody struct {
|
||||
@@ -97,6 +98,30 @@ func (h *AuthHandler) Me(c *gin.Context) {
|
||||
response.OK(c, me)
|
||||
}
|
||||
|
||||
type patchMeBody struct {
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
|
||||
// PatchMe handles PATCH /auth/me (update nickname).
|
||||
func (h *AuthHandler) PatchMe(c *gin.Context) {
|
||||
userID, ok := middleware.UserIDFromContext(c)
|
||||
if !ok {
|
||||
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
||||
return
|
||||
}
|
||||
var body patchMeBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
||||
return
|
||||
}
|
||||
me, err := h.Svc.UpdateNickname(c.Request.Context(), userID, body.Nickname)
|
||||
if err != nil {
|
||||
response.Fail(c, http.StatusBadRequest, 40113, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, me)
|
||||
}
|
||||
|
||||
func bearerToken(c *gin.Context) string {
|
||||
h := c.GetHeader("Authorization")
|
||||
if strings.HasPrefix(strings.ToLower(h), "bearer ") {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
||||
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
|
||||
"github.com/yuxingu/digital-psychology/apps/api/internal/service/home"
|
||||
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
||||
@@ -19,6 +20,7 @@ type HomeHandler struct {
|
||||
func (h *HomeHandler) Register(api *gin.RouterGroup) {
|
||||
g := api.Group("/home")
|
||||
g.GET("/tools", h.Tools)
|
||||
g.GET("/daily-tips", h.DailyTips)
|
||||
}
|
||||
|
||||
func (h *HomeHandler) Tools(c *gin.Context) {
|
||||
@@ -32,3 +34,13 @@ func (h *HomeHandler) Tools(c *gin.Context) {
|
||||
}
|
||||
response.OK(c, gin.H{"items": items})
|
||||
}
|
||||
|
||||
func (h *HomeHandler) DailyTips(c *gin.Context) {
|
||||
uid, _ := middleware.UserIDFromContext(c)
|
||||
tips, err := h.Svc.DailyTips(c.Request.Context(), uid)
|
||||
if err != nil {
|
||||
response.Fail(c, http.StatusInternalServerError, 50031, "daily tips failed")
|
||||
return
|
||||
}
|
||||
response.OK(c, tips)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -56,6 +57,10 @@ func (h *ProfileHandler) Create(c *gin.Context) {
|
||||
RelationType: req.RelationType, BirthTime: req.BirthTime, BirthPlace: req.BirthPlace,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, profile.ErrSelfExists) {
|
||||
response.Fail(c, http.StatusConflict, 40902, err.Error())
|
||||
return
|
||||
}
|
||||
response.Fail(c, http.StatusBadRequest, 30001, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user