implement creation of link on first step in backend

This commit is contained in:
2021-12-01 20:08:22 -05:00
parent 04225c44aa
commit 89c2a99283
9 changed files with 98 additions and 44 deletions

View File

@@ -5,6 +5,10 @@ defmodule Entendu.Links.Link do
schema "links" do
field :burn_after_reading, :boolean, default: false
field :expires, :utc_datetime
field :filename, :string
field :filetype, :string
field :text_content, :string
field :file_content, :string
timestamps()
end
@@ -12,7 +16,7 @@ defmodule Entendu.Links.Link do
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:expires, :burn_after_reading])
|> validate_required([:expires, :burn_after_reading])
|> cast(attrs, [:expires, :burn_after_reading, :filename, :filetype, :text_content, :file_content])
|> validate_required([:expires, :burn_after_reading, :filename, :filetype])
end
end

View File

@@ -4,22 +4,40 @@ defmodule EntenduWeb.LinkController do
"""
use EntenduWeb, :controller
use Params
alias Entendu.Links
alias Links.Link
alias Ecto.Changeset
def just_page(conn, _params) do
render(conn, "just.html")
end
def just(conn, %{encrypted_contents: contents}) do
conn
|> put_session(:encrypted_contents, contents)
|> redirect(to: "/just/for")
defparams first_step %{
burn_after_reading: [field: :boolean, default: false],
expires: :utc_datetime,
filename: :string,
filetype: :string,
text_content: :string,
file_content: :string
}
def just(conn, params) do
with %Changeset{valid?: true} = changeset <- first_step(params),
link_params <- Params.to_map(changeset),
{:ok, %Link{} = link} <- Links.create_link(link_params) do
conn
|> assign(:link, link)
|> render("show_authorized", link: link)
end
end
def for_page(conn, _params) do
render(conn, "for.html")
end
def for(conn, %{username: username, service: service}) do
def for(_conn, %{username: _username, service: _service}) do
{:error, "not implemented"}
end

View File

@@ -1,3 +1,22 @@
defmodule EntenduWeb.LinkView do
use EntenduWeb, :view
alias EntenduWeb.LinkView
def render("show_authorized.json", %{link: link}) do
%{
id: link.id,
burn_after_reading: link.burn_after_reading,
expires: link.expires,
filename: link.filename,
filetype: link.filetype,
text_content: link.text_content,
file_content: link.file_content
}
end
def render("show_unauthorized.json", %{link: link}) do
%{
id: link.id
}
end
end