Skip to content

Commit

Permalink
Add news
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Aug 6, 2023
1 parent 6a2136b commit 2224b08
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 3 deletions.
116 changes: 116 additions & 0 deletions lib/atomic/news.ex
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
25 changes: 25 additions & 0 deletions lib/atomic/news/new.ex
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
7 changes: 7 additions & 0 deletions lib/atomic/organizations/organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Atomic.Organizations.Organization do
use Atomic.Schema
alias Atomic.Accounts.User
alias Atomic.Activities.Location
alias Atomic.News.New
alias Atomic.Organizations.Card
alias Atomic.Organizations.Department
alias Atomic.Organizations.Membership
Expand Down Expand Up @@ -34,6 +35,12 @@ defmodule Atomic.Organizations.Organization do
embeds_one :location, Location, on_replace: :delete
embeds_one :card, Card, on_replace: :delete

has_many :news, New,
on_replace: :delete_if_exists,
on_delete: :delete_all,
foreign_key: :organization_id,
preload_order: [asc: :title]

timestamps()
end

Expand Down
4 changes: 4 additions & 0 deletions lib/atomic_web/live/home_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AtomicWeb.HomeLive.Index do
use AtomicWeb, :live_view

alias Atomic.Activities
alias Atomic.News
alias Atomic.Partnerships
alias Atomic.Uploaders.Card

Expand All @@ -23,6 +24,8 @@ defmodule AtomicWeb.HomeLive.Index do
activities =
Activities.list_activities_by_organization_id(socket.assigns.current_organization.id, [])

news = News.list_news_by_organization_id(socket.assigns.current_organization.id)

mode = params["mode"] || "month"

entries = [
Expand All @@ -37,6 +40,7 @@ defmodule AtomicWeb.HomeLive.Index do
|> assign(:page_title, "Home")
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :home)
|> assign(:news, news)
|> assign(:partners, partners)
|> assign(:activities, activities)
|> assign(:time_zone, socket.assigns.time_zone)
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/home_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<%= live_patch("See all", to: Routes.partner_index_path(@socket, :index, @current_organization), class: "text-sm font-semibold text-gray-300 ml-4") %>
</div>
<ul class="">
<%= for new <- Enum.take(@activities,5) do %>
<%= for new <- Enum.take(@news,5) do %>
<li class="w-full items-center justify-between border-b-2 border-gray-200 gap-x-6 py-5">
<div class="">
<div class="flex flex-col">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Atomic.Repo.Migrations.CreateOrganizations do
add :id, :binary_id, primary_key: true
add :name, :string, null: false
add :description, :text, null: false
add :organization_id, references(:organizations, type: :binary_id)
add :location, :map

timestamps()
Expand Down
17 changes: 17 additions & 0 deletions priv/repo/migrations/20230830011755_add_news.exs
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
3 changes: 2 additions & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule Atomic.Repo.Seeds do
"accounts.exs",
"departments.exs",
"activities.exs",
"memberships.exs"
"memberships.exs",
"news.exs"
]
|> Enum.each(fn file ->
Code.require_file("#{@seeds_dir}/#{file}")
Expand Down
27 changes: 27 additions & 0 deletions priv/repo/seeds/news.exs
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()
18 changes: 18 additions & 0 deletions test/support/factories/news_factory.ex
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

0 comments on commit 2224b08

Please sign in to comment.