| 1 |
|
-module(shurbej_http_item_template). |
| 2 |
|
-export([init/2]). |
| 3 |
|
|
| 4 |
|
%% GET /items/new?itemType=book — return a blank template for the given item type. |
| 5 |
|
init(Req0, State) -> |
| 6 |
6 |
case cowboy_req:method(Req0) of |
| 7 |
|
<<"GET">> -> |
| 8 |
5 |
#{itemType := Type} = cowboy_req:match_qs([{itemType, [], <<>>}], Req0), |
| 9 |
5 |
case Type of |
| 10 |
|
<<>> -> |
| 11 |
1 |
Req = shurbej_http_common:error_response(400, |
| 12 |
|
<<"'itemType' query parameter is required">>, Req0), |
| 13 |
1 |
{ok, Req, State}; |
| 14 |
|
_ -> |
| 15 |
4 |
case lists:member(Type, shurbej_validate:item_types()) of |
| 16 |
|
true -> |
| 17 |
3 |
Template = base_template(Type), |
| 18 |
3 |
Req = shurbej_http_common:json_response(200, Template, Req0), |
| 19 |
3 |
{ok, Req, State}; |
| 20 |
|
false -> |
| 21 |
1 |
Req = shurbej_http_common:error_response(400, |
| 22 |
|
<<"Unknown item type: ", Type/binary>>, Req0), |
| 23 |
1 |
{ok, Req, State} |
| 24 |
|
end |
| 25 |
|
end; |
| 26 |
|
_ -> |
| 27 |
1 |
Req = shurbej_http_common:error_response(405, <<"Method not allowed">>, Req0), |
| 28 |
1 |
{ok, Req, State} |
| 29 |
|
end. |
| 30 |
|
|
| 31 |
|
base_template(Type) -> |
| 32 |
3 |
Base = #{ |
| 33 |
|
<<"itemType">> => Type, |
| 34 |
|
<<"title">> => <<>>, |
| 35 |
|
<<"creators">> => [], |
| 36 |
|
<<"tags">> => [], |
| 37 |
|
<<"collections">> => [], |
| 38 |
|
<<"relations">> => #{} |
| 39 |
|
}, |
| 40 |
|
%% Add type-specific fields |
| 41 |
3 |
maps:merge(Base, type_fields(Type)). |
| 42 |
|
|
| 43 |
|
type_fields(<<"note">>) -> |
| 44 |
1 |
#{<<"note">> => <<>>}; |
| 45 |
|
type_fields(<<"attachment">>) -> |
| 46 |
1 |
#{<<"linkMode">> => <<>>, <<"contentType">> => <<>>, <<"charset">> => <<>>, |
| 47 |
|
<<"filename">> => <<>>, <<"path">> => <<>>}; |
| 48 |
|
type_fields(<<"annotation">>) -> |
| 49 |
:-( |
#{<<"annotationType">> => <<>>, <<"annotationText">> => <<>>, |
| 50 |
|
<<"annotationComment">> => <<>>, <<"annotationColor">> => <<>>, |
| 51 |
|
<<"annotationPageLabel">> => <<>>, <<"annotationPosition">> => <<>>}; |
| 52 |
|
type_fields(_) -> |
| 53 |
|
%% Common fields for most item types |
| 54 |
1 |
#{<<"abstractNote">> => <<>>, <<"date">> => <<>>, <<"language">> => <<>>, |
| 55 |
|
<<"shortTitle">> => <<>>, <<"url">> => <<>>, <<"accessDate">> => <<>>, |
| 56 |
|
<<"extra">> => <<>>, <<"rights">> => <<>>}. |