d88c1ae647
手动合并 tsk_x7wrlA87orsY(设备管理 + 订阅校验中间件)到 main:
冲突解决:
- server/internal/apierr/apierr.go:保留 tsk_GXDoc3Cs07Rn 版本(New/StatusFor/
Middleware/ErrConflict/改善文档),并入 tsk_x7wrlA87orsY 新增的 ErrAccountBanned
及对应 StatusFor case(→ 403)。
新增文件(来自 tsk_x7wrlA87orsY):
- server/internal/devices/doc.go package 文档(替换占位 stub)
- server/internal/devices/context.go CtxKeyUserID / Plan / WithPlan / PlanFromCtx
- server/internal/devices/handler.go GET /v1/me/devices · DELETE /v1/me/devices/{id}
- server/internal/devices/middleware.go SubscriptionMiddleware · CheckDeviceQuota · RequirePaidTier
- server/internal/devices/service.go RegisterIfAbsent / DeleteDevice / ResolvePlan + 纯函数 resolveEffectivePlan
- server/internal/devices/store.go MySQL 数据访问层
- server/internal/devices/service_test.go 15 个单测(全通过)
- server/internal/devices/devices_integration_test.go testcontainers 集成测试
OpenAPI 更新(来自 tsk_x7wrlA87orsY):
- server/api/openapi.yaml:SubscriptionInfo.source 枚举补 free
- design/server/openapi.yaml:SubscriptionInfo.source 枚举补 admin, free
测试:go build ./... ✓;go test ./internal/apierr/... ✓(8 tests);
go test ./internal/devices/... ✓(15 tests)。
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package devices
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/wangjia/pangolin/server/internal/apierr"
|
|
)
|
|
|
|
// Handler serves the account device endpoints. It assumes the JWT auth
|
|
// middleware (module #2) has set CtxKeyUserID on the request context.
|
|
type Handler struct {
|
|
svc *Service
|
|
}
|
|
|
|
// NewHandler creates a Handler backed by svc.
|
|
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
|
|
|
|
// RegisterRoutes mounts the device routes on r under the caller's chosen prefix
|
|
// (the API mounts these beneath /v1/me):
|
|
//
|
|
// GET /devices
|
|
// DELETE /devices/{id}
|
|
func (h *Handler) RegisterRoutes(r chi.Router) {
|
|
r.Get("/devices", h.ListDevices)
|
|
r.Delete("/devices/{id}", h.DeleteDevice)
|
|
}
|
|
|
|
type listDevicesResponse struct {
|
|
Devices []Device `json:"devices"`
|
|
}
|
|
|
|
// ListDevices handles GET /v1/me/devices.
|
|
func (h *Handler) ListDevices(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := UserIDFromContext(r.Context())
|
|
if !ok {
|
|
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
devices, apiErr := h.svc.ListDevices(r.Context(), userID)
|
|
if apiErr != nil {
|
|
apierr.WriteJSON(w, StatusForError(apiErr), apiErr)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(http.StatusOK)
|
|
_ = json.NewEncoder(w).Encode(listDevicesResponse{Devices: devices})
|
|
}
|
|
|
|
// DeleteDevice handles DELETE /v1/me/devices/{id}.
|
|
func (h *Handler) DeleteDevice(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := UserIDFromContext(r.Context())
|
|
if !ok {
|
|
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
deviceUUID := chi.URLParam(r, "id")
|
|
if apiErr := h.svc.DeleteDevice(r.Context(), userID, deviceUUID); apiErr != nil {
|
|
apierr.WriteJSON(w, StatusForError(apiErr), apiErr)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|