Skip to content

Commit

Permalink
Add to playlist (rauversion#17)
Browse files Browse the repository at this point in the history
* add to playlist first tab

* readme notes
  • Loading branch information
michelson authored Jul 9, 2022
1 parent 245fea9 commit 4995324
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,30 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
- [x] Add metadata form step
- [x] Embed at /embed/:track_id
- [x] Embed at /embed/:track_id/private with signed_id
- [x] Filter public/private tracks (where: private: true)
- [x] Reposts
- [x] Likes
- [x] Private preview on show
- [x] Followers / Followings
- [x] load waveform data as data
- [x] Range responses
- [ ] Fix specs on tests/rauversion
- [ ] Fix specs on tests/rauversion-web
- [ ] make GithubActions work!
- [ ] Paginate profile tracks /:username
- [ ] Paginate /tracks
- [ ] Listening history
- [x] Filter public/private tracks (where: private: true)
- [ ] Give feedback on upload preprosessing
- [ ] A logo for rauversion
- [x] Reposts
- [ ] Albums
- [ ] Playlists
- [ ] add to playlist select playlist / modal first tab
- [x] add to playlist select playlist / modal first tab
- [ ] sort songs on playlist
- [ ] playlist player
- [ ] A WYSIWYG editor for Markdown for Track/Playlist description
- [x] Likes
- [x] Private preview on show
- [x] Followers / Followings
- [ ] Telemetry for dashboard
- [ ] Centralized player, fixed (bottom) across navigation
- [x] load waveform data as data
- [x] Range responses




Expand Down
15 changes: 15 additions & 0 deletions lib/rauversion/playlists.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ defmodule Rauversion.Playlists do
]
end

def list_playlists_by_user_with_track(
_track = %Rauversion.Tracks.Track{id: track_id},
_current_user = %Rauversion.Accounts.User{id: user_id}
) do
track_query =
from pi in Rauversion.TrackPlaylists.TrackPlaylist,
where: pi.track_id == ^track_id

from pi in Playlist,
where: pi.user_id == ^user_id,
preload: [
track_playlists: ^track_query
]
end

def get_public_playlist!(id) do
Playlist
|> where(id: ^id)
Expand Down
20 changes: 19 additions & 1 deletion lib/rauversion_web/live/playlist_live/create_form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ defmodule RauversionWeb.PlaylistLive.CreateFormComponent do
<p class="text-sm text-gray-500 truncate"></p>
</div>
<div>
<a href="#" class="inline-flex items-center shadow-sm px-2.5 py-0.5 border border-gray-300 text-sm leading-5 font-medium rounded-full text-gray-700 bg-white hover:bg-gray-50"> View </a>
<%= if length(item.track_playlists) > 0 do %>
<a href="#"
phx-click="remove-from-playlist"
phx-target={@ref}
phx-value-playlist={item.id}
phx-value-track-playlist={List.first(item.track_playlists).id}
class="inline-flex items-center shadow-sm px-2.5 py-0.5 border border-gray-300 text-sm leading-5 font-medium rounded-full text-gray-700 bg-white hover:bg-gray-50">
Remove
</a>
<% else %>
<a href="#"
phx-click="add-to-playlist"
phx-target={@ref}
phx-value-playlist={item.id}
class="inline-flex items-center shadow-sm px-2.5 py-0.5 border border-gray-300 text-sm leading-5 font-medium rounded-full text-gray-700 bg-white hover:bg-gray-50">
Add
</a>
<% end %>
</div>
</div>
</li>
Expand Down
40 changes: 39 additions & 1 deletion lib/rauversion_web/live/playlist_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule RauversionWeb.PlaylistLive.FormComponent do
})

playlists =
Rauversion.Playlists.list_playlists_by_user(track.user, assigns.current_user)
Rauversion.Playlists.list_playlists_by_user_with_track(track, assigns.current_user)
|> Rauversion.Repo.all()

{:ok,
Expand Down Expand Up @@ -59,6 +59,44 @@ defmodule RauversionWeb.PlaylistLive.FormComponent do
{:noreply, assign(socket, :tab, "create-playlist-tab")}
end

# TODO: securize this
@impl true
def handle_event("add-to-playlist", %{"playlist" => id}, socket) do
playlist = Rauversion.Playlists.get_playlist!(id)
track = socket.assigns.track

Rauversion.TrackPlaylists.create_track_playlist(%{
playlist_id: playlist.id,
track_id: track.id
})

playlists =
Rauversion.Playlists.list_playlists_by_user_with_track(track, socket.assigns.current_user)
|> Rauversion.Repo.all()

{:noreply, socket |> assign(:playlists, playlists)}
end

# TODO: securize this
@impl true
def handle_event(
"remove-from-playlist",
%{"playlist" => _playlist_id, "track-playlist" => track_playlist_id},
socket
) do
track_playlist = Rauversion.TrackPlaylists.get_track_playlist!(track_playlist_id)
Rauversion.TrackPlaylists.delete_track_playlist(track_playlist)

playlists =
Rauversion.Playlists.list_playlists_by_user_with_track(
socket.assigns.track,
socket.assigns.current_user
)
|> Rauversion.Repo.all()

{:noreply, socket |> assign(:playlists, playlists)}
end

@impl true
def handle_event("validate", %{"playlist" => playlist_params}, socket) do
changeset =
Expand Down

0 comments on commit 4995324

Please sign in to comment.