-
Notifications
You must be signed in to change notification settings - Fork 8
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
6a2136b
commit 2224b08
Showing
10 changed files
with
217 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Atomic.News do | ||
@moduledoc """ | ||
The news context. | ||
""" | ||
|
||
alias Atomic.News.New | ||
alias Atomic.Repo | ||
|
||
@doc """ | ||
Returns the list of news. | ||
## Examples | ||
iex> list_news() | ||
[%New{}, ...] | ||
""" | ||
def list_news do | ||
Repo.all(New) | ||
end | ||
|
||
@doc """ | ||
Returns the list of news belonging to an organization. | ||
## Examples | ||
iex> list_news_by_organization_id(99d7c9e5-4212-4f59-a097-28aaa33c2621) | ||
[%New{}, ...] | ||
""" | ||
def list_news_by_organization_id(id) do | ||
Repo.all(New) | ||
|> Enum.filter(fn new -> new.organization_id == id end) | ||
end | ||
|
||
@doc """ | ||
Gets a single new. | ||
Raises `Ecto.NoResultsError` if the new does not exist. | ||
## Examples | ||
iex> get_new!(123) | ||
%New{} | ||
iex> get_new!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_new!(id), do: Repo.get!(New, id) | ||
|
||
@doc """ | ||
Creates a new. | ||
## Examples | ||
iex> create_new(%{field: value}) | ||
{:ok, %New{}} | ||
iex> create_new(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_new(attrs \\ %{}, _after_save \\ &{:ok, &1}) do | ||
%New{} | ||
|> New.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a new. | ||
## Examples | ||
iex> update_new(new, %{field: new_value}) | ||
{:ok, %New{}} | ||
iex> update_new(new, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_new(%New{} = new, attrs, _after_save \\ &{:ok, &1}) do | ||
new | ||
|> New.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a new. | ||
## Examples | ||
iex> delete_new(New) | ||
{:ok, %New{}} | ||
iex> delete_new(New) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_new(%New{} = new) do | ||
Repo.delete(new) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking new changes. | ||
## Examples | ||
iex> change_new(new) | ||
%Ecto.Changeset{data: %New{}} | ||
""" | ||
def change_new(%New{} = new, attrs \\ %{}) do | ||
New.changeset(new, 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,25 @@ | ||
defmodule Atomic.News.New do | ||
@moduledoc """ | ||
The person who speaks and provides the activity | ||
""" | ||
use Atomic.Schema | ||
alias Atomic.Organizations.Organization | ||
|
||
@required_fields ~w(title description organization_id)a | ||
|
||
schema "news" do | ||
field :title, :string | ||
field :description, :string | ||
|
||
belongs_to :organization, Organization, on_replace: :delete_if_exists | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(new, attrs) do | ||
new | ||
|> 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
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
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,17 @@ | ||
defmodule Atomic.Repo.Migrations.News do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:news, primary_key: false) do | ||
add :id, :binary_id, primary_key: true | ||
|
||
add :title, :string, null: false | ||
add :description, :string, null: false | ||
|
||
add :organization_id, references(:organizations, on_delete: :nothing, type: :binary_id), | ||
null: false | ||
|
||
timestamps() | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Atomic.Repo.Seeds.News do | ||
alias Atomic.News.New | ||
alias Atomic.Organizations.Organization | ||
alias Atomic.Repo | ||
|
||
def run do | ||
seed_news() | ||
end | ||
|
||
def seed_news() do | ||
organizations = Repo.all(Organization) | ||
|
||
for organization <- organizations do | ||
for i <- 1..10 do | ||
%New{} | ||
|> New.changeset(%{ | ||
title: "News title #{organization.name} #{i}", | ||
description: "News description #{organization.name} #{i}", | ||
organization_id: organization.id | ||
}) | ||
|> Repo.insert!() | ||
end | ||
end | ||
end | ||
end | ||
|
||
Atomic.Repo.Seeds.News.run() |
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,18 @@ | ||
defmodule Atomic.Factories.NewFactory do | ||
@moduledoc """ | ||
A factory to generate account related structs | ||
""" | ||
|
||
alias Atomic.News.New | ||
|
||
defmacro __using__(_opts) do | ||
quote do | ||
def new_factory do | ||
%New{ | ||
title: "News title", | ||
description: "News description" | ||
} | ||
end | ||
end | ||
end | ||
end |