76 lines
1.9 KiB
Elixir
76 lines
1.9 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
|
|
%{
|
|
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),
|
|
created_on_time: Timex.to_unix(item.date)
|
|
}
|
|
end
|
|
|
|
def to_fervor(item) do
|
|
%{
|
|
id: item.id,
|
|
feed_id: item.feed_id,
|
|
title: item.title,
|
|
author: item.creator,
|
|
created_at: DateTime.to_iso8601(item.date),
|
|
content: item.content,
|
|
url: item.url,
|
|
service_url:
|
|
Application.get_env(:frenzy, :base_url) <> Routes.item_path(Endpoint, :show, item.id),
|
|
read: item.read
|
|
}
|
|
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(),
|
|
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, :title, :url, :date, :content, :feed])
|
|
end
|
|
end
|