This commit is contained in:
Pierre de Lacroix 2026-01-22 16:29:25 +01:00
parent 617c2ab725
commit c451662178
Signed by: lateralus23
GPG key ID: 53E0CEC29C24EF39
17 changed files with 602 additions and 2 deletions

View file

@ -0,0 +1,25 @@
defmodule CampApiWeb.ChangesetJSON do
@doc """
Renders changeset errors.
"""
def error(%{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
end
defp translate_error({msg, opts}) do
# You can make use of gettext to translate error messages by
# uncommenting and adjusting the following code:
# if count = opts[:count] do
# Gettext.dngettext(CampApiWeb.Gettext, "errors", msg, msg, count, opts)
# else
# Gettext.dgettext(CampApiWeb.Gettext, "errors", msg, opts)
# end
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
end)
end
end

View file

@ -0,0 +1,16 @@
defmodule CampApiWeb.FallbackController do
@moduledoc """
Translates controller action results into valid `Plug.Conn` responses.
See `Phoenix.Controller.action_fallback/1` for more details.
"""
use CampApiWeb, :controller
# This clause is an example of how to handle resources that cannot be found.
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> put_view(html: CampApiWeb.ErrorHTML, json: CampApiWeb.ErrorJSON)
|> render(:"404")
end
end

View file

@ -0,0 +1,58 @@
defmodule CampApiWeb.PaymentLinkController do
use CampApiWeb, :controller
alias CampApi.PaymentLinks
alias CampApi.PaymentLinks.Link
alias CampApi.PaymentLinks.Qonto
action_fallback CampApiWeb.FallbackController
def qonto_auth(conn, %{"code" => code, "state" => state}) do
case Qonto.create_token(code, state) do
:ok ->
conn
# |> put_resp_content_type
|> send_resp(200, "connected")
:error ->
conn
# |> put_resp_content_type
|> send_resp(500, "error")
end
end
# def index(conn, _params) do
# links = PaymentLinks.list_links()
# render(conn, :index, links: links)
# end
def create(conn, %{"link" => link_params}) do
with {:ok, %Link{} = link} <- PaymentLinks.create_link(link_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/links/#{link}")
|> render(:show, link: link)
end
end
# def show(conn, %{"id" => id}) do
# link = PaymentLinks.get_link!(id)
# render(conn, :show, link: link)
# end
# def update(conn, %{"id" => id, "link" => link_params}) do
# link = PaymentLinks.get_link!(id)
#
# with {:ok, %Link{} = link} <- PaymentLinks.update_link(link, link_params) do
# render(conn, :show, link: link)
# end
# end
# def delete(conn, %{"id" => id}) do
# link = PaymentLinks.get_link!(id)
#
# with {:ok, %Link{}} <- PaymentLinks.delete_link(link) do
# send_resp(conn, :no_content, "")
# end
# end
end

View file

@ -0,0 +1,24 @@
defmodule CampApiWeb.PaymentLinkJSON do
alias CampApi.PaymentLinks.Link
@doc """
Renders a list of links.
"""
def index(%{links: links}) do
%{data: for(link <- links, do: data(link))}
end
@doc """
Renders a single link.
"""
def show(%{link: link}) do
%{data: data(link)}
end
defp data(%Link{} = link) do
%{
id: link.id,
price: link.price
}
end
end

View file

@ -7,6 +7,10 @@ defmodule CampApiWeb.Router do
scope "/api", CampApiWeb do
pipe_through :api
resources "/payment_links", PaymentLinkController, only: [:create]
get "/qonto_auth", PaymentLinkController, :qonto_auth
end
# Enable LiveDashboard in development