35 lines
708 B
Plaintext
35 lines
708 B
Plaintext
|
import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
|
||
|
import insert from table
|
||
|
import max from math
|
||
|
|
||
|
api = (tab) ->
|
||
|
json_params capture_errors {
|
||
|
respond_to(tab),
|
||
|
on_error: =>
|
||
|
status = 400
|
||
|
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: { success: false, :errors })
|
||
|
}
|
||
|
|
||
|
abort = (status, message) ->
|
||
|
if message
|
||
|
yield_error {status, message}
|
||
|
else
|
||
|
yield_error status
|
||
|
|
||
|
assert_model = (result, err) ->
|
||
|
abort 500, err if err
|
||
|
return result
|
||
|
|
||
|
{
|
||
|
:api
|
||
|
:abort
|
||
|
:assert_model
|
||
|
}
|