Skip to content

Commit

Permalink
add tag controller and /api/tag endpoint, #14
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Apr 20, 2020
1 parent 14dc9b4 commit 17a1523
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/app_api_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule AppApiWeb.TagController do
use AppApiWeb, :controller
alias AppApi.Tags

def index(conn, _params) do
tags = Tags.get_tags_by_id_person(conn.assigns.person.id_person)
default_tags = Tags.get_default_tags()
render(conn, "index.json", tags: default_tags ++ tags)
end
end
1 change: 1 addition & 0 deletions lib/app_api_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ defmodule AppApiWeb.Router do
resources "/capture", CaptureController, only: [:index, :create, :show, :update] do
resources "/timers", TimerController, only: [:index, :create, :update]
end
get "/tags", TagController, :index
end
end
4 changes: 3 additions & 1 deletion lib/app_api_web/views/capture_view.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AppApiWeb.CaptureView do
use AppApiWeb, :view
alias AppApiWeb.TimerView
alias AppApiWeb.TagView

def render("index.json", %{captures: captures}) do
%{data: Enum.map(captures, &capture_to_json/1)}
Expand All @@ -20,7 +21,8 @@ defmodule AppApiWeb.CaptureView do
id_person: capture.id_person,
text: capture.text,
completed: capture.completed,
timers: Enum.map(capture.timers, &TimerView.timer_to_json/1)
timers: Enum.map(capture.timers, &TimerView.timer_to_json/1),
tags: Enum.map(capture.tags, &TagView.tag_to_json/1)
}
end
end
14 changes: 14 additions & 0 deletions lib/app_api_web/views/tag_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule AppApiWeb.TagView do
use AppApiWeb, :view

def render("index.json", %{tags: tags}) do
%{data: Enum.map(tags, &tag_to_json/1)}
end

def tag_to_json(tag) do
%{
text: tag.text,
id_person: tag.id_person
}
end
end

0 comments on commit 17a1523

Please sign in to comment.