mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
This will add the endpoint to the API.
This commit is contained in:
parent
3fdae6b99c
commit
297e0909d6
5 changed files with 95 additions and 0 deletions
16
lib/philomena_web/award_json.ex
Executable file
16
lib/philomena_web/award_json.ex
Executable file
|
@ -0,0 +1,16 @@
|
|||
defmodule PhilomenaWeb.AwardsJson do
|
||||
|
||||
def as_json(_conn, award) do
|
||||
%{
|
||||
image_url: badge_url_root() <> "/" <> award.badge.image,
|
||||
title: award.badge.title,
|
||||
id: award.badge_id,
|
||||
label: award.label,
|
||||
awarded_on: award.awarded_on
|
||||
}
|
||||
end
|
||||
|
||||
defp badge_url_root do
|
||||
Application.get_env(:philomena, :badge_url_root)
|
||||
end
|
||||
end
|
26
lib/philomena_web/controllers/api/json/profile_controller.ex
Executable file
26
lib/philomena_web/controllers/api/json/profile_controller.ex
Executable file
|
@ -0,0 +1,26 @@
|
|||
defmodule PhilomenaWeb.Api.Json.ProfileController do
|
||||
use PhilomenaWeb, :controller
|
||||
|
||||
alias PhilomenaWeb.UserJson
|
||||
alias Philomena.Users.User
|
||||
alias Philomena.Repo
|
||||
import Ecto.Query
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
profile =
|
||||
User
|
||||
|> where(id: ^id)
|
||||
|> preload([public_links: :tag, awards: :badge])
|
||||
|> Repo.one()
|
||||
|
||||
cond do
|
||||
is_nil(profile) or profile.deleted_at ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> text("")
|
||||
|
||||
true ->
|
||||
json(conn, %{user: UserJson.as_json(conn, profile)})
|
||||
end
|
||||
end
|
||||
end
|
21
lib/philomena_web/links_json.ex
Executable file
21
lib/philomena_web/links_json.ex
Executable file
|
@ -0,0 +1,21 @@
|
|||
defmodule PhilomenaWeb.LinksJson do
|
||||
|
||||
def as_json(_conn, %{public: false}), do: nil
|
||||
|
||||
def as_json(_conn, link) do
|
||||
%{
|
||||
user_id: link.user_id,
|
||||
created_at: link.created_at,
|
||||
state: link.aasm_state,
|
||||
tag_id: tag_id(link.tag)
|
||||
}
|
||||
end
|
||||
|
||||
defp tag_id(nil) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp tag_id(tag) do
|
||||
tag.id
|
||||
end
|
||||
end
|
|
@ -118,6 +118,7 @@ defmodule PhilomenaWeb.Router do
|
|||
resources "/tags", TagController, only: [:show]
|
||||
resources "/comments", CommentController, only: [:show]
|
||||
resources "/posts", PostController, only: [:show]
|
||||
resources "/profiles", ProfileController, only: [:show]
|
||||
|
||||
resources "/forums", ForumController, only: [:show, :index] do
|
||||
resources "/topics", Forum.TopicController, only: [:show, :index] do
|
||||
|
|
31
lib/philomena_web/user_json.ex
Executable file
31
lib/philomena_web/user_json.ex
Executable file
|
@ -0,0 +1,31 @@
|
|||
defmodule PhilomenaWeb.UserJson do
|
||||
|
||||
alias PhilomenaWeb.LinksJson
|
||||
alias PhilomenaWeb.AwardsJson
|
||||
|
||||
def as_json(conn, user) do
|
||||
%{
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
slug: user.slug,
|
||||
role: role(user),
|
||||
description: user.description,
|
||||
avatar_url: user.avatar,
|
||||
created_at: user.created_at,
|
||||
comments_count: user.comments_posted_count,
|
||||
uploads_count: user.uploads_count,
|
||||
posts_count: user.forum_posts_count,
|
||||
topics_count: user.topic_count,
|
||||
links: Enum.map(user.public_links, &LinksJson.as_json(conn, &1)),
|
||||
awards: Enum.map(user.awards, &AwardsJson.as_json(conn, &1))
|
||||
}
|
||||
end
|
||||
|
||||
defp role(%{hide_default_role: true}) do
|
||||
"user"
|
||||
end
|
||||
|
||||
defp role(user) do
|
||||
user.role
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue