|
|
@@ -27,15 +27,21 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
// Handler holds the admin dependencies.
|
|
|
|
// Handler holds the admin dependencies.
|
|
|
|
type Handler struct {
|
|
|
|
type Handler struct {
|
|
|
|
store *adminstore.Store
|
|
|
|
store *adminstore.Store
|
|
|
|
authn *auth.Authenticator
|
|
|
|
authn *auth.Authenticator
|
|
|
|
basePath string
|
|
|
|
basePath string
|
|
|
|
spa fs.FS
|
|
|
|
spa fs.FS
|
|
|
|
submitLimit *rateLimiter
|
|
|
|
submitLimit *rateLimiter
|
|
|
|
loginLimit *loginLimiter
|
|
|
|
loginLimit *loginLimiter
|
|
|
|
usage *ratelimit.Limiter
|
|
|
|
usage usageLimiter
|
|
|
|
cache cacheBumper
|
|
|
|
backflowRate int
|
|
|
|
clientIP *clientip.Resolver
|
|
|
|
cache cacheBumper
|
|
|
|
|
|
|
|
clientIP *clientip.Resolver
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type usageLimiter interface {
|
|
|
|
|
|
|
|
Allow(context.Context, string, int, time.Duration) ratelimit.Result
|
|
|
|
|
|
|
|
Usage(context.Context, string) ratelimit.UsageStat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type cacheBumper interface {
|
|
|
|
type cacheBumper interface {
|
|
|
@@ -46,13 +52,14 @@ type cacheBumper interface {
|
|
|
|
func New(store *adminstore.Store, authn *auth.Authenticator, basePath string, spa fs.FS) *Handler {
|
|
|
|
func New(store *adminstore.Store, authn *auth.Authenticator, basePath string, spa fs.FS) *Handler {
|
|
|
|
basePath = "/" + strings.Trim(basePath, "/")
|
|
|
|
basePath = "/" + strings.Trim(basePath, "/")
|
|
|
|
return &Handler{
|
|
|
|
return &Handler{
|
|
|
|
store: store,
|
|
|
|
store: store,
|
|
|
|
authn: authn,
|
|
|
|
authn: authn,
|
|
|
|
basePath: basePath,
|
|
|
|
basePath: basePath,
|
|
|
|
spa: spa,
|
|
|
|
spa: spa,
|
|
|
|
submitLimit: newRateLimiter(5, 10*time.Minute),
|
|
|
|
submitLimit: newRateLimiter(5, 10*time.Minute),
|
|
|
|
loginLimit: newLoginLimiter(10, 20, 15*time.Minute),
|
|
|
|
loginLimit: newLoginLimiter(10, 20, 15*time.Minute),
|
|
|
|
clientIP: clientip.Default(),
|
|
|
|
backflowRate: 30,
|
|
|
|
|
|
|
|
clientIP: clientip.Default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@@ -63,6 +70,12 @@ func (h *Handler) WithUsage(l *ratelimit.Limiter) *Handler {
|
|
|
|
return h
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithBackflowRate configures the per-key hourly backflow request limit.
|
|
|
|
|
|
|
|
func (h *Handler) WithBackflowRate(n int) *Handler {
|
|
|
|
|
|
|
|
h.backflowRate = n
|
|
|
|
|
|
|
|
return h
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithCache attaches a cache epoch invalidator for successful admin writes.
|
|
|
|
// WithCache attaches a cache epoch invalidator for successful admin writes.
|
|
|
|
func (h *Handler) WithCache(c cacheBumper) *Handler {
|
|
|
|
func (h *Handler) WithCache(c cacheBumper) *Handler {
|
|
|
|
h.cache = c
|
|
|
|
h.cache = c
|
|
|
@@ -281,7 +294,7 @@ func (h *Handler) ListProducts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
order := r.URL.Query().Get("order")
|
|
|
|
order := r.URL.Query().Get("order")
|
|
|
|
page, size := pageParams(r)
|
|
|
|
page, size := pageParams(r)
|
|
|
|
items, total, err := h.store.ListProducts(r.Context(), q, sort, order, size, (page-1)*size)
|
|
|
|
items, total, err := h.store.ListProducts(r.Context(), q, sort, order, size, (page-1)*size)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
|
@@ -293,7 +306,7 @@ func (h *Handler) ListProducts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// GetProduct returns full editable detail.
|
|
|
|
// GetProduct returns full editable detail.
|
|
|
|
func (h *Handler) GetProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) GetProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d, err := h.store.GetProduct(r.Context(), chi.URLParam(r, "id"))
|
|
|
|
d, err := h.store.GetProduct(r.Context(), chi.URLParam(r, "id"))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
@@ -316,7 +329,7 @@ func (h *Handler) CreateProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
writeError(w, http.StatusConflict, "duplicate_gtin", "该条码(GTIN)已被其它商品使用")
|
|
|
|
writeError(w, http.StatusConflict, "duplicate_gtin", "该条码(GTIN)已被其它商品使用")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, d)
|
|
|
|
writeJSON(w, http.StatusCreated, d)
|
|
|
@@ -325,7 +338,7 @@ func (h *Handler) CreateProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Stats returns the dashboard overview counters.
|
|
|
|
// Stats returns the dashboard overview counters.
|
|
|
|
func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
st, err := h.store.Stats(r.Context())
|
|
|
|
st, err := h.store.Stats(r.Context())
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, st)
|
|
|
|
writeJSON(w, http.StatusOK, st)
|
|
|
@@ -335,7 +348,7 @@ func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ListAllAudit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ListAllAudit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
page, size := pageParams(r)
|
|
|
|
page, size := pageParams(r)
|
|
|
|
items, total, err := h.store.ListAllAudit(r.Context(), size, (page-1)*size)
|
|
|
|
items, total, err := h.store.ListAllAudit(r.Context(), size, (page-1)*size)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
|
@@ -386,7 +399,7 @@ func (h *Handler) BulkProducts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
writeError(w, http.StatusBadRequest, "bad_request", "未选择任何商品")
|
|
|
|
writeError(w, http.StatusBadRequest, "bad_request", "未选择任何商品")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "affected": affected})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "affected": affected})
|
|
|
@@ -404,7 +417,7 @@ func (h *Handler) UpdateProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d, err := h.store.UpdateProduct(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
d, err := h.store.UpdateProduct(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
@@ -413,7 +426,7 @@ func (h *Handler) UpdateProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// ListAudit returns audit history for a product.
|
|
|
|
// ListAudit returns audit history for a product.
|
|
|
|
func (h *Handler) ListAudit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ListAudit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
items, err := h.store.ListAudit(r.Context(), chi.URLParam(r, "id"), 100)
|
|
|
|
items, err := h.store.ListAudit(r.Context(), chi.URLParam(r, "id"), 100)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
@@ -430,7 +443,7 @@ func (h *Handler) AddImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
im, err := h.store.AddImage(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), body.URL, body.Kind)
|
|
|
|
im, err := h.store.AddImage(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), body.URL, body.Kind)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, im)
|
|
|
|
writeJSON(w, http.StatusCreated, im)
|
|
|
@@ -439,7 +452,7 @@ func (h *Handler) AddImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// DeleteImage removes an image.
|
|
|
|
// DeleteImage removes an image.
|
|
|
|
func (h *Handler) DeleteImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) DeleteImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := h.store.DeleteImage(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "imageID"), auth.UserFrom(r.Context()))
|
|
|
|
err := h.store.DeleteImage(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "imageID"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
@@ -453,7 +466,7 @@ func (h *Handler) AddMSRP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m, err := h.store.AddMSRP(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
m, err := h.store.AddMSRP(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, m)
|
|
|
|
writeJSON(w, http.StatusCreated, m)
|
|
|
@@ -462,7 +475,7 @@ func (h *Handler) AddMSRP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// DeleteMSRP removes an MSRP snapshot.
|
|
|
|
// DeleteMSRP removes an MSRP snapshot.
|
|
|
|
func (h *Handler) DeleteMSRP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) DeleteMSRP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := h.store.DeleteMSRP(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "msrpID"), auth.UserFrom(r.Context()))
|
|
|
|
err := h.store.DeleteMSRP(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "msrpID"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
@@ -480,7 +493,7 @@ func (h *Handler) AddBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := h.store.AddBarcode(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
b, err := h.store.AddBarcode(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
if h.handleBarcodeErr(w, err) {
|
|
|
|
if h.handleBarcodeErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, b)
|
|
|
|
writeJSON(w, http.StatusCreated, b)
|
|
|
@@ -489,7 +502,7 @@ func (h *Handler) AddBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// DeleteBarcode removes a barcode; a primary one is replaced automatically.
|
|
|
|
// DeleteBarcode removes a barcode; a primary one is replaced automatically.
|
|
|
|
func (h *Handler) DeleteBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) DeleteBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := h.store.DeleteBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
|
|
|
err := h.store.DeleteBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
@@ -498,7 +511,7 @@ func (h *Handler) DeleteBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// SetPrimaryBarcode marks one barcode primary and mirrors it to product.gtin.
|
|
|
|
// SetPrimaryBarcode marks one barcode primary and mirrors it to product.gtin.
|
|
|
|
func (h *Handler) SetPrimaryBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) SetPrimaryBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := h.store.SetPrimaryBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
|
|
|
b, err := h.store.SetPrimaryBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
@@ -506,7 +519,7 @@ func (h *Handler) SetPrimaryBarcode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
// handleBarcodeErr maps barcode-specific errors (GTIN validation, ownership
|
|
|
|
// handleBarcodeErr maps barcode-specific errors (GTIN validation, ownership
|
|
|
|
// conflict) to client-facing statuses, falling back to handleErr otherwise.
|
|
|
|
// conflict) to client-facing statuses, falling back to handleErr otherwise.
|
|
|
|
func (h *Handler) handleBarcodeErr(w http.ResponseWriter, err error) bool {
|
|
|
|
func (h *Handler) handleBarcodeErr(w http.ResponseWriter, r *http.Request, err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -527,13 +540,13 @@ func (h *Handler) handleBarcodeErr(w http.ResponseWriter, err error) bool {
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_gtin", err.Error())
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_gtin", err.Error())
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h.handleErr(w, err)
|
|
|
|
return h.handleErr(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListBrands returns brand options.
|
|
|
|
// ListBrands returns brand options.
|
|
|
|
func (h *Handler) ListBrands(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ListBrands(w http.ResponseWriter, r *http.Request) {
|
|
|
|
items, err := h.store.ListBrands(r.Context())
|
|
|
|
items, err := h.store.ListBrands(r.Context())
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
@@ -547,7 +560,7 @@ func (h *Handler) ListKindFields(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items, err := h.store.ListKindFields(r.Context(), kind)
|
|
|
|
items, err := h.store.ListKindFields(r.Context(), kind)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items, "kind": kind})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items, "kind": kind})
|
|
|
@@ -556,7 +569,7 @@ func (h *Handler) ListKindFields(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// ListCategories returns category options.
|
|
|
|
// ListCategories returns category options.
|
|
|
|
func (h *Handler) ListCategories(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ListCategories(w http.ResponseWriter, r *http.Request) {
|
|
|
|
items, err := h.store.ListCategories(r.Context())
|
|
|
|
items, err := h.store.ListCategories(r.Context())
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
|
@@ -574,7 +587,7 @@ func (h *Handler) CreateCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := h.store.CreateCategory(r.Context(), auth.UserFrom(r.Context()), in)
|
|
|
|
c, err := h.store.CreateCategory(r.Context(), auth.UserFrom(r.Context()), in)
|
|
|
|
if h.handleCategoryErr(w, err) {
|
|
|
|
if h.handleCategoryErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, c)
|
|
|
|
writeJSON(w, http.StatusCreated, c)
|
|
|
@@ -592,7 +605,7 @@ func (h *Handler) UpdateCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := h.store.UpdateCategory(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
c, err := h.store.UpdateCategory(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
|
|
|
if h.handleCategoryErr(w, err) {
|
|
|
|
if h.handleCategoryErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, c)
|
|
|
|
writeJSON(w, http.StatusOK, c)
|
|
|
@@ -601,7 +614,7 @@ func (h *Handler) UpdateCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// DeleteCategory removes a leaf category that no product uses.
|
|
|
|
// DeleteCategory removes a leaf category that no product uses.
|
|
|
|
func (h *Handler) DeleteCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) DeleteCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := h.store.DeleteCategory(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
err := h.store.DeleteCategory(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleCategoryErr(w, err) {
|
|
|
|
if h.handleCategoryErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
@@ -609,7 +622,7 @@ func (h *Handler) DeleteCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
// handleCategoryErr maps category-specific errors to client statuses, falling
|
|
|
|
// handleCategoryErr maps category-specific errors to client statuses, falling
|
|
|
|
// back to handleErr otherwise.
|
|
|
|
// back to handleErr otherwise.
|
|
|
|
func (h *Handler) handleCategoryErr(w http.ResponseWriter, err error) bool {
|
|
|
|
func (h *Handler) handleCategoryErr(w http.ResponseWriter, r *http.Request, err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -627,7 +640,7 @@ func (h *Handler) handleCategoryErr(w http.ResponseWriter, err error) bool {
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_parent", "上级分类无效(不存在或不能移动到自身/子级下)")
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_parent", "上级分类无效(不存在或不能移动到自身/子级下)")
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h.handleErr(w, err)
|
|
|
|
return h.handleErr(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type brandInput struct {
|
|
|
|
type brandInput struct {
|
|
|
@@ -650,7 +663,7 @@ func (h *Handler) CreateBrand(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := h.store.CreateBrand(r.Context(), auth.UserFrom(r.Context()), in.Name)
|
|
|
|
b, err := h.store.CreateBrand(r.Context(), auth.UserFrom(r.Context()), in.Name)
|
|
|
|
if h.handleBrandErr(w, err) {
|
|
|
|
if h.handleBrandErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, b)
|
|
|
|
writeJSON(w, http.StatusCreated, b)
|
|
|
@@ -668,7 +681,7 @@ func (h *Handler) UpdateBrand(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := h.store.UpdateBrand(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in.Name)
|
|
|
|
b, err := h.store.UpdateBrand(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in.Name)
|
|
|
|
if h.handleBrandErr(w, err) {
|
|
|
|
if h.handleBrandErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
@@ -686,7 +699,7 @@ func (h *Handler) MergeBrands(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := h.store.MergeBrands(r.Context(), chi.URLParam(r, "id"), in.TargetID, auth.UserFrom(r.Context()))
|
|
|
|
b, err := h.store.MergeBrands(r.Context(), chi.URLParam(r, "id"), in.TargetID, auth.UserFrom(r.Context()))
|
|
|
|
if h.handleBrandErr(w, err) {
|
|
|
|
if h.handleBrandErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
|
writeJSON(w, http.StatusOK, b)
|
|
|
@@ -695,14 +708,14 @@ func (h *Handler) MergeBrands(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// DeleteBrand removes a brand no product references.
|
|
|
|
// DeleteBrand removes a brand no product references.
|
|
|
|
func (h *Handler) DeleteBrand(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) DeleteBrand(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := h.store.DeleteBrand(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
err := h.store.DeleteBrand(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleBrandErr(w, err) {
|
|
|
|
if h.handleBrandErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// handleBrandErr maps brand-specific errors to client statuses.
|
|
|
|
// handleBrandErr maps brand-specific errors to client statuses.
|
|
|
|
func (h *Handler) handleBrandErr(w http.ResponseWriter, err error) bool {
|
|
|
|
func (h *Handler) handleBrandErr(w http.ResponseWriter, r *http.Request, err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -717,7 +730,7 @@ func (h *Handler) handleBrandErr(w http.ResponseWriter, err error) bool {
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_merge", "合并目标无效(不存在或与源品牌相同)")
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid_merge", "合并目标无效(不存在或与源品牌相同)")
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h.handleErr(w, err)
|
|
|
|
return h.handleErr(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- submissions ----------
|
|
|
|
// ---------- submissions ----------
|
|
|
@@ -739,7 +752,7 @@ func (h *Handler) CreateSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id, err := h.store.CreateSubmission(r.Context(), in, realIP(r))
|
|
|
|
id, err := h.store.CreateSubmission(r.Context(), in, realIP(r))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
|
|
|
|
internalError(w, r, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, map[string]string{"id": id, "status": "pending"})
|
|
|
|
writeJSON(w, http.StatusCreated, map[string]string{"id": id, "status": "pending"})
|
|
|
@@ -758,12 +771,16 @@ func (h *Handler) Backflow(w http.ResponseWriter, r *http.Request) {
|
|
|
|
writeError(w, http.StatusUnauthorized, "invalid_api_key", "API key 格式无效")
|
|
|
|
writeError(w, http.StatusUnauthorized, "invalid_api_key", "API key 格式无效")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := h.store.APIKeyByHash(r.Context(), apikey.Hash(raw)); err != nil {
|
|
|
|
key, err := h.store.APIKeyByHash(r.Context(), apikey.Hash(raw))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, adminstore.ErrNotFound) {
|
|
|
|
if errors.Is(err, adminstore.ErrNotFound) {
|
|
|
|
writeError(w, http.StatusUnauthorized, "invalid_api_key", "API key 无效或已吊销")
|
|
|
|
writeError(w, http.StatusUnauthorized, "invalid_api_key", "API key 无效或已吊销")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
|
|
|
|
internalError(w, r, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.allowBackflow(w, r, key.ID) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@@ -784,12 +801,29 @@ func (h *Handler) Backflow(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
sum, err := h.store.CreateBackflowSubmissions(r.Context(), items, realIP(r))
|
|
|
|
sum, err := h.store.CreateBackflowSubmissions(r.Context(), items, realIP(r))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
|
|
|
|
internalError(w, r, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, sum)
|
|
|
|
writeJSON(w, http.StatusOK, sum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Handler) allowBackflow(w http.ResponseWriter, r *http.Request, keyID string) bool {
|
|
|
|
|
|
|
|
if h.usage == nil {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res := h.usage.Allow(r.Context(), "backflow:"+keyID, h.backflowRate, time.Hour)
|
|
|
|
|
|
|
|
if res.Allowed {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
retryAfter := res.ResetUnix - time.Now().Unix()
|
|
|
|
|
|
|
|
if retryAfter < 1 {
|
|
|
|
|
|
|
|
retryAfter = 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Retry-After", strconv.FormatInt(retryAfter, 10))
|
|
|
|
|
|
|
|
writeError(w, http.StatusTooManyRequests, "rate_limited", "回流请求超过每小时限制")
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// presentedAPIKey extracts a public API key from X-API-Key or a Bearer token.
|
|
|
|
// presentedAPIKey extracts a public API key from X-API-Key or a Bearer token.
|
|
|
|
func presentedAPIKey(r *http.Request) string {
|
|
|
|
func presentedAPIKey(r *http.Request) string {
|
|
|
|
if v := strings.TrimSpace(r.Header.Get("X-API-Key")); v != "" {
|
|
|
|
if v := strings.TrimSpace(r.Header.Get("X-API-Key")); v != "" {
|
|
|
@@ -806,7 +840,7 @@ func (h *Handler) ListSubmissions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
status := r.URL.Query().Get("status")
|
|
|
|
status := r.URL.Query().Get("status")
|
|
|
|
page, size := pageParams(r)
|
|
|
|
page, size := pageParams(r)
|
|
|
|
items, total, err := h.store.ListSubmissions(r.Context(), status, size, (page-1)*size)
|
|
|
|
items, total, err := h.store.ListSubmissions(r.Context(), status, size, (page-1)*size)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending, _ := h.store.PendingSubmissionCount(r.Context())
|
|
|
|
pending, _ := h.store.PendingSubmissionCount(r.Context())
|
|
|
@@ -818,7 +852,7 @@ func (h *Handler) ListSubmissions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// GetSubmission returns full submission detail (admin).
|
|
|
|
// GetSubmission returns full submission detail (admin).
|
|
|
|
func (h *Handler) GetSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) GetSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d, err := h.store.GetSubmission(r.Context(), chi.URLParam(r, "id"))
|
|
|
|
d, err := h.store.GetSubmission(r.Context(), chi.URLParam(r, "id"))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
@@ -827,7 +861,7 @@ func (h *Handler) GetSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// ApproveSubmission applies a contribution to the product store (admin).
|
|
|
|
// ApproveSubmission applies a contribution to the product store (admin).
|
|
|
|
func (h *Handler) ApproveSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *Handler) ApproveSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d, err := h.store.ApproveSubmission(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
d, err := h.store.ApproveSubmission(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()))
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
|
writeJSON(w, http.StatusOK, d)
|
|
|
@@ -840,7 +874,7 @@ func (h *Handler) RejectSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
err := h.store.RejectSubmission(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), body.Note)
|
|
|
|
err := h.store.RejectSubmission(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), body.Note)
|
|
|
|
if h.handleErr(w, err) {
|
|
|
|
if h.handleErr(w, r, err) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "rejected"})
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "rejected"})
|
|
|
@@ -855,7 +889,12 @@ func realIP(r *http.Request) string {
|
|
|
|
return r.RemoteAddr
|
|
|
|
return r.RemoteAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Handler) handleErr(w http.ResponseWriter, err error) bool {
|
|
|
|
func internalError(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
|
|
|
|
log.Printf("adminhandler: internal error request_id=%s: %v", middleware.GetReqID(r.Context()), err)
|
|
|
|
|
|
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "服务器内部错误")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Handler) handleErr(w http.ResponseWriter, r *http.Request, err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -867,7 +906,7 @@ func (h *Handler) handleErr(w http.ResponseWriter, err error) bool {
|
|
|
|
writeError(w, http.StatusConflict, "conflict", "该投稿已被处理")
|
|
|
|
writeError(w, http.StatusConflict, "conflict", "该投稿已被处理")
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
|
|
|
|
internalError(w, r, err)
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|