Browse Source

feat(listens): create tables, switch to waffle for uploading

pull/37/head
Inhji Y. 1 year ago
parent
commit
e983cfb33c
  1. 66
      apps/db/priv/repo/migrations/20200423204624_create_listens_models.exs
  2. 32
      apps/listens/lib/albums/album.ex
  3. 32
      apps/listens/lib/albums/uploader.ex
  4. 32
      apps/listens/lib/artists/artist.ex
  5. 32
      apps/listens/lib/artists/uploader.ex
  6. 21
      apps/listens/lib/listens/listen.ex
  7. 22
      apps/listens/lib/tracks/track.ex
  8. 2
      apps/listens/mix.exs
  9. 2
      mix.lock

66
apps/db/priv/repo/migrations/20200423204624_create_listens_models.exs

@ -0,0 +1,66 @@
defmodule Db.Repo.Migrations.CreateListensModels do
use Ecto.Migration
def change do
# ARTISTS
create table(:artists) do
add :name, :string
add :image, :string
add :discogs_id, :integer
add :mbid, :string
add :msid, :string
timestamps()
end
create index(:artists, :msid)
# ALBUMS
create table(:albums) do
add :name, :string
add :image, :string
add :mbid, :string
add :msid, :string
add :discogs_id, :integer
add :artist_id, references(:artists, on_delete: :nothing)
timestamps()
end
create index(:albums, :artist_id)
create index(:albums, :msid)
# TRACKS
create table(:tracks) do
add :name, :string
add :album_id, references(:albums, on_delete: :nothing)
add :artist_id, references(:artists, on_delete: :nothing)
timestamps()
end
create index(:tracks, [:album_id])
create index(:tracks, [:artist_id])
# LISTENS
create table(:listens) do
add :listened_at, :timestamptz
add :track_id, references(:tracks, on_delete: :nothing)
add :artist_id, references(:artists, on_delete: :nothing)
add :album_id, references(:albums, on_delete: :nothing)
timestamps()
end
create index(:listens, [:track_id])
create index(:listens, [:album_id])
create index(:listens, [:artist_id])
end
end

32
apps/listens/lib/albums/album.ex

@ -0,0 +1,32 @@
defmodule Listens.Albums.Album do
use Ecto.Schema
use Waffle.Ecto.Schema
import Ecto.Changeset
schema "albums" do
field :name, :string
field :image, Listens.Albums.Uploader.Type
field :mbid, :string
field :msid, :string
field :discogs_id, :integer
field :delete_image, :boolean,
virtual: true,
default: false
belongs_to :artist, Listens.Artists.Artist
has_many :tracks, Listens.Tracks.Track
has_many :listens, Listens.Listens.Listen
timestamps()
end
@doc false
def changeset(album \\ %__MODULE__{}, attrs \\ %{}) do
album
|> cast(attrs, [:name, :mbid, :msid, :artist_id, :discogs_id])
|> cast_attachments(attrs, [:image], allow_paths: true)
|> validate_required([:name, :msid, :artist_id])
end
end

32
apps/listens/lib/albums/uploader.ex

@ -0,0 +1,32 @@
defmodule Listens.Albums.Uploader do
use Waffle.Definition
use Waffle.Ecto.Definition
@versions [:thumb, :large]
# Whitelist file extensions:
def validate({_file, _scope}), do: true
def transform(:large, _) do
{:convert, "-strip -thumbnail 500x500^ -gravity center -extent 500x500 -format png", :png}
end
def transform(:thumb, _) do
{:convert, "-strip -thumbnail 100x100^ -gravity center -extent 100x100 -format png", :png}
end
# Override the persisted filenames:
def filename(version, {_file, scope}) do
"#{scope.msid}_#{version}"
end
# Override the storage directory:
def storage_dir(_, _) do
"uploads/album"
end
# Provide a default URL if there hasn't been a file uploaded
def default_url(_version, _scope) do
"/images/album.png"
end
end

32
apps/listens/lib/artists/artist.ex

@ -0,0 +1,32 @@
defmodule Listens.Artists.Artist do
use Ecto.Schema
use Waffle.Ecto.Schema
import Ecto.Changeset
schema "artists" do
field :name, :string
field :image, Listens.Artists.Uploader.Type
field :msid, :string
field :mbid, :string
field :discogs_id, :integer
field :delete_image, :boolean,
virtual: true,
default: false
has_many :albums, Listens.Albums.Album
has_many :tracks, Listens.Tracks.Track
has_many :listens, Listens.Listens.Listen
timestamps()
end
@doc false
def changeset(artist \\ %__MODULE__{}, attrs \\ %{}) do
artist
|> cast(attrs, [:name, :msid, :mbid, :discogs_id])
|> cast_attachments(attrs, [:image], allow_paths: true)
|> validate_required([:name, :msid])
end
end

32
apps/listens/lib/artists/uploader.ex

@ -0,0 +1,32 @@
defmodule Listens.Artists.Uploader do
use Waffle.Definition
use Waffle.Ecto.Definition
@versions [:thumb, :large]
# Whitelist file extensions:
def validate({_file, _scope}), do: true
def transform(:large, _) do
{:convert, "-strip -thumbnail 500x500^ -gravity center -extent 500x500 -format png", :png}
end
def transform(:thumb, _) do
{:convert, "-strip -thumbnail 100x100^ -gravity center -extent 100x100 -format png", :png}
end
# Override the persisted filenames:
def filename(version, {_file, scope}) do
"#{scope.msid}_#{version}"
end
# Override the storage directory:
def storage_dir(_, _) do
"uploads/listens/artists"
end
# Provide a default URL if there hasn't been a file uploaded
def default_url(_version, _scope) do
"/images/artist.jpg"
end
end

21
apps/listens/lib/listens/listen.ex

@ -0,0 +1,21 @@
defmodule Listens.Listens.Listen do
use Ecto.Schema
import Ecto.Changeset
schema "listens" do
field :listened_at, :utc_datetime
belongs_to :track, Listens.Tracks.Track
belongs_to :artist, Listens.Artists.Artist
belongs_to :album, Listens.Albums.Album
timestamps()
end
@doc false
def changeset(listen \\ %__MODULE__{}, attrs(%{})) do
listen
|> cast(attrs, [:track_id, :album_id, :artist_id, :listened_at])
|> validate_required([:track_id, :album_id, :artist_id, :listened_at])
end
end

22
apps/listens/lib/tracks/track.ex

@ -0,0 +1,22 @@
defmodule Listens.Tracks.Track do
use Ecto.Schema
import Ecto.Changeset
schema "tracks" do
field :name, :string
belongs_to :album, Listens.Albums.Album
belongs_to :artist, Listens.Artists.Artist
has_many :listens, Listens.Listens.Listen
timestamps()
end
@doc false
def changeset(track \\ %__MODULE__{}, attrs \\ %{}) do
track
|> cast(attrs, [:name, :album_id, :artist_id])
|> validate_required([:name, :album_id, :artist_id])
end
end

2
apps/listens/mix.exs

@ -25,6 +25,8 @@ defmodule Listens.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:waffle, "~> 1.0.1"},
{:waffle_ecto, "~> 0.0.8"},
{:ecto_sql, "~> 3.1"},
{:db, in_umbrella: true}
]

2
mix.lock

@ -62,4 +62,6 @@
"timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"},
"tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
"waffle": {:hex, :waffle, "1.0.1", "2eb6d14866b07717fc1450ee2e7a4a3583660fc62a7ee1984b8e75c642c09d2a", [:mix], [{:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:ex_aws_s3, "~> 2.0", [hex: :ex_aws_s3, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "36a7303fd6014dfce83c1b732219a51607a7469c4b4d6b47a47e0600dc7c01dc"},
"waffle_ecto": {:hex, :waffle_ecto, "0.0.8", "a89273d09b27cfd534c0d99265cbc8dd14e6f8d74e568a0a353f29b0c79be926", [:mix], [{:ecto, ">= 2.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:waffle, "~> 1.0.0", [hex: :waffle, repo: "hexpm", optional: false]}], "hexpm", "dcc6170b3f75c9c9db1b009dff80bd4157503f1638d20331c1814595515008d5"},
}
Loading…
Cancel
Save