| 1 |
|
-module(shurbej_http_meta). |
| 2 |
|
-export([init/2]). |
| 3 |
|
|
| 4 |
|
%% Serves schema-derived metadata endpoints: |
| 5 |
|
%% GET /itemTypes — all item types |
| 6 |
|
%% GET /itemFields — all item fields |
| 7 |
|
%% GET /itemTypeFields?itemType=<type> — fields for a type |
| 8 |
|
%% GET /itemTypeCreatorTypes?itemType=<type> — creator types for a type |
| 9 |
|
%% GET /creatorFields — creator field names |
| 10 |
|
|
| 11 |
|
init(Req0, #{action := Action} = State) -> |
| 12 |
5 |
case cowboy_req:method(Req0) of |
| 13 |
5 |
<<"GET">> -> handle(Action, Req0, State); |
| 14 |
|
_ -> |
| 15 |
:-( |
Req = shurbej_http_common:error_response(405, <<"Method not allowed">>, Req0), |
| 16 |
:-( |
{ok, Req, State} |
| 17 |
|
end. |
| 18 |
|
|
| 19 |
|
handle(item_types, Req0, State) -> |
| 20 |
1 |
Schema = get_schema(), |
| 21 |
1 |
Types = [#{<<"itemType">> => maps:get(<<"itemType">>, IT), |
| 22 |
|
<<"localized">> => maps:get(<<"itemType">>, IT)} |
| 23 |
1 |
|| IT <- maps:get(<<"itemTypes">>, Schema, [])], |
| 24 |
1 |
Req = shurbej_http_common:json_response(200, Types, Req0), |
| 25 |
1 |
{ok, Req, State}; |
| 26 |
|
|
| 27 |
|
handle(item_fields, Req0, State) -> |
| 28 |
1 |
Schema = get_schema(), |
| 29 |
1 |
AllFields = lists:usort(lists:flatmap(fun(IT) -> |
| 30 |
40 |
[maps:get(<<"field">>, F) || F <- maps:get(<<"fields">>, IT, [])] |
| 31 |
|
end, maps:get(<<"itemTypes">>, Schema, []))), |
| 32 |
1 |
Fields = [#{<<"field">> => F, <<"localized">> => F} || F <- AllFields], |
| 33 |
1 |
Req = shurbej_http_common:json_response(200, Fields, Req0), |
| 34 |
1 |
{ok, Req, State}; |
| 35 |
|
|
| 36 |
|
handle(item_type_fields, Req0, State) -> |
| 37 |
1 |
#{itemType := Type} = cowboy_req:match_qs([{itemType, [], <<>>}], Req0), |
| 38 |
1 |
case Type of |
| 39 |
|
<<>> -> |
| 40 |
:-( |
Req = shurbej_http_common:error_response(400, |
| 41 |
|
<<"'itemType' is required">>, Req0), |
| 42 |
:-( |
{ok, Req, State}; |
| 43 |
|
_ -> |
| 44 |
1 |
Schema = get_schema(), |
| 45 |
1 |
case find_item_type(Type, Schema) of |
| 46 |
|
undefined -> |
| 47 |
:-( |
Req = shurbej_http_common:error_response(404, |
| 48 |
|
<<"Unknown item type">>, Req0), |
| 49 |
:-( |
{ok, Req, State}; |
| 50 |
|
IT -> |
| 51 |
1 |
Fields = [format_field(F) || F <- maps:get(<<"fields">>, IT, [])], |
| 52 |
1 |
Req = shurbej_http_common:json_response(200, Fields, Req0), |
| 53 |
1 |
{ok, Req, State} |
| 54 |
|
end |
| 55 |
|
end; |
| 56 |
|
|
| 57 |
|
handle(item_type_creator_types, Req0, State) -> |
| 58 |
1 |
#{itemType := Type} = cowboy_req:match_qs([{itemType, [], <<>>}], Req0), |
| 59 |
1 |
case Type of |
| 60 |
|
<<>> -> |
| 61 |
:-( |
Req = shurbej_http_common:error_response(400, |
| 62 |
|
<<"'itemType' is required">>, Req0), |
| 63 |
:-( |
{ok, Req, State}; |
| 64 |
|
_ -> |
| 65 |
1 |
Schema = get_schema(), |
| 66 |
1 |
case find_item_type(Type, Schema) of |
| 67 |
|
undefined -> |
| 68 |
:-( |
Req = shurbej_http_common:error_response(404, |
| 69 |
|
<<"Unknown item type">>, Req0), |
| 70 |
:-( |
{ok, Req, State}; |
| 71 |
|
IT -> |
| 72 |
1 |
CTypes = [#{<<"creatorType">> => maps:get(<<"creatorType">>, C), |
| 73 |
|
<<"localized">> => maps:get(<<"creatorType">>, C)} |
| 74 |
1 |
|| C <- maps:get(<<"creatorTypes">>, IT, [])], |
| 75 |
1 |
Req = shurbej_http_common:json_response(200, CTypes, Req0), |
| 76 |
1 |
{ok, Req, State} |
| 77 |
|
end |
| 78 |
|
end; |
| 79 |
|
|
| 80 |
|
handle(creator_fields, Req0, State) -> |
| 81 |
1 |
Fields = [ |
| 82 |
|
#{<<"field">> => <<"firstName">>, <<"localized">> => <<"firstName">>}, |
| 83 |
|
#{<<"field">> => <<"lastName">>, <<"localized">> => <<"lastName">>} |
| 84 |
|
], |
| 85 |
1 |
Req = shurbej_http_common:json_response(200, Fields, Req0), |
| 86 |
1 |
{ok, Req, State}. |
| 87 |
|
|
| 88 |
|
%% Internal |
| 89 |
|
|
| 90 |
|
get_schema() -> |
| 91 |
4 |
shurbej_schema_data:schema(). |
| 92 |
|
|
| 93 |
|
find_item_type(Type, Schema) -> |
| 94 |
2 |
case [IT || IT <- maps:get(<<"itemTypes">>, Schema, []), |
| 95 |
80 |
maps:get(<<"itemType">>, IT) =:= Type] of |
| 96 |
2 |
[Found | _] -> Found; |
| 97 |
:-( |
[] -> undefined |
| 98 |
|
end. |
| 99 |
|
|
| 100 |
|
format_field(F) -> |
| 101 |
29 |
Base = #{<<"field">> => maps:get(<<"field">>, F), |
| 102 |
|
<<"localized">> => maps:get(<<"field">>, F)}, |
| 103 |
29 |
case maps:get(<<"baseField">>, F, undefined) of |
| 104 |
28 |
undefined -> Base; |
| 105 |
1 |
BF -> Base#{<<"baseField">> => BF} |
| 106 |
|
end. |