frenzy/lib/frenzy/group.ex

58 lines
1.3 KiB
Elixir

defmodule Frenzy.Group do
use Ecto.Schema
import Ecto.Changeset
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
def to_fever_group(group) do
%{
id: group.id,
title: group.title
}
end
def to_fever_feeds_group(group) do
%{
group_id: group.id,
feed_ids: group.feeds |> Enum.map(fn feed -> feed.id end) |> Enum.join(",")
}
end
def to_fervor(group) do
%{
id: group.id,
title: group.title,
feed_ids: group.feeds |> Enum.map(fn f -> f.id end),
service_url:
Application.get_env(:frenzy, :base_url) <> Routes.group_path(Endpoint, :show, group.id)
}
end
schema "groups" do
field :title, :string
belongs_to :user, Frenzy.User
has_many :feeds, Frenzy.Feed, on_delete: :delete_all
timestamps()
end
@type t() :: %__MODULE__{
__meta__: Ecto.Schema.Metadata.t(),
id: integer() | nil,
title: String.t(),
user: Frenzy.User.t() | Ecto.Association.NotLoaded.t(),
feeds: [Frenzy.Feed.t()] | Ecto.Association.NotLoaded.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@doc false
def changeset(group, attrs) do
group
|> cast(attrs, [:title])
|> validate_required([:title])
end
end