88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
"github.com/Jaculabilis/intake/web/html"
|
|
)
|
|
|
|
func (env *Env) getItem(writer http.ResponseWriter, req *http.Request) {
|
|
source := req.PathValue("source")
|
|
id := req.PathValue("id")
|
|
|
|
item, err := core.GetItem(env.db, source, id)
|
|
if err != nil {
|
|
writer.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
html.Item(writer, html.ItemData{Item: item})
|
|
}
|
|
|
|
func (env *Env) deleteItem(writer http.ResponseWriter, req *http.Request) {
|
|
source := req.PathValue("source")
|
|
id := req.PathValue("id")
|
|
|
|
_, err := core.DeactivateItem(env.db, source, id)
|
|
if err != nil {
|
|
writer.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
item, err := core.GetItem(env.db, source, id)
|
|
if err != nil {
|
|
writer.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
html.Item(writer, html.ItemData{Item: item})
|
|
}
|
|
|
|
func (env *Env) doAction(writer http.ResponseWriter, req *http.Request) {
|
|
source := req.PathValue("source")
|
|
id := req.PathValue("id")
|
|
action := req.PathValue("action")
|
|
|
|
item, err := core.GetItem(env.db, source, id)
|
|
if err != nil {
|
|
http.Error(writer, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
if item.Action[action] == nil {
|
|
http.Error(writer, "no such action", 500)
|
|
return
|
|
}
|
|
|
|
argv, err := core.GetArgvForAction(env.db, source, action)
|
|
if err != nil {
|
|
http.Error(writer, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
itemJson, err := json.Marshal(item)
|
|
if err != nil {
|
|
http.Error(writer, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
res, err := core.Execute(source, argv, nil, string(itemJson), time.Minute)
|
|
if err != nil {
|
|
http.Error(writer, err.Error(), 500)
|
|
return
|
|
}
|
|
if len(res) != 1 {
|
|
http.Error(writer, "not exactly one item", 500)
|
|
return
|
|
}
|
|
newItem := res[0]
|
|
core.BackfillItem(&newItem, &item)
|
|
|
|
if err = core.UpdateItems(env.db, []core.Item{newItem}); err != nil {
|
|
http.Error(writer, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
html.Item(writer, html.ItemData{Item: newItem})
|
|
}
|