Added arn to the main repository

This commit is contained in:
2019-06-03 18:32:43 +09:00
parent cf258573a8
commit 29a48d94a5
465 changed files with 15968 additions and 288 deletions

54
arn/IDCollection.go Normal file
View File

@ -0,0 +1,54 @@
package arn
import (
"errors"
"github.com/aerogo/aero"
"github.com/aerogo/api"
)
// IDCollection ...
type IDCollection interface {
Add(id string) error
Remove(id string) bool
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
},
}
}