Playlistr: Songs

Adding a Song feature will be similar to the others but with some new twists.

Songs

We'll be building support for Songs in the same way as we have the other features in this app.

Migrate Create

Using commandbox-migrations as before in the terminal, we use migrate create songs to create a templated script which we'll update to support Songs and the relationship with Albums.

component {

	function up( schema ) {
		schema.create( "songs", function( table ) {
			table.uuid( "id" ).primaryKey();

			table.string( "name" );

			table.unsignedInteger( "albumID" )
				.nullable()
				.references( "id" )
				.onTable( "albums" )
				.onDelete( "cascade" );
		} );
	}

	function down( schema ) {
		schema.drop( "songs" );
	}

}
resources/database/migrations/2018_07_04_100000_songs.cfc

This time we've made our primary key a UUID, which is a char(35), and added the foreign key relationship with our Album table.

Song.cfc (Model)

This is similar to the Album entity and saved as models/Song.cfc with a relationship to Albums with belongsTo( "Album" ) and declaring the primary key as a UUID type.

component extends="quick.models.BaseEntity" {
	property id;
	property name;

	property albumID sqltype="cf_sql_integer";

	function keyType() {
		return variables._wirebox.getInstance( "UUIDKeyType@quick" );
	}

	function album() {
		return belongsTo( "Album" );
	}
}
models/Song.cfc

SongService.cfc (Model)

We also have a service to do all our database interaction in the same way as Albums.

index.cfm & createUpdate.cfm (Views)

These are also the same as those for the Albums.

Handler

Once again, our Songs handler is the same as the one for Albums with the obvious names changed.