Skip to content

Commit

Permalink
feat: add activities
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo committed Nov 1, 2024
1 parent 72afbc9 commit 086d795
Show file tree
Hide file tree
Showing 18 changed files with 1,062 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/safira/accounts/roles/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Safira.Accounts.Roles.Permissions do
"badges" => ["show", "edit", "delete", "give", "revoke", "give_without_restrictions"],
"minigames" => ["show", "edit", "simulate"],
"spotlights" => ["edit"],
"schedule" => ["edit"],
"statistics" => ["show"],
"mailer" => ["send"]
}
Expand Down
216 changes: 216 additions & 0 deletions lib/safira/activities.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
defmodule Safira.Activities do
@moduledoc """
The Activities context.
"""

use Safira.Context

alias Safira.Activities.Activity

@doc """
Returns the list of activities.
## Examples
iex> list_activities()
[%Activity{}, ...]
"""
def list_activities do
Repo.all(Activity)
end

def list_activities(opts) when is_list(opts) do
Activity
|> apply_filters(opts)
|> Repo.all()
end

def list_activities(params) do
Activity
|> Flop.validate_and_run(params, for: Activity)
end

def list_activities(%{} = params, opts) when is_list(opts) do
Activity
|> apply_filters(opts)
|> Flop.validate_and_run(params, for: Activity)
end

@doc """
Gets a single activity.
Raises `Ecto.NoResultsError` if the Activity does not exist.
## Examples
iex> get_activity!(123)
%Activity{}
iex> get_activity!(456)
** (Ecto.NoResultsError)
"""
def get_activity!(id), do: Repo.get!(Activity, id)

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

@doc """
Updates a activity.
## Examples
iex> update_activity(activity, %{field: new_value})
{:ok, %Activity{}}
iex> update_activity(activity, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_activity(%Activity{} = activity, attrs) do
activity
|> Activity.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a activity.
## Examples
iex> delete_activity(activity)
{:ok, %Activity{}}
iex> delete_activity(activity)
{:error, %Ecto.Changeset{}}
"""
def delete_activity(%Activity{} = activity) do
Repo.delete(activity)
end

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

alias Safira.Activities.ActivityCategory

@doc """
Returns the list of activity_categories.
## Examples
iex> list_activity_categories()
[%ActivityCategory{}, ...]
"""
def list_activity_categories do
Repo.all(ActivityCategory)
end

@doc """
Gets a single activity_category.
Raises `Ecto.NoResultsError` if the Activity category does not exist.
## Examples
iex> get_activity_category!(123)
%ActivityCategory{}
iex> get_activity_category!(456)
** (Ecto.NoResultsError)
"""
def get_activity_category!(id), do: Repo.get!(ActivityCategory, id)

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

@doc """
Updates a activity_category.
## Examples
iex> update_activity_category(activity_category, %{field: new_value})
{:ok, %ActivityCategory{}}
iex> update_activity_category(activity_category, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_activity_category(%ActivityCategory{} = activity_category, attrs) do
activity_category
|> ActivityCategory.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a activity_category.
## Examples
iex> delete_activity_category(activity_category)
{:ok, %ActivityCategory{}}
iex> delete_activity_category(activity_category)
{:error, %Ecto.Changeset{}}
"""
def delete_activity_category(%ActivityCategory{} = activity_category) do
Repo.delete(activity_category)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking activity_category changes.
## Examples
iex> change_activity_category(activity_category)
%Ecto.Changeset{data: %ActivityCategory{}}
"""
def change_activity_category(%ActivityCategory{} = activity_category, attrs \\ %{}) do
ActivityCategory.changeset(activity_category, attrs)
end
end
35 changes: 35 additions & 0 deletions lib/safira/activities/activity.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Safira.Activities.Activity do
@moduledoc """
Activities scheduled for the event.
"""
use Safira.Schema

@required_fields ~w(title date time_start time_end)a
@optional_fields ~w(description category_id location has_enrolments)a

@derive {
Flop.Schema,
filterable: [:title], sortable: [:title, :date], default_limit: 11
}

schema "activities" do
field :title, :string
field :description, :string
field :location, :string
field :date, :date
field :time_start, :time
field :time_end, :time
field :has_enrolments, :boolean, default: false

belongs_to :category, Safira.Activities.ActivityCategory

timestamps(type: :utc_datetime)
end

@doc false
def changeset(activity, attrs) do
activity
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
21 changes: 21 additions & 0 deletions lib/safira/activities/activity_category.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Safira.Activities.ActivityCategory do
@moduledoc """
Categories for activities.
"""
use Safira.Schema

@required_fields ~w(name)a

schema "activity_categories" do
field :name, :string

timestamps(type: :utc_datetime)
end

@doc false
def changeset(activity_category, attrs) do
activity_category
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end
end
2 changes: 1 addition & 1 deletion lib/safira_web/components/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule SafiraWeb.Components.Table do
</tbody>
</table>
</div>
<.pagination meta={@meta} params={@params} />
<.pagination :if={@meta.total_pages > 1} meta={@meta} params={@params} />
</div>
"""
end
Expand Down
7 changes: 7 additions & 0 deletions lib/safira_web/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ defmodule SafiraWeb.Config do
url: "/dashboard/spotlights",
scope: %{"spotlights" => ["edit"]}
},
%{
key: :schedule,
title: "Schedule",
icon: "hero-calendar-days",
url: "/dashboard/schedule/activities",
scope: %{"schedule" => ["edit"]}
},
%{
key: :statistics,
title: "Statistics",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule SafiraWeb.Backoffice.ScheduleLive.CategoryLive.FormComponent do
use SafiraWeb, :live_component

alias Safira.Activities
import SafiraWeb.Components.Forms

@impl true
def render(assigns) do
~H"""
<div>
<.page title={@title} subtitle={gettext("Categories group scheduled activities.")}>
<.simple_form
for={@form}
id="category-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<div class="w-full">
<.field field={@form[:name]} type="text" label="Name" required />
</div>
<:actions>
<.button phx-disable-with="Saving...">Save Category</.button>
</:actions>
</.simple_form>
</.page>
</div>
"""
end

@impl true
def mount(socket) do
{:ok, socket}
end

@impl true
def update(%{category: category} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign_new(:form, fn ->
to_form(Activities.change_activity_category(category))
end)}
end

@impl true
def handle_event("validate", %{"activity_category" => category_params}, socket) do
changeset = Activities.change_activity_category(socket.assigns.category, category_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end

def handle_event("save", %{"activity_category" => category_params}, socket) do
save_category(socket, socket.assigns.action, category_params)
end

defp save_category(socket, :categories_edit, category_params) do
case Activities.update_activity_category(socket.assigns.category, category_params) do
{:ok, _category} ->
{:noreply,
socket
|> put_flash(:info, "Activity category updated successfully")
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end

defp save_category(socket, :categories_new, category_params) do
case Activities.create_activity_category(category_params) do
{:ok, _category} ->
{:noreply,
socket
|> put_flash(:info, "Activity category created successfully")
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
end
Loading

0 comments on commit 086d795

Please sign in to comment.