Skip to content

Commit

Permalink
track comments model (rauversion#22)
Browse files Browse the repository at this point in the history
* track comments model

* track comments form on track show

* append comments, comments component
  • Loading branch information
michelson authored Jul 13, 2022
1 parent afc4d5b commit 66f9b4e
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 99 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
- [x] load waveform data as data
- [x] Range responses
- [x] Fix specs on tests/rauversion
- [x] Paginate profile tracks /:username
- [x] Paginate /tracks
- [ ] Fix specs on tests/rauversion-web
- [ ] Make GithubActions work!
- [ ] Paginate profile tracks /:username
- [ ] Paginate /tracks
- [ ] Listening history
- [ ] Give feedback on upload preprosessing
- [ ] Refactor audio processing, add proceesing queue for the after upload
Expand Down
1 change: 1 addition & 0 deletions lib/rauversion/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Rauversion.Accounts.User do
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime

has_many :track_comments, Rauversion.TrackComments.TrackComment
has_many :tracks, Rauversion.Tracks.Track, on_delete: :delete_all
has_many :playlists, Rauversion.Playlists.Playlist, on_delete: :delete_all

Expand Down
104 changes: 104 additions & 0 deletions lib/rauversion/track_comments.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule Rauversion.TrackComments do
@moduledoc """
The TrackComments context.
"""

import Ecto.Query, warn: false
alias Rauversion.Repo

alias Rauversion.TrackComments.TrackComment

@doc """
Returns the list of track_comments.
## Examples
iex> list_track_comments()
[%TrackComment{}, ...]
"""
def list_track_comments do
Repo.all(TrackComment)
end

@doc """
Gets a single track_comment.
Raises `Ecto.NoResultsError` if the Track comment does not exist.
## Examples
iex> get_track_comment!(123)
%TrackComment{}
iex> get_track_comment!(456)
** (Ecto.NoResultsError)
"""
def get_track_comment!(id), do: Repo.get!(TrackComment, id)

@doc """
Creates a track_comment.
## Examples
iex> create_track_comment(%{field: value})
{:ok, %TrackComment{}}
iex> create_track_comment(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_track_comment(attrs \\ %{}) do
%TrackComment{}
|> TrackComment.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a track_comment.
## Examples
iex> update_track_comment(track_comment, %{field: new_value})
{:ok, %TrackComment{}}
iex> update_track_comment(track_comment, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_track_comment(%TrackComment{} = track_comment, attrs) do
track_comment
|> TrackComment.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a track_comment.
## Examples
iex> delete_track_comment(track_comment)
{:ok, %TrackComment{}}
iex> delete_track_comment(track_comment)
{:error, %Ecto.Changeset{}}
"""
def delete_track_comment(%TrackComment{} = track_comment) do
Repo.delete(track_comment)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking track_comment changes.
## Examples
iex> change_track_comment(track_comment)
%Ecto.Changeset{data: %TrackComment{}}
"""
def change_track_comment(%TrackComment{} = track_comment, attrs \\ %{}) do
TrackComment.changeset(track_comment, attrs)
end
end
24 changes: 24 additions & 0 deletions lib/rauversion/track_comments/track_comment.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Rauversion.TrackComments.TrackComment do
use Ecto.Schema
import Ecto.Changeset

schema "track_comments" do
field :body, :string
field :state, :string
field :track_minute, :integer
# field :user_id, :id
# field :track_id, :id

belongs_to :user, Rauversion.Accounts.User
belongs_to :track, Rauversion.Tracks.Track

timestamps()
end

@doc false
def changeset(track_comment, attrs) do
track_comment
|> cast(attrs, [:user_id, :track_id, :body, :track_minute, :state])
|> validate_required([:user_id, :track_id, :body])
end
end
1 change: 1 addition & 0 deletions lib/rauversion/tracks/track.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Rauversion.Tracks.Track do
field :slug, TitleSlug.Type
field :title, :string

has_many :track_comments, Rauversion.TrackComments.TrackComment
has_many :track_playlists, Rauversion.TrackPlaylists.TrackPlaylist, on_delete: :delete_all
has_many :playlists, through: [:track_playlists, :playlist]

Expand Down
Loading

0 comments on commit 66f9b4e

Please sign in to comment.