Packages

This library is a summary of the functions that are generally required for Matching Web service development.

Current section

Files

Jump to
servicex_matching lib servicex_matching_web controllers user_profile_controller.ex
Raw

lib/servicex_matching_web/controllers/user_profile_controller.ex

defmodule ServicexMatchingWeb.UserProfileController do
use ServicexMatchingWeb, :controller
alias ServicexMatching.Accounts
alias ServicexMatching.Accounts.UserProfile
alias ServicexMatching.Joiners
alias AppExUtils.Json.ErrorHelper
alias Ecto.Multi
action_fallback ServicexMatchingWeb.FallbackController
def index(conn, %{"user_id" => user_id}) do
repo = Application.get_env(:servicex, :repo)
user_profile = Accounts.get_user_profile_by_user_id!(user_id)
user_profile
|> repo.preload([:organization, :user_profile_tags, :records])
|> Accounts.preload_user_profile_records()
render(conn, "show.json", user_profile: user_profile)
end
def index(conn, _params) do
repo = Application.get_env(:servicex, :repo)
user_profiles = Accounts.list_user_profiles()
render(conn, "index.json", user_profiles: user_profiles)
end
def create(conn, user_profile_params) do
repo = Application.get_env(:servicex, :repo)
with {:ok, result} <- Multi.new
|> Multi.run(:create_user_profile, Accounts, :create_user_profile, [user_profile_params])
|> repo.transaction() do
conn
|> put_status(:created)
|> put_resp_header("location", user_profile_path(conn, :show, result.create_user_profile))
|> render("show.json", user_profile: result.create_user_profile)
end
end
def show(conn, %{"id" => id}) do
user_profile = Accounts.get_user_profile!(id)
render(conn, "show.json", user_profile: user_profile)
end
def show_me(conn, _params) do
repo = Application.get_env(:servicex, :repo)
id = String.to_integer(conn.private.guardian_default_claims["sub"])
user_profile = Accounts.get_user_profile_by_user_id!(id)
user_profile = user_profile
|> repo.preload([:organization, :user_profile_tags, :records])
|> Accounts.preload_user_profile_records()
render(conn, "show.json", user_profile: user_profile)
end
def update(conn, user_profile_params) do
user_profile = Accounts.get_user_profile!(user_profile_params["id"])
with {:ok, %UserProfile{} = user_profile} <- Accounts.update_user_profile(user_profile, user_profile_params) do
render(conn, "show.json", user_profile: user_profile)
end
end
def edit_me(conn, user_profile_params) do
id = String.to_integer(conn.private.guardian_default_claims["sub"])
user_profile = Accounts.get_user_profile_by_user_id!(id)
with {:ok, %UserProfile{} = user_profile} <- Accounts.update_user_profile(user_profile, user_profile_params) do
render(conn, "show.json", user_profile: user_profile)
end
end
def delete(conn, %{"id" => id}) do
user_profile = Accounts.get_user_profile!(id)
with {:ok, %UserProfile{}} <- Accounts.delete_user_profile(user_profile) do
send_resp(conn, :no_content, "")
end
end
def create_joiner(conn, user_profile_params) do
Servicex.ControllerBase.transaction_flow(conn, :user_profile, ServicexMatching.Joiners, :create_joiner, [user_profile_params])
end
#def create_joiner2(conn, user_profile_params) do
#
# repo = Application.get_env(:servicex, :repo)
#
# try do
# with {:ok, result} <- Multi.new
# |> Multi.run(:create_joiner, Joiners, :create_joiner, [user_profile_params])
# |> repo.transaction() do
# conn
# |> put_status(:created)
# |> put_resp_header("location", user_profile_path(conn, :show, result.create_joiner))
# |> render("show.json", user_profile: result.create_joiner)
# else
# {:error, reason} ->
# conn
# |> put_status(:internal_server_error)
# |> send_resp(500, Poison.encode!(ErrorHelper.render("error.json", %{reason: reason})))
# end
# rescue
# e ->
# conn
# |> put_status(:internal_server_error)
# |> send_resp(500, Poison.encode!(ErrorHelper.render("error.json", %{reason: e})))
# end
#end
def update_joiner(conn, user_profile_params) do
repo = Application.get_env(:servicex, :repo)
user_id = String.to_integer(conn.private.guardian_default_claims["sub"])
user_profile = Accounts.get_user_profile!(user_id)
Servicex.ControllerBase.transaction_flow(conn, :user_profile, ServicexMatching.Joiners, :update_joiner, [user_profile, user_profile_params])
#with {:ok, result} <- Multi.new
#|> Multi.run(:update_joiner, Joiners, :update_joiner, [user_profile, user_profile_params])
#|> repo.transaction() do
# render(conn, "show.json", user_profile: update_joiner.user_profile)
#end
end
end