/__w/shurbej/shurbej/_build/test/cover/aggregate/shurbej_http_tags.html

1 -module(shurbej_http_tags).
2 -include_lib("shurbej_store/include/shurbej_records.hrl").
3
4 -export([init/2]).
5
6 init(Req0, State) ->
7 9 case shurbej_http_common:authorize(Req0) of
8 {ok, LibRef, _} ->
9 8 Method = cowboy_req:method(Req0),
10 8 Perm = perm_for_method(Method),
11 8 case shurbej_http_common:check_lib_perm(Perm, LibRef) of
12 {error, forbidden} ->
13
:-(
Req = shurbej_http_common:error_response(403, <<"Access denied">>, Req0),
14
:-(
{ok, Req, State};
15 ok ->
16 8 handle(Method, Req0, State)
17 end;
18 {error, Reason, _} ->
19 1 Req = shurbej_http_common:auth_error_response(Reason, Req0),
20 1 {ok, Req, State}
21 end.
22
23 6 perm_for_method(<<"GET">>) -> read;
24
:-(
perm_for_method(<<"HEAD">>) -> read;
25 2 perm_for_method(_) -> write.
26
27 %% GET — list tags (optionally scoped to items matching filters)
28 handle(<<"GET">>, Req0, State) ->
29 6 LibRef = shurbej_http_common:lib_ref(Req0),
30 6 {ok, LibVersion} = shurbej_version:get(LibRef),
31 6 case shurbej_http_common:check_304(Req0, LibVersion) of
32 1 {304, Req} -> {ok, Req, State};
33 5 continue -> handle_get_tags(Req0, LibRef, LibVersion, State)
34 end;
35
36 %% DELETE /tags?tag=t1||t2
37 handle(<<"DELETE">>, Req0, State) ->
38 1 LibRef = shurbej_http_common:lib_ref(Req0),
39 1 ExpectedVersion = shurbej_http_common:get_if_unmodified(Req0),
40 1 #{tag := TagParam} = cowboy_req:match_qs([{tag, [], <<>>}], Req0),
41 1 case TagParam of
42 <<>> ->
43
:-(
Req = shurbej_http_common:error_response(400, <<"No tags specified">>, Req0),
44
:-(
{ok, Req, State};
45 _ ->
46 1 TagNames = binary:split(TagParam, <<"||">>, [global]),
47 1 case shurbej_version:write(LibRef, ExpectedVersion, fun(NewVersion) ->
48 1 Deleted = shurbej_db:delete_tags_by_name(LibRef, TagNames),
49 1 lists:foreach(fun(Tag) ->
50 1 shurbej_db:record_deletion(LibRef, <<"tag">>, Tag, NewVersion)
51 end, Deleted),
52 1 ok
53 end) of
54 {ok, NewVersion} ->
55 1 Req = cowboy_req:reply(204, #{
56 <<"last-modified-version">> => integer_to_binary(NewVersion)
57 }, Req0),
58 1 {ok, Req, State};
59 {error, precondition, CurrentVersion} ->
60
:-(
Req = shurbej_http_common:json_response(412,
61 #{<<"message">> => <<"Library has been modified since specified version">>},
62 CurrentVersion, Req0),
63
:-(
{ok, Req, State}
64 end
65 end;
66
67 handle(_, Req0, State) ->
68 1 Req = shurbej_http_common:error_response(405, <<"Method not allowed">>, Req0),
69 1 {ok, Req, State}.
70
71 handle_get_tags(Req0, LibRef, LibVersion, State) ->
72 5 Since = shurbej_http_common:get_since(Req0),
73 5 Scope = maps:get(scope, State, all),
74 5 TagPairs = case Scope of
75 item_tags ->
76 1 ItemKey = cowboy_req:binding(item_key, Req0),
77 1 shurbej_db:list_item_tags(LibRef, ItemKey);
78 _ ->
79 4 shurbej_db:list_tags(LibRef, Since)
80 end,
81 5 Format = shurbej_http_common:get_format(Req0),
82 5 case Format of
83 <<"versions">> ->
84 1 VersionMap = maps:from_list([{Tag, 0} || {Tag, _} <- TagPairs]),
85 1 Req = shurbej_http_common:json_response(200, VersionMap, LibVersion, Req0),
86 1 {ok, Req, State};
87 _ ->
88 4 Tags = [#{<<"tag">> => Tag, <<"type">> => Type} || {Tag, Type} <- TagPairs],
89 4 Req = shurbej_http_common:json_response(200, Tags, LibVersion, Req0),
90 4 {ok, Req, State}
91 end.
Line Hits Source