Introduce forms into a play application to enable the user to create playlists.
Introduce a 'Delete Playlist' button for each playlist, represented by a trash
icon. E.g:
In addition, the view
link is replace by a folder open
icon.
Bind the delete playlist
button to a new function to be implemented in the Dashboard controller, which should log the id of the playlist to be deleted.
First the new user interface:
#{extends 'main.html' /}
#{set title:'Dashboard' /}
#{menu id:"dashboard"/}
#{list items:playlists, as:'playlist'}
<section class="ui segment">
<h2 class="ui header">
${playlist.title}
</h2>
<p> Total Duration: ${playlist.duration} </p>
<a href="/playlists/${playlist.id}" class="ui icon button">
<i class="icon folder open"></i>
</a>
<a href="/dashboard/deleteplaylist/${playlist.id}" class="ui icon button">
<i class="icon trash"></i>
</a>
</section>
#{/list}
Now, make the button actually delete the denoted playlist.
GET /dashboard/deleteplaylist/{id} Dashboard.deletePlaylist
...
public static void deletePlaylist (Long id)
{
Playlist playlist = Playlist.findById(id);
Logger.info ("Removing" + playlist.title);
playlist.delete();
redirect ("/dashboard");
}
...
Try this now - and make sure a playlist is being deleted.
Also - check to see if the songs in the playlist are also deleted?
Introduce a new partial to provide a simple form to add songs to a playlist:
<form class="ui stacked segment form" action="/playlists/${_playlist.id}/addsong" method="POST">
<div class="two fields">
<div class="field">
<label>Title</label>
<input placeholder="Title" type="text" name="title">
</div>
<div class="field">
<label>Artist</label>
<input placeholder="Artist" type="text" name="artist">
</div>
<div class="field">
<label>Duration</label>
<input placeholder="Duration" type="number" name="duration">
</div>
</div>
<button class="ui blue submit button">Add Song</button>
</form>
#{extends 'main.html' /}
#{set title:'Playlist' /}
#{menu id:"dashboard"/}
<section class="ui segment">
<h2 class="ui header">
${playlist.title}
</h2>
#{listsongs playlist:playlist /}
#{addsong playlist:playlist /}
</section>
The playlists should now render like this:
To implement the feature - we need a new route:
...
POST /playlists/{id}/addsong PlaylistCtrl.addSong
...
Finally, the actual implementation of the add the song:
...
public static void addSong(Long id, String title, String artist, int duration)
{
Song song = new Song(title, artist, duration);
Playlist playlist = Playlist.findById(id);
playlist.songs.add(song);
playlist.save();
redirect ("/playlists/" + id);
}
...
Restart the app now - and verify that you can add songs. Check in the database to verify that the new songs appear in the table.
A new form to support adding new playlists:
<form class="ui stacked segment form" action="/dashboard/addplaylist" method="POST">
<div class="field">
<label>Title</label>
<input placeholder="Title" type="text" name="title">
</div>
<button class="ui blue submit button">Add Playlist</button>
</form>
Include this partial at the end of the dashboard view:
...
...
#{addplaylist /}
The dashboard should look like this now:
To implement the feature - we need a new route:
...
POST /dasghboard/addplaylist Dashboard.addPlaylist
...
Finally, the actual implementation of the add playlist logic:
...
public static void addPlaylist (String title)
{
Playlist playlist = new Playlist (title, 0);
Logger.info ("Adding a new playlist called " + title);
playlist.save();
redirect ("/dashboard");
}
...
Restart the app now - and verify that you can add playlists. Check in the database to verify that the new playlists appear in the table. Also add some songs to the playlist.
Even if you have successfully completed this lab - do not skip this step, as it is important to be able to run the sample solutions occasionally, and also retain them for reference purposes.
A complete version of the app as it should be at the end of this lab:
Download and unzip the archive (select the zip version). Once unzipped, rename the folder to playlist-4
. Make sure the unzipped and renamed folder looks exactly like this:
Now we need to rename the project. Open the following file:
# This is the main configuration file for the application.
# ~~
application.name=playlist
...
Change playlist
above to playlist-4
.
Next, from a shell inside the project folder run the play eclipsify
command:
In eclipse - select File->Import
... and then select the following option:
Select Browse
and locate the playlist-4 folder:
Press Finish
- and the project will be imported:
You should be able to run app in the usual way:
The last lab had install instruction for InteliJ Idea. Try to figure out how to import the project into this Idea.