Last active — forked from hiyosi/gist:3850864

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:11204417
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
<erl module=rest>
 
-include("qlc.hrl").
%-include("../../include/yaws_api.hrl").
 
-export([out/1, addAirport/4, handle/2 ]).
%-compile(export_all).
 
-define(RECORD_TYPE, airport).
-define(RECORD_KEY_FIELD, code).
 
-record(?RECORD_TYPE, {?RECORD_KEY_FIELD, city, country, name}).
 
 
out(Arg) ->
Method = method(Arg),
io:format("~p:~p ~p Request ~n", [?MODULE, ?LINE, Method]),
handle(Method, Arg).
 
method(Arg) ->
Rec = Arg#arg.req,
Rec#http_request.method.
 
 
convert_to_Json(Lines) ->
Data = [{obj,
[{airport, Line#?RECORD_TYPE.code},
{city, Line#?RECORD_TYPE.city},
{country, Line#?RECORD_TYPE.country},
{name, Line#?RECORD_TYPE.name}]}
|| Line <- Lines],
JsonData = {obj, [{data, Data}]},
rfc4627:encode(JsonData).
 
addAirport(Code, City, Country, Name) ->
NewRec = #?RECORD_TYPE{
?RECORD_KEY_FIELD = Code,
city = City,
country = Country,
name = Name},
io:format("~p:~p Adding Airport ~p~n", [?MODULE, ?LINE, NewRec]),
Add = fun() ->
mnesia:write(NewRec)
end,
{atomic, _Rec} = mnesia:transaction(Add),
NewRec.
 
handle('GET', _Arg) ->
io:format("~n ~p:~p GET Request ~n", [?MODULE, ?LINE]),
Records = do(qlc:q([X || X <- mnesia:table(?RECORD_TYPE)])),
Json = convert_to_Json(Records),
io:format("~n ~p:~p GET Request Response ~p ~n", [?MODULE, ?LINE, Json]),
{html, Json};
 
handle('POST', Arg) ->
io:format("~n Request Data ~p: ~n", [Arg#arg.clidata]),
{ok, Json, _} = rfc4627:decode(Arg#arg.clidata),
io:format("~n~p:~p POST request ~p~n", [?MODULE, ?LINE, Json]),
Airport = rfc4627:get_field(Json, "airport", <<>>),
City = rfc4627:get_field(Json, "city", <<>>),
Country = rfc4627:get_field(Json, "country", <<>>),
Name = rfc4627:get_field(Json, "country", <<>>),
_Status = addAirport(Airport, City, Country, Name),
[{status, 201}, {html, Arg#arg.clidata}];
 
handle('PUT', Arg) ->
[IndexValue,_] = string:tokens(Arg#arg.pathinfo),
{ok, Json, _} = rfc4627:decode(Arg#arg.clidata),
io:format("~p:~p PUT request ~p ~p~n", [?MODULE, ?LINE, IndexValue, Json]),
Airport = rfc4627:get_field(Json, "airport", <<>>),
City = rfc4627:get_field(Json, "city", <<>>),
Country = rfc4627:get_field(Json, "country", <<>>),
Name = rfc4627:get_field(Json, "country", <<>>),
 
NewRec = #?RECORD_TYPE{
?RECORD_KEY_FIELD = Airport,
city = City,
country = Country,
name = Name},
io:format("~p:~p Renaming ~p", [?MODULE, ?LINE, NewRec]),
ChangeName = fun() ->
mnesia:delete({?RECORD_KEY_FIELD, IndexValue}),
mnesia:write(NewRec)
end,
{atomic, _Rec} = mnesia:transaction(ChangeName),
[{status, 200},
{html, IndexValue}];
 
handle('DELETE', Arg) ->
[IndexValue, _] = string:tokens(Arg#arg.pathinfo),
io:format("~p:~p DELETE request ~p", [?MODULE, ?LINE, IndexValue]),
Delete = fun() ->
mnesia:delete({?RECORD_KEY_FIELD, IndexValue})
end,
Resp = mnesia:transaction(Delete),
case Resp of
{atomic, ok} ->
[{status, 204}];
{_, Error} ->
io:format("~p:~p Error ~p ", [?MODULE, ?LINE, Error]),
[{status, 400}, {html, Error}]
end;
 
handle(Method,_) ->
[{error, "Unknown method " ++ Method },
{status, 405},
{header, "Allow: GET, HEAD, POST, PUT, DELETE"}
].
 
do(Q) ->
F = fun() ->
qlc:e(Q)
end,
{atomic, Value} = mnesia:transaction(F),
Value.
</erl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.