2018-04-25 00:55:38 +00:00
|
|
|
import json_params, capture_errors, yield_error from require "lapis.application"
|
|
|
|
import insert, remove from table
|
|
|
|
import max from math
|
2018-04-25 00:28:23 +00:00
|
|
|
|
|
|
|
api_request = (fn) ->
|
|
|
|
json_params capture_errors {
|
|
|
|
fn,
|
|
|
|
on_error: =>
|
|
|
|
status = 400
|
2018-04-25 00:55:38 +00:00
|
|
|
errors = {}
|
|
|
|
for err in *@errors
|
|
|
|
if "table" == type err
|
|
|
|
status = max status, err[1]
|
|
|
|
insert errors, err[2]
|
|
|
|
else
|
|
|
|
insert errors, err
|
|
|
|
return(:status, json: { :errors })
|
2018-04-25 00:28:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:55:38 +00:00
|
|
|
abort = (status, message) ->
|
|
|
|
if message
|
|
|
|
yield_error {status, message}
|
|
|
|
else
|
|
|
|
yield_error status
|
|
|
|
|
2018-04-25 00:28:23 +00:00
|
|
|
{
|
|
|
|
:api_request
|
2018-04-25 00:55:38 +00:00
|
|
|
:abort
|
2018-04-25 00:28:23 +00:00
|
|
|
}
|