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

@@ -0,0 +1,27 @@
defmodule Eventos.Events.Comment do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.Event
alias Eventos.Accounts.Account
alias Eventos.Accounts.Comment
schema "comments" do
field :text, :string
field :url, :string
field :local, :boolean, default: true
belongs_to :account, Account, [foreign_key: :account_id]
belongs_to :event, Event, [foreign_key: :event_id]
belongs_to :in_reply_to_comment, Comment, [foreign_key: :in_reply_to_comment_id]
belongs_to :origin_comment, Comment, [foreign_key: :origin_comment_id]
timestamps()
end
@doc false
def changeset(comment, attrs) do
comment
|> cast(attrs, [:url, :text, :account_id, :event_id, :in_reply_to_comment_id])
|> validate_required([:url, :text, :account_id])
end
end

View File

@@ -39,6 +39,8 @@ defmodule Eventos.Events.Event do
alias Eventos.Addresses.Address
schema "events" do
field :url, :string
field :local, :boolean, default: true
field :begins_on, Timex.Ecto.DateTimeWithTimezone
field :description, :string
field :ends_on, Timex.Ecto.DateTimeWithTimezone
@@ -60,13 +62,13 @@ defmodule Eventos.Events.Event do
has_many :sessions, Session
belongs_to :address, Address
timestamps()
timestamps(type: :utc_datetime)
end
@doc false
def changeset(%Event{} = event, attrs) do
event
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|> cast(attrs, [:title, :description, :url, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|> cast_assoc(:tags)
|> cast_assoc(:address)
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_account_id, :category_id])

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