From 4a54661f24a48c5bb9e0db28d5bf325b2ecfbf7e Mon Sep 17 00:00:00 2001 From: matst80 Date: Fri, 17 Oct 2025 12:48:43 +0200 Subject: [PATCH] add voucher store --- cmd/backoffice/fileserver.go | 28 ++++++++++++++++++++++++++++ cmd/backoffice/main.go | 1 + 2 files changed, 29 insertions(+) diff --git a/cmd/backoffice/fileserver.go b/cmd/backoffice/fileserver.go index 21106b0..69da05c 100644 --- a/cmd/backoffice/fileserver.go +++ b/cmd/backoffice/fileserver.go @@ -184,6 +184,34 @@ func (fs *FileServer) PromotionsHandler(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusMethodNotAllowed) } +func (fs *FileServer) VoucherHandler(w http.ResponseWriter, r *http.Request) { + fileName := filepath.Join(fs.dataDir, "vouchers.json") + if r.Method == http.MethodGet { + file, err := os.Open(fileName) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + defer file.Close() + + io.Copy(w, file) + return + } + if r.Method == http.MethodPost { + file, err := os.Create(fileName) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + defer file.Close() + + io.Copy(file, r.Body) + return + } + + w.WriteHeader(http.StatusMethodNotAllowed) +} + func (fs *FileServer) PromotionPartHandler(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("id") if idStr == "" { diff --git a/cmd/backoffice/main.go b/cmd/backoffice/main.go index 7f489ce..402ec81 100644 --- a/cmd/backoffice/main.go +++ b/cmd/backoffice/main.go @@ -90,6 +90,7 @@ func main() { mux.HandleFunc("GET /carts", fs.CartsHandler) mux.HandleFunc("GET /cart/{id}", fs.CartHandler) mux.HandleFunc("/promotions", fs.PromotionsHandler) + mux.HandleFunc("/vouchers", fs.VoucherHandler) mux.HandleFunc("/promotion/{id}", fs.PromotionPartHandler) mux.HandleFunc("/ws", hub.ServeWS)