2019-06-03 09:32:43 +00:00
|
|
|
package arn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/aerogo/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IDCollection ...
|
|
|
|
type IDCollection interface {
|
2019-11-18 06:13:51 +00:00
|
|
|
Add(id ID) error
|
|
|
|
Remove(id ID) bool
|
2019-06-03 09:32:43 +00:00
|
|
|
Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAction returns an API action that adds a new item to the IDCollection.
|
|
|
|
func AddAction() *api.Action {
|
|
|
|
return &api.Action{
|
|
|
|
Name: "add",
|
|
|
|
Route: "/add/:item-id",
|
|
|
|
Run: func(obj interface{}, ctx aero.Context) error {
|
|
|
|
list := obj.(IDCollection)
|
|
|
|
itemID := ctx.Get("item-id")
|
|
|
|
err := list.Add(itemID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Save()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAction returns an API action that removes an item from the IDCollection.
|
|
|
|
func RemoveAction() *api.Action {
|
|
|
|
return &api.Action{
|
|
|
|
Name: "remove",
|
|
|
|
Route: "/remove/:item-id",
|
|
|
|
Run: func(obj interface{}, ctx aero.Context) error {
|
|
|
|
list := obj.(IDCollection)
|
|
|
|
itemID := ctx.Get("item-id")
|
|
|
|
|
|
|
|
if !list.Remove(itemID) {
|
|
|
|
return errors.New("This item does not exist in the list")
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Save()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|