initial commit

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
master
Thomas Citharel 5 years ago
commit 90ceb4f6fe

16
.gitignore vendored

@ -0,0 +1,16 @@
# App artifacts
/_build
/db
/deps
/*.ez
# Generated on crash by the VM
erl_crash.dump
# Files matching config/*.secret.exs pattern contain sensitive
# data and you should not commit them into version control.
#
# Alternatively, you may comment the line below and commit the
# secrets files as long as you replace their contents by environment
# variables.
/config/*.secret.exs

@ -0,0 +1,19 @@
# Eventos
To start your Phoenix server:
* Install dependencies with `mix deps.get`
* Create and migrate your database with `mix ecto.create && mix ecto.migrate`
* Start Phoenix endpoint with `mix phx.server`
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
Ready to run in production? Please [check our deployment guides](http://www.phoenixframework.org/docs/deployment).
## Learn more
* Official website: http://www.phoenixframework.org/
* Guides: http://phoenixframework.org/docs/overview
* Docs: https://hexdocs.pm/phoenix
* Mailing list: http://groups.google.com/group/phoenix-talk
* Source: https://github.com/phoenixframework/phoenix

@ -0,0 +1,46 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.
use Mix.Config
# General application configuration
config :eventos,
ecto_repos: [Eventos.Repo]
# Configures the endpoint
config :eventos, EventosWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "1yOazsoE0Wqu4kXk3uC5gu3jDbShOimTCzyFL3OjCdBmOXMyHX87Qmf3+Tu9s0iM",
render_errors: [view: EventosWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Eventos.PubSub,
adapter: Phoenix.PubSub.PG2]
# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
# %% Coherence Configuration %% Don't remove this line
config :coherence,
user_schema: Eventos.Accounts.User,
repo: Eventos.Repo,
module: Eventos,
web_module: EventosWeb,
router: EventosWeb.Router,
messages_backend: EventosWeb.Coherence.Messages,
logged_out_url: "/",
user_active_field: true,
email_from_name: "Your Name",
email_from_email: "yourname@example.com",
opts: [:invitable, :confirmable, :rememberable, :authenticatable, :recoverable, :lockable, :trackable, :unlockable_with_token, :registerable]
config :coherence, EventosWeb.Coherence.Mailer,
adapter: Swoosh.Adapters.Sendgrid,
api_key: "your api key here"
# %% End Coherence Configuration %%

@ -0,0 +1,57 @@
use Mix.Config
# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with brunch.io to recompile .js and .css sources.
config :eventos, EventosWeb.Endpoint,
http: [port: 4000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: []
# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# command from your terminal:
#
# openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
#
# The `http:` config above can be replaced with:
#
# https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.
# Watch static and templates for browser reloading.
config :eventos, EventosWeb.Endpoint,
live_reload: [
patterns: [
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
~r{priv/gettext/.*(po)$},
~r{lib/eventos_web/views/.*(ex)$},
~r{lib/eventos_web/templates/.*(eex)$}
]
]
# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n", level: :debug
# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20
# Configure your database
config :eventos, Eventos.Repo,
adapter: Ecto.Adapters.Postgres,
username: "elixir",
password: "elixir",
database: "eventos_dev",
hostname: "localhost",
pool_size: 10

@ -0,0 +1,64 @@
use Mix.Config
# For production, we often load configuration from external
# sources, such as your system environment. For this reason,
# you won't find the :http configuration below, but set inside
# EventosWeb.Endpoint.init/2 when load_from_system_env is
# true. Any dynamic configuration should be done there.
#
# Don't forget to configure the url host to something meaningful,
# Phoenix uses this information when generating URLs.
#
# Finally, we also include the path to a cache manifest
# containing the digested version of static files. This
# manifest is generated by the mix phx.digest task
# which you typically run after static files are built.
config :eventos, EventosWeb.Endpoint,
load_from_system_env: true,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"
# Do not print debug messages in production
config :logger, level: :info
# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
# to the previous section and set your `:url` port to 443:
#
# config :eventos, EventosWeb.Endpoint,
# ...
# url: [host: "example.com", port: 443],
# https: [:inet6,
# port: 443,
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
#
# Where those two env variables return an absolute path to
# the key and cert in disk or a relative path inside priv,
# for example "priv/ssl/server.key".
#
# We also recommend setting `force_ssl`, ensuring no data is
# ever sent via http, always redirecting to https:
#
# config :eventos, EventosWeb.Endpoint,
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.
# ## Using releases
#
# If you are doing OTP releases, you need to instruct Phoenix
# to start the server for all endpoints:
#
# config :phoenix, :serve_endpoints, true
#
# Alternatively, you can configure exactly which server to
# start per endpoint:
#
# config :eventos, EventosWeb.Endpoint, server: true
#
# Finally import the config/prod.secret.exs
# which should be versioned separately.
import_config "prod.secret.exs"

@ -0,0 +1,19 @@
use Mix.Config
# We don't run a server during test. If one is required,
# you can enable the server option below.
config :eventos, EventosWeb.Endpoint,
http: [port: 4001],
server: false
# Print only warnings and errors during test
config :logger, level: :warn
# Configure your database
config :eventos, Eventos.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "eventos_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox

@ -0,0 +1,9 @@
defmodule Eventos do
@moduledoc """
Eventos keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end

@ -0,0 +1,32 @@
defmodule Eventos.Accounts.Account do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts.{Account, GroupAccount, GroupRequest, Group, User}
alias Eventos.Events.Event
schema "accounts" do
field :description, :string
field :display_name, :string
field :domain, :string
field :private_key, :string
field :public_key, :string
field :suspended, :boolean, default: false
field :uri, :string
field :url, :string
field :username, :string
has_many :organized_events, Event
many_to_many :groups, Group, join_through: GroupAccount
has_many :group_request, GroupRequest
has_one :user_id, User
timestamps()
end
@doc false
def changeset(%Account{} = account, attrs) do
account
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url])
|> validate_required([:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url])
end
end

@ -0,0 +1,488 @@
defmodule Eventos.Accounts do
@moduledoc """
The Accounts context.
"""
import Ecto.Query, warn: false
alias Eventos.Repo
alias Eventos.Accounts.User
@doc """
Returns the list of users.
## Examples
iex> list_users()
[%User{}, ...]
"""
def list_users do
Repo.all(User)
end
@doc """
Gets a single user.
Raises `Ecto.NoResultsError` if the User does not exist.
## Examples
iex> get_user!(123)
%User{}
iex> get_user!(456)
** (Ecto.NoResultsError)
"""
def get_user!(id), do: Repo.get!(User, id)
@doc """
Creates a user.
## Examples
iex> create_user(%{field: value})
{:ok, %User{}}
iex> create_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a user.
## Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a User.
## Examples
iex> delete_user(user)
{:ok, %User{}}
iex> delete_user(user)
{:error, %Ecto.Changeset{}}
"""
def delete_user(%User{} = user) do
Repo.delete(user)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.
## Examples
iex> change_user(user)
%Ecto.Changeset{source: %User{}}
"""
def change_user(%User{} = user) do
User.changeset(user, %{})
end
alias Eventos.Accounts.Account
@doc """
Returns the list of accounts.
## Examples
iex> list_accounts()
[%Account{}, ...]
"""
def list_accounts do
Repo.all(Account)
end
@doc """
Gets a single account.
Raises `Ecto.NoResultsError` if the Account does not exist.
## Examples
iex> get_account!(123)
%Account{}
iex> get_account!(456)
** (Ecto.NoResultsError)
"""
def get_account!(id), do: Repo.get!(Account, id)
@doc """
Creates a account.
## Examples
iex> create_account(%{field: value})
{:ok, %Account{}}
iex> create_account(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_account(attrs \\ %{}) do
%Account{}
|> Account.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a account.
## Examples
iex> update_account(account, %{field: new_value})
{:ok, %Account{}}
iex> update_account(account, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_account(%Account{} = account, attrs) do
account
|> Account.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Account.
## Examples
iex> delete_account(account)
{:ok, %Account{}}
iex> delete_account(account)
{:error, %Ecto.Changeset{}}
"""
def delete_account(%Account{} = account) do
Repo.delete(account)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking account changes.
## Examples
iex> change_account(account)
%Ecto.Changeset{source: %Account{}}
"""
def change_account(%Account{} = account) do
Account.changeset(account, %{})
end
alias Eventos.Accounts.Group
@doc """
Returns the list of groups.
## Examples
iex> list_groups()
[%Group{}, ...]
"""
def list_groups do
Repo.all(Group)
end
@doc """
Gets a single group.
Raises `Ecto.NoResultsError` if the Group does not exist.
## Examples
iex> get_group!(123)
%Group{}
iex> get_group!(456)
** (Ecto.NoResultsError)
"""
def get_group!(id), do: Repo.get!(Group, id)
@doc """
Creates a group.
## Examples
iex> create_group(%{field: value})
{:ok, %Group{}}
iex> create_group(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_group(attrs \\ %{}) do
%Group{}
|> Group.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a group.
## Examples
iex> update_group(group, %{field: new_value})
{:ok, %Group{}}
iex> update_group(group, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_group(%Group{} = group, attrs) do
group
|> Group.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Group.
## Examples
iex> delete_group(group)
{:ok, %Group{}}
iex> delete_group(group)
{:error, %Ecto.Changeset{}}
"""
def delete_group(%Group{} = group) do
Repo.delete(group)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking group changes.
## Examples
iex> change_group(group)
%Ecto.Changeset{source: %Group{}}
"""
def change_group(%Group{} = group) do
Group.changeset(group, %{})
end
alias Eventos.Accounts.GroupAccount
@doc """
Returns the list of group_accounts.
## Examples
iex> list_group_accounts()
[%GroupAccount{}, ...]
"""
def list_group_accounts do
Repo.all(GroupAccount)
end
@doc """
Gets a single group_account.
Raises `Ecto.NoResultsError` if the Group account does not exist.
## Examples
iex> get_group_account!(123)
%GroupAccount{}
iex> get_group_account!(456)
** (Ecto.NoResultsError)
"""
def get_group_account!(id), do: Repo.get!(GroupAccount, id)
@doc """
Creates a group_account.
## Examples
iex> create_group_account(%{field: value})
{:ok, %GroupAccount{}}
iex> create_group_account(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_group_account(attrs \\ %{}) do
%GroupAccount{}
|> GroupAccount.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a group_account.
## Examples
iex> update_group_account(group_account, %{field: new_value})
{:ok, %GroupAccount{}}
iex> update_group_account(group_account, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_group_account(%GroupAccount{} = group_account, attrs) do
group_account
|> GroupAccount.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a GroupAccount.
## Examples
iex> delete_group_account(group_account)
{:ok, %GroupAccount{}}
iex> delete_group_account(group_account)
{:error, %Ecto.Changeset{}}
"""
def delete_group_account(%GroupAccount{} = group_account) do
Repo.delete(group_account)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking group_account changes.
## Examples
iex> change_group_account(group_account)
%Ecto.Changeset{source: %GroupAccount{}}
"""
def change_group_account(%GroupAccount{} = group_account) do
GroupAccount.changeset(group_account, %{})
end
alias Eventos.Accounts.GroupRequest
@doc """
Returns the list of group_request.
## Examples
iex> list_group_requests()
[%GroupRequest{}, ...]
"""
def list_group_requests do
Repo.all(GroupRequest)
end
@doc """
Gets a single group_request.
Raises `Ecto.NoResultsError` if the Group request does not exist.
## Examples
iex> get_group_request!(123)
%GroupRequest{}
iex> get_group_request!(456)
** (Ecto.NoResultsError)
"""
def get_group_request!(id), do: Repo.get!(GroupRequest, id)
@doc """
Creates a group_request.
## Examples
iex> create_group_request(%{field: value})
{:ok, %GroupRequest{}}
iex> create_group_request(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_group_request(attrs \\ %{}) do
%GroupRequest{}
|> GroupRequest.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a group_request.
## Examples
iex> update_group_request(group_request, %{field: new_value})
{:ok, %GroupRequest{}}
iex> update_group_request(group_request, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_group_request(%GroupRequest{} = group_request, attrs) do
group_request
|> GroupRequest.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a GroupRequest.
## Examples
iex> delete_group_request(group_request)
{:ok, %GroupRequest{}}
iex> delete_group_request(group_request)
{:error, %Ecto.Changeset{}}
"""
def delete_group_request(%GroupRequest{} = group_request) do
Repo.delete(group_request)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking group_request changes.
## Examples
iex> change_group_request(group_request)
%Ecto.Changeset{source: %GroupRequest{}}
"""
def change_group_request(%GroupRequest{} = group_request) do
GroupRequest.changeset(group_request, %{})
end
end

@ -0,0 +1,25 @@
defmodule Eventos.Accounts.Group do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts.{Group, Account, GroupAccount, GroupRequest}
schema "groups" do
field :description, :string
field :suspended, :boolean, default: false
field :title, :string
field :uri, :string
field :url, :string
many_to_many :accounts, Account, join_through: GroupAccount
has_many :requests, GroupRequest
timestamps()
end
@doc false
def changeset(%Group{} = group, attrs) do
group
|> cast(attrs, [:title, :description, :suspended, :url, :uri])
|> validate_required([:title, :description, :suspended, :url, :uri])
end
end

@ -0,0 +1,21 @@
defmodule Eventos.Accounts.GroupAccount do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts.{GroupAccount, Account, Group}
@primary_key false
schema "group_accounts" do
field :role, :integer
belongs_to :group, Group
belongs_to :account, Account
timestamps()
end
@doc false
def changeset(%GroupAccount{} = group_account, attrs) do
group_account
|> cast(attrs, [:role])
|> validate_required([:role])
end
end

@ -0,0 +1,20 @@
defmodule Eventos.Accounts.GroupRequest do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts.{GroupRequest}
schema "group_requests" do
field :state, :integer
field :group_id, :integer
field :account_id, :integer
timestamps()
end
@doc false
def changeset(%GroupRequest{} = group_request, attrs) do
group_request
|> cast(attrs, [:state])
|> validate_required([:state])
end
end

@ -0,0 +1,43 @@
defmodule Eventos.Accounts.User do
use Ecto.Schema
use Coherence.Schema
import Ecto.Changeset
alias Eventos.Accounts.{User}
schema "users" do
field :email, :string
field :role, :integer, default: 0
field :username, :string
field :account_id, :integer
coherence_schema()
timestamps()
end
def changeset(user, attrs, :password) do
user
|> cast(attrs, ~w(password password_confirmation reset_password_token reset_password_sent_at))
|> validate_coherence_password_reset(attrs)
end
def changeset(user, attrs, :registration) do
user
|> cast(attrs, [:username, :email] ++ coherence_fields())
|> validate_required([:username, :email])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:username)
|> validate_coherence(attrs)
end
@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:username, :email, :password_hash, :role] ++ coherence_fields())
|> validate_required([:username, :email])
|> unique_constraint(:username)
|> validate_format(:email, ~r/@/)
|> validate_coherence(attrs)
end
end

@ -0,0 +1,31 @@
defmodule Eventos.Application do
use Application
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec
# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Eventos.Repo, []),
# Start the endpoint when the application starts
supervisor(EventosWeb.Endpoint, []),
# Start your own worker by calling: Eventos.Worker.start_link(arg1, arg2, arg3)
# worker(Eventos.Worker, [arg1, arg2, arg3]),
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Eventos.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
EventosWeb.Endpoint.config_change(changed, removed)
:ok
end
end

@ -0,0 +1,40 @@
defmodule Eventos.Coherence.Invitation do
@moduledoc """
Schema to support inviting a someone to create an account.
"""
use Ecto.Schema
import Ecto.Changeset
schema "invitations" do
field :name, :string
field :email, :string
field :token, :string
timestamps()
end
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
@spec changeset(Ecto.Schema.t, Map.t) :: Ecto.Changeset.t
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(name email token))
|> validate_required([:name, :email])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
end
@doc """
Creates a changeset for a new schema
"""
@spec new_changeset(Map.t) :: Ecto.Changeset.t
def new_changeset(params \\ %{}) do
changeset %__MODULE__{}, params
end
end

@ -0,0 +1,44 @@
defmodule Eventos.Coherence.Rememberable do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Coherence.Config
schema "rememberables" do
field :series_hash, :string
field :token_hash, :string
field :token_created_at, Timex.Ecto.DateTime
belongs_to :user, Module.concat(Config.module, Config.user_schema)
timestamps()
end
use Coherence.Rememberable
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
@spec changeset(Ecto.Schema.t, Map.t) :: Ecto.Changeset.t
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(series_hash token_hash token_created_at user_id))
|> validate_required(~w(series_hash token_hash token_created_at user_id)a)
end
@doc """
Creates a changeset for a new schema
"""
@spec new_changeset(Map.t) :: Ecto.Changeset.t
def new_changeset(params \\ %{}) do
changeset %Rememberable{}, params
end
end

@ -0,0 +1,141 @@
defmodule Eventos.Coherence.Schemas do
use Coherence.Config
import Ecto.Query
@user_schema Config.user_schema
@repo Config.repo
def list_user do
@repo.all @user_schema
end
def get_by_user(opts) do
@repo.get_by @user_schema, opts
end
def get_user(id) do
@repo.get @user_schema, id
end
def get_user!(id) do
@repo.get! @user_schema, id
end
def get_user_by_email(email) do
@repo.get_by @user_schema, email: email
end
def change_user(struct, params) do
@user_schema.changeset struct, params
end
def change_user(params) do
@user_schema.changeset @user_schema.__struct__, params
end
def change_user do
@user_schema.changeset @user_schema.__struct__, %{}
end
def update_user(user, params) do
@repo.update change_user(user, params)
end
def create_user(params) do
@repo.insert change_user(params)
end
Enum.each [Eventos.Coherence.Invitation, Eventos.Coherence.Rememberable], fn module ->
name =
module
|> Module.split
|> List.last
|> String.downcase
def unquote(String.to_atom("list_#{name}"))() do
@repo.all unquote(module)
end
def unquote(String.to_atom("list_#{name}"))(%Ecto.Query{} = query) do
@repo.all query
end
def unquote(String.to_atom("get_#{name}"))(id) do
@repo.get unquote(module), id
end
def unquote(String.to_atom("get_#{name}!"))(id) do
@repo.get! unquote(module), id
end
def unquote(String.to_atom("get_by_#{name}"))(opts) do
@repo.get_by unquote(module), opts
end
def unquote(String.to_atom("change_#{name}"))(struct, params) do
unquote(module).changeset(struct, params)
end
def unquote(String.to_atom("change_#{name}"))(params) do
unquote(module).new_changeset(params)
end
def unquote(String.to_atom("change_#{name}"))() do
unquote(module).new_changeset(%{})
end
def unquote(String.to_atom("create_#{name}"))(params) do
@repo.insert unquote(module).new_changeset(params)
end
def unquote(String.to_atom("update_#{name}"))(struct, params) do
@repo.update unquote(module).changeset(struct, params)
end
def unquote(String.to_atom("delete_#{name}"))(struct) do
@repo.delete struct
end
end
def query_by(schema, opts) do
Enum.reduce opts, schema, fn {k, v}, query ->
where(query, [b], field(b, ^k) == ^v)
end
end
def delete_all(%Ecto.Query{} = query) do
@repo.delete_all query
end
def delete_all(module) when is_atom(module) do
@repo.delete_all module
end
def create(%Ecto.Changeset{} = changeset) do
@repo.insert changeset
end
def create!(%Ecto.Changeset{} = changeset) do
@repo.insert! changeset
end
def update(%Ecto.Changeset{} = changeset) do
@repo.update changeset
end
def update!(%Ecto.Changeset{} = changeset) do
@repo.update! changeset
end
def delete(schema) do
@repo.delete schema
end
def delete!(schema) do
@repo.delete! schema
end
end

@ -0,0 +1,21 @@
defmodule Eventos.Events.Category do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.Category
schema "categories" do
field :picture, :string
field :title, :string
timestamps()
end
@doc false
def changeset(%Category{} = category, attrs) do
category
|> cast(attrs, [:title, :picture])
|> validate_required([:title, :picture])
|> unique_constraint(:title)
end
end

@ -0,0 +1,26 @@
defmodule Eventos.Events.Event do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.{Event, EventAccount, EventRequest}
alias Eventos.Accounts.Account
schema "events" do
field :begin_on, :utc_datetime
field :description, :string
field :ends_on, :utc_datetime
field :title, :string
has_one :organizer_id, Account
many_to_many :participants, Account, join_through: EventAccount
has_many :event_request, EventRequest
timestamps()
end
@doc false
def changeset(%Event{} = event, attrs) do
event
|> cast(attrs, [:title, :description, :begin_on, :ends_on])
|> validate_required([:title, :description, :begin_on, :ends_on, :organizer_id])
end
end

@ -0,0 +1,22 @@
defmodule Eventos.Events.EventAccounts do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.{EventAccounts, Event}
alias Eventos.Accounts.Account
@primary_key false
schema "event_accounts" do
field :roles, :integer
belongs_to :event, Event
belongs_to :account, Account
timestamps()
end
@doc false
def changeset(%EventAccounts{} = event_accounts, attrs) do
event_accounts
|> cast(attrs, [:roles])
|> validate_required([:roles])
end
end

@ -0,0 +1,22 @@
defmodule Eventos.Events.EventRequest do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.{EventRequest, Event}
alias Eventos.Accounts.Account
schema "event_requests" do
field :state, :integer
has_one :event_id, Event
has_one :account_id, Account
timestamps()
end
@doc false
def changeset(%EventRequest{} = event_request, attrs) do
event_request
|> cast(attrs, [:state])
|> validate_required([:state])
end
end

@ -0,0 +1,488 @@
defmodule Eventos.Events do
@moduledoc """
The Events context.
"""
import Ecto.Query, warn: false
alias Eventos.Repo
alias Eventos.Events.Event
@doc """
Returns the list of events.
## Examples
iex> list_events()
[%Event{}, ...]
"""
def list_events do
Repo.all(Event)
end
@doc """
Gets a single event.
Raises `Ecto.NoResultsError` if the Event does not exist.
## Examples
iex> get_event!(123)
%Event{}
iex> get_event!(456)
** (Ecto.NoResultsError)
"""
def get_event!(id), do: Repo.get!(Event, id)
@doc """
Creates a event.
## Examples
iex> create_event(%{field: value})
{:ok, %Event{}}
iex> create_event(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_event(attrs \\ %{}) do
%Event{}
|> Event.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a event.
## Examples
iex> update_event(event, %{field: new_value})
{:ok, %Event{}}
iex> update_event(event, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_event(%Event{} = event, attrs) do
event
|> Event.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Event.
## Examples
iex> delete_event(event)
{:ok, %Event{}}
iex> delete_event(event)
{:error, %Ecto.Changeset{}}
"""
def delete_event(%Event{} = event) do
Repo.delete(event)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking event changes.
## Examples
iex> change_event(event)
%Ecto.Changeset{source: %Event{}}
"""
def change_event(%Event{} = event) do
Event.changeset(event, %{})
end
alias Eventos.Events.Category
@doc """
Returns the list of categories.
## Examples
iex> list_categories()
[%Category{}, ...]
"""
def list_categories do
Repo.all(Category)
end
@doc """
Gets a single category.
Raises `Ecto.NoResultsError` if the Category does not exist.
## Examples
iex> get_category!(123)
%Category{}
iex> get_category!(456)
** (Ecto.NoResultsError)
"""
def get_category!(id), do: Repo.get!(Category, id)
@doc """
Creates a category.
## Examples
iex> create_category(%{field: value})
{:ok, %Category{}}
iex> create_category(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_category(attrs \\ %{}) do
%Category{}
|> Category.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a category.
## Examples
iex> update_category(category, %{field: new_value})
{:ok, %Category{}}
iex> update_category(category, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_category(%Category{} = category, attrs) do
category
|> Category.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Category.
## Examples
iex> delete_category(category)
{:ok, %Category{}}
iex> delete_category(category)
{:error, %Ecto.Changeset{}}
"""
def delete_category(%Category{} = category) do
Repo.delete(category)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking category changes.
## Examples
iex> change_category(category)
%Ecto.Changeset{source: %Category{}}
"""
def change_category(%Category{} = category) do
Category.changeset(category, %{})
end
alias Eventos.Events.Tag
@doc """
Returns the list of tags.
## Examples
iex> list_tags()
[%Tag{}, ...]
"""
def list_tags do
Repo.all(Tag)
end
@doc """
Gets a single tag.
Raises `Ecto.NoResultsError` if the Tag does not exist.
## Examples
iex> get_tag!(123)
%Tag{}
iex> get_tag!(456)
** (Ecto.NoResultsError)
"""
def get_tag!(id), do: Repo.get!(Tag, id)
@doc """
Creates a tag.
## Examples
iex> create_tag(%{field: value})
{:ok, %Tag{}}
iex> create_tag(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_tag(attrs \\ %{}) do
%Tag{}
|> Tag.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a tag.
## Examples
iex> update_tag(tag, %{field: new_value})
{:ok, %Tag{}}
iex> update_tag(tag, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_tag(%Tag{} = tag, attrs) do
tag
|> Tag.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Tag.
## Examples
iex> delete_tag(tag)
{:ok, %Tag{}}
iex> delete_tag(tag)
{:error, %Ecto.Changeset{}}
"""
def delete_tag(%Tag{} = tag) do
Repo.delete(tag)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking tag changes.
## Examples
iex> change_tag(tag)
%Ecto.Changeset{source: %Tag{}}
"""
def change_tag(%Tag{} = tag) do
Tag.changeset(tag, %{})
end
alias Eventos.Events.EventAccounts
@doc """
Returns the list of event_accounts.
## Examples
iex> list_event_accounts()
[%EventAccounts{}, ...]
"""
def list_event_accounts do
Repo.all(EventAccounts)