Skip to content

Commit

Permalink
feat: Add Embed #61 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 authored Nov 23, 2023
1 parent c4b91b0 commit d398906
Show file tree
Hide file tree
Showing 26 changed files with 1,627 additions and 284 deletions.
189 changes: 189 additions & 0 deletions lib/claper/embeds.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
defmodule Claper.Embeds do
@moduledoc """
The Embeds context.
"""

import Ecto.Query, warn: false
alias Claper.Repo

alias Claper.Embeds.Embed

@doc """
Returns the list of embeds for a given presentation file.
## Examples
iex> list_embeds(123)
[%Embed{}, ...]
"""
def list_embeds(presentation_file_id) do
from(e in Embed,
where: e.presentation_file_id == ^presentation_file_id,
order_by: [asc: e.id, asc: e.position]
)
|> Repo.all()
end

@doc """
Returns the list of embeds for a given presentation file and a given position.
## Examples
iex> list_embeds_at_position(123, 0)
[%Embed{}, ...]
"""
def list_embeds_at_position(presentation_file_id, position) do
from(e in Embed,
where: e.presentation_file_id == ^presentation_file_id and e.position == ^position,
order_by: [asc: e.id]
)
|> Repo.all()
end

@doc """
Gets a single embed.
Raises `Ecto.NoResultsError` if the Embed does not exist.
## Examples
iex> get_embed!(123)
%Embed{}
iex> get_embed!(456)
** (Ecto.NoResultsError)
"""
def get_embed!(id, preload \\ []),
do: Repo.get!(Embed, id) |> Repo.preload(preload)

@doc """
Gets a single embed for a given position.
## Examples
iex> get_embed_current_position(123, 0)
%Embed{}
"""
def get_embed_current_position(presentation_file_id, position) do
from(e in Embed,
where:
e.position == ^position and e.presentation_file_id == ^presentation_file_id and
e.enabled == true
)
|> Repo.one()
end

@doc """
Creates a embed.
## Examples
iex> create_embed(%{field: value})
{:ok, %Embed{}}
iex> create_embed(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_embed(attrs \\ %{}) do
%Embed{}
|> Embed.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a embed.
## Examples
iex> update_embed("123e4567-e89b-12d3-a456-426614174000", embed, %{field: new_value})
{:ok, %Embed{}}
iex> update_embed("123e4567-e89b-12d3-a456-426614174000", embed, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_embed(event_uuid, %Embed{} = embed, attrs) do
embed
|> Embed.changeset(attrs)
|> Repo.update()
|> case do
{:ok, embed} ->
broadcast({:ok, embed, event_uuid}, :embed_updated)

{:error, changeset} ->
{:error, %{changeset | action: :update}}
end
end

@doc """
Deletes a embed.
## Examples
iex> delete_embed("123e4567-e89b-12d3-a456-426614174000", embed)
{:ok, %Embed{}}
iex> delete_embed("123e4567-e89b-12d3-a456-426614174000", embed)
{:error, %Ecto.Changeset{}}
"""
def delete_embed(event_uuid, %Embed{} = embed) do
{:ok, embed} = Repo.delete(embed)
broadcast({:ok, embed, event_uuid}, :embed_deleted)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking embed changes.
## Examples
iex> change_embed(embed)
%Ecto.Changeset{data: %Embed{}}
"""
def change_embed(%Embed{} = embed, attrs \\ %{}) do
Embed.changeset(embed, attrs)
end

def disable_all(presentation_file_id, position) do
from(e in Embed,
where: e.presentation_file_id == ^presentation_file_id and e.position == ^position
)
|> Repo.update_all(set: [enabled: false])
end

def set_status(id, presentation_file_id, position, status) do
if status do
from(e in Embed,
where:
e.presentation_file_id == ^presentation_file_id and e.position == ^position and
e.id != ^id
)
|> Repo.update_all(set: [enabled: false])
end

from(e in Embed,
where:
e.presentation_file_id == ^presentation_file_id and e.position == ^position and
e.id == ^id
)
|> Repo.update_all(set: [enabled: status])
end

defp broadcast({:error, _reason} = error, _embed), do: error

defp broadcast({:ok, embed, event_uuid}, event) do
Phoenix.PubSub.broadcast(
Claper.PubSub,
"event:#{event_uuid}",
{event, embed}
)

{:ok, embed}
end
end
38 changes: 38 additions & 0 deletions lib/claper/embeds/embed.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Claper.Embeds.Embed do
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:title, :content, :position, :attendee_visibility]}
schema "embeds" do
field :title, :string
field :content, :string
field :enabled, :boolean, default: true
field :position, :integer, default: 0
field :attendee_visibility, :boolean, default: false

belongs_to :presentation_file, Claper.Presentations.PresentationFile

timestamps()
end

@doc false
def changeset(embed, attrs \\ %{}) do
embed
|> cast(attrs, [
:enabled,
:title,
:content,
:presentation_file_id,
:position,
:attendee_visibility
])
|> validate_required([
:title,
:content,
:presentation_file_id,
:position,
:attendee_visibility
])
|> validate_format(:content, ~r/<iframe.*<\/iframe>/, message: "Invalid embed format")
end
end
21 changes: 19 additions & 2 deletions lib/claper/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,12 @@ defmodule Claper.Events do
|> Ecto.Multi.run(:from_event, fn _repo, _changes ->
{:ok,
get_user_event!(user_id, from_event_uuid,
presentation_file: [polls: [:poll_opts], forms: []]
presentation_file: [polls: [:poll_opts], forms: [], embeds: []]
)}
end)
|> Ecto.Multi.run(:to_event, fn _repo, _changes ->
{:ok, get_user_event!(user_id, to_event_uuid, presentation_file: [:polls, :forms])}
{:ok,
get_user_event!(user_id, to_event_uuid, presentation_file: [:polls, :forms, :embeds])}
end)
|> Ecto.Multi.run(:polls, fn _repo, %{from_event: from_event, to_event: to_event} ->
{:ok,
Expand Down Expand Up @@ -321,6 +322,22 @@ defmodule Claper.Events do
end
end)}
end)
|> Ecto.Multi.run(:embeds, fn _repo, %{from_event: from_event, to_event: to_event} ->
{:ok,
from_event.presentation_file.embeds
|> Enum.each(fn embed ->
if embed.position < to_event.presentation_file.length do
Claper.Embeds.create_embed(%{
title: embed.title,
content: embed.content,
position: embed.position,
enabled: embed.enabled,
attendee_visibility: embed.attendee_visibility,
presentation_file_id: to_event.presentation_file.id
})
end
end)}
end)
|> Repo.transaction() do
{:ok, %{to_event: to_event}} -> {:ok, to_event}
end
Expand Down
18 changes: 10 additions & 8 deletions lib/claper/forms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,22 @@ defmodule Claper.Forms do
|> Repo.update_all(set: [enabled: false])
end

def set_default(id, presentation_file_id, position) do
from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id != ^id
)
|> Repo.update_all(set: [enabled: false])
def set_status(id, presentation_file_id, position, status) do
if status do
from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id != ^id
)
|> Repo.update_all(set: [enabled: false])
end

from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id == ^id
)
|> Repo.update_all(set: [enabled: true])
|> Repo.update_all(set: [enabled: status])
end

defp broadcast({:error, _reason} = error, _form), do: error
Expand Down
18 changes: 10 additions & 8 deletions lib/claper/polls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,22 @@ defmodule Claper.Polls do
|> Repo.update_all(set: [enabled: false])
end

def set_default(id, presentation_file_id, position) do
from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id != ^id
)
|> Repo.update_all(set: [enabled: false])
def set_status(id, presentation_file_id, position, status) do
if status do
from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id != ^id
)
|> Repo.update_all(set: [enabled: false])
end

from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id == ^id
)
|> Repo.update_all(set: [enabled: true])
|> Repo.update_all(set: [enabled: status])
end

defp broadcast({:error, _reason} = error, _poll), do: error
Expand Down
1 change: 1 addition & 0 deletions lib/claper/presentations/presentation_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Claper.Presentations.PresentationFile do
belongs_to :event, Claper.Events.Event
has_many :polls, Claper.Polls.Poll
has_many :forms, Claper.Forms.Form
has_many :embeds, Claper.Embeds.Embed
has_one :presentation_state, Claper.Presentations.PresentationState, on_replace: :delete

timestamps()
Expand Down
Loading

0 comments on commit d398906

Please sign in to comment.