cart
This commit is contained in:
@@ -24,11 +24,12 @@ import (
|
||||
|
||||
type FileServer struct {
|
||||
// Define fields here
|
||||
dataDir string
|
||||
checkoutDataDir string
|
||||
promotionsFile string
|
||||
storage actor.LogStorage[cart.CartGrain]
|
||||
checkoutStorage actor.LogStorage[checkout.CheckoutGrain]
|
||||
dataDir string
|
||||
checkoutDataDir string
|
||||
promotionsFile string
|
||||
storage actor.LogStorage[cart.CartGrain]
|
||||
checkoutStorage actor.LogStorage[checkout.CheckoutGrain]
|
||||
OnVouchersChange func(codes []string)
|
||||
}
|
||||
|
||||
func NewFileServer(dataDir string, checkoutDataDir string, promotionsFile string, storage actor.LogStorage[cart.CartGrain], checkoutStorage actor.LogStorage[checkout.CheckoutGrain]) *FileServer {
|
||||
@@ -276,15 +277,37 @@ func (fs *FileServer) VoucherHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
_ = os.MkdirAll(filepath.Dir(fileName), 0755)
|
||||
file, err := os.Create(fileName)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
io.Copy(file, r.Body)
|
||||
var parsed struct {
|
||||
State struct {
|
||||
Vouchers []struct {
|
||||
Code string `json:"code"`
|
||||
} `json:"vouchers"`
|
||||
} `json:"state"`
|
||||
}
|
||||
var codes []string
|
||||
if err := json.Unmarshal(body, &parsed); err == nil {
|
||||
for _, v := range parsed.State.Vouchers {
|
||||
if v.Code != "" {
|
||||
codes = append(codes, v.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = os.MkdirAll(filepath.Dir(fileName), 0755)
|
||||
if err := os.WriteFile(fileName, body, 0644); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if fs.OnVouchersChange != nil {
|
||||
fs.OnVouchersChange(codes)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user