cart
This commit is contained in:
+42
-9
@@ -18,6 +18,7 @@ type Store struct {
|
||||
path string
|
||||
version int
|
||||
promotions []PromotionRule
|
||||
OnChange func(id string, isDelete bool)
|
||||
}
|
||||
|
||||
// NewStore loads the state file at path (an absent file yields an empty store)
|
||||
@@ -72,40 +73,72 @@ func (s *Store) Upsert(rule PromotionRule) (replaced bool, err error) {
|
||||
if rule.ID == "" {
|
||||
return false, fmt.Errorf("promotion id is required")
|
||||
}
|
||||
var cb func(string, bool)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
defer func() {
|
||||
s.mu.Unlock()
|
||||
if err == nil && cb != nil {
|
||||
cb(rule.ID, false)
|
||||
}
|
||||
}()
|
||||
cb = s.OnChange
|
||||
|
||||
for i := range s.promotions {
|
||||
if s.promotions[i].ID == rule.ID {
|
||||
s.promotions[i] = rule
|
||||
return true, s.saveLocked()
|
||||
replaced = true
|
||||
err = s.saveLocked()
|
||||
return
|
||||
}
|
||||
}
|
||||
s.promotions = append(s.promotions, rule)
|
||||
return false, s.saveLocked()
|
||||
replaced = false
|
||||
err = s.saveLocked()
|
||||
return
|
||||
}
|
||||
|
||||
// SetStatus changes the status of a rule and persists. Returns false if no rule
|
||||
// with the id exists.
|
||||
func (s *Store) SetStatus(id string, status PromotionStatus) (bool, error) {
|
||||
func (s *Store) SetStatus(id string, status PromotionStatus) (found bool, err error) {
|
||||
var cb func(string, bool)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
defer func() {
|
||||
s.mu.Unlock()
|
||||
if err == nil && found && cb != nil {
|
||||
cb(id, false)
|
||||
}
|
||||
}()
|
||||
cb = s.OnChange
|
||||
|
||||
for i := range s.promotions {
|
||||
if s.promotions[i].ID == id {
|
||||
s.promotions[i].Status = status
|
||||
return true, s.saveLocked()
|
||||
found = true
|
||||
err = s.saveLocked()
|
||||
return
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Delete removes the rule with the id and persists. Returns false if not found.
|
||||
func (s *Store) Delete(id string) (bool, error) {
|
||||
func (s *Store) Delete(id string) (found bool, err error) {
|
||||
var cb func(string, bool)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
defer func() {
|
||||
s.mu.Unlock()
|
||||
if err == nil && found && cb != nil {
|
||||
cb(id, true)
|
||||
}
|
||||
}()
|
||||
cb = s.OnChange
|
||||
|
||||
for i := range s.promotions {
|
||||
if s.promotions[i].ID == id {
|
||||
s.promotions = append(s.promotions[:i], s.promotions[i+1:]...)
|
||||
return true, s.saveLocked()
|
||||
found = true
|
||||
err = s.saveLocked()
|
||||
return
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
|
||||
Reference in New Issue
Block a user