-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72afbc9
commit 086d795
Showing
18 changed files
with
1,062 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
lib/safira_web/live/backoffice/schedule_live/category_live/form_component.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.