frenzy/lib/frenzy/item.ex

86 lines
2.0 KiB
Elixir

defmodule Frenzy.Item do
use Ecto.Schema
import Ecto.Changeset
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
def to_fever(item) do
res = %{
id: item.id,
feed_id: item.feed_id,
title: item.title,
author: item.creator,
html: item.content,
url: item.url,
is_saved: 0,
is_read: if(item.read, do: 1, else: 0)
}
if item.date do
Map.put(res, :created_on_time, Timex.to_unix(item.date))
else
res
end
end
def to_fervor(item) do
res = %{
id: item.id,
feed_id: item.feed_id,
title: item.title,
author: item.creator,
content: item.content,
url: item.url,
service_url:
Application.get_env(:frenzy, :base_url) <> Routes.item_path(Endpoint, :show, item.id),
read: item.read
}
if item.date do
Map.put(res, :created_at, DateTime.to_iso8601(item.date))
else
res
end
end
schema "items" do
field :content, :string
field :date, :utc_datetime
field :creator, :string
field :guid, :string
field :url, :string
field :read, :boolean, default: false
field :read_date, :utc_datetime
field :title, :string
field :tombstone, :boolean, default: false
belongs_to :feed, Frenzy.Feed
timestamps()
end
@type t() :: %__MODULE__{
__meta__: Ecto.Schema.Metadata.t(),
id: integer() | nil,
content: String.t(),
date: DateTime.t(),
creator: String.t(),
guid: String.t(),
url: String.t(),
read: boolean(),
read_date: DateTime.t(),
title: String.t() | nil,
tombstone: boolean(),
feed: Frenzy.Feed.t() | Ecto.Association.NotLoaded.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone])
|> validate_required([:guid, :url, :date, :content, :feed])
end
end