Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-05-17 11:32:23 +02:00
parent 2c1abe5e19
commit e14007bac5
45 changed files with 2916 additions and 111 deletions

View File

@@ -7,6 +7,7 @@ defmodule Eventos.Events do
alias Eventos.Repo
alias Eventos.Events.Event
alias Eventos.Events.Comment
alias Eventos.Accounts.Account
@doc """
@@ -22,6 +23,36 @@ defmodule Eventos.Events do
Repo.all(Event)
end
def get_events_for_account(%Account{id: account_id} = _account, page \\ 1, limit \\ 10) do
start = (page - 1) * limit
query = from e in Event,
where: e.organizer_account_id == ^account_id,
limit: ^limit,
order_by: [desc: :id],
offset: ^start,
preload: [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address]
events = Repo.all(query)
count_events = Repo.one(from e in Event, select: count(e.id))
{:ok, events, count_events}
end
def count_local_events do
Repo.one(
from e in Event,
select: count(e.id),
where: e.local == ^true
)
end
def count_local_comments do
Repo.one(
from c in Comment,
select: count(c.id),
where: c.local == ^true
)
end
@doc """
Gets a single event.
@@ -38,6 +69,13 @@ defmodule Eventos.Events do
"""
def get_event!(id), do: Repo.get!(Event, id)
@doc """
Gets an event by it's URL
"""
def get_event_by_url!(url) do
Repo.get_by(Event, url: url)
end
@doc """
Gets a single event, with all associations loaded.
"""
@@ -46,6 +84,25 @@ defmodule Eventos.Events do
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@doc """
Gets an event by it's URL
"""
def get_event_full_by_url!(url) do
event = Repo.get_by(Event, url: url)
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@spec get_event_full_by_username_and_slug!(String.t, String.t) :: Event.t
def get_event_full_by_username_and_slug!(username, slug) do
event = Repo.one(
from e in Event,
join: a in Account,
on: a.id == e.organizer_account_id and a.username == ^username,
where: e.slug == ^slug
)
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@doc """
Creates a event.
@@ -706,4 +763,100 @@ defmodule Eventos.Events do
def change_track(%Track{} = track) do
Track.changeset(track, %{})
end
alias Eventos.Events.Comment
@doc """
Returns the list of comments.
## Examples
iex> list_comments()
[%Comment{}, ...]
"""
def list_comments do
Repo.all(Comment)
end
@doc """
Gets a single comment.
Raises `Ecto.NoResultsError` if the Comment does not exist.
## Examples
iex> get_comment!(123)
%Comment{}
iex> get_comment!(456)
** (Ecto.NoResultsError)
"""
def get_comment!(id), do: Repo.get!(Comment, id)
@doc """
Creates a comment.
## Examples
iex> create_comment(%{field: value})
{:ok, %Comment{}}
iex> create_comment(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_comment(attrs \\ %{}) do
%Comment{}
|> Comment.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a comment.
## Examples
iex> update_comment(comment, %{field: new_value})
{:ok, %Comment{}}
iex> update_comment(comment, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_comment(%Comment{} = comment, attrs) do
comment
|> Comment.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Comment.
## Examples
iex> delete_comment(comment)
{:ok, %Comment{}}
iex> delete_comment(comment)
{:error, %Ecto.Changeset{}}
"""
def delete_comment(%Comment{} = comment) do
Repo.delete(comment)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking comment changes.
## Examples
iex> change_comment(comment)
%Ecto.Changeset{source: %Comment{}}
"""
def change_comment(%Comment{} = comment) do
Comment.changeset(comment, %{})
end
end