feat: add private and profile
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hakemsho/internal/httpx"
|
||||
@@ -158,3 +159,33 @@ func (h *Handler) Me(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
httpx.JSON(w, http.StatusOK, map[string]any{"user": u})
|
||||
}
|
||||
|
||||
type profileReq struct {
|
||||
FirstName string `json:"first_name"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
// UpdateProfile — POST /api/profile (تنظیم نام نمایشی و آواتار، نیازمند JWT)
|
||||
func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
id, ok := UserID(r.Context())
|
||||
if !ok {
|
||||
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
var req profileReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
httpx.Error(w, http.StatusBadRequest, "invalid body")
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(req.FirstName)
|
||||
if len([]rune(name)) < 2 || len([]rune(name)) > 20 {
|
||||
httpx.Error(w, http.StatusUnprocessableEntity, "name must be 2-20 characters")
|
||||
return
|
||||
}
|
||||
if err := h.users.UpdateProfile(r.Context(), id, name, req.Avatar); err != nil {
|
||||
httpx.Error(w, http.StatusInternalServerError, "server error")
|
||||
return
|
||||
}
|
||||
u, _ := h.users.FindByID(r.Context(), id)
|
||||
httpx.JSON(w, http.StatusOK, map[string]any{"user": u})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user