Skip to content

Commit

Permalink
Add new models (Album, Band and Track) (implement #347)
Browse files Browse the repository at this point in the history
  • Loading branch information
lGuillaume124 committed Feb 5, 2019
1 parent b71e276 commit aa9b594
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/Config/Schema/sonerezh_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ public function before($event = array()) {
public function after($event = array()) {
}

public $albums = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index', 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'cover' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 37, 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'band_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false, 'key' => 'index'),
'year' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 4, 'unsigned' => false),
'created' => array('type' => 'timestamp', 'null' => false, 'default' => null),
'updated' => array('type' => 'timestamp', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'albums_bands_id_fk' => array('column' => 'band_id', 'unique' => 0),
'albums_name_index' => array('column' => 'name', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8mb4', 'collate' => 'utf8mb4_unicode_ci', 'engine' => 'InnoDB')
);

public $bands = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index', 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'created' => array('type' => 'timestamp', 'null' => false, 'default' => null),
'updated' => array('type' => 'timestamp', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'bands_name_index' => array('column' => 'name', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8mb4', 'collate' => 'utf8mb4_unicode_ci', 'engine' => 'InnoDB')
);

public $playlist_memberships = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 8, 'unsigned' => true, 'key' => 'primary'),
'playlist_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 8, 'unsigned' => true),
Expand Down Expand Up @@ -79,6 +107,33 @@ public function after($event = array()) {
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);

public $tracks = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'title' => array('type' => 'string', 'null' => true, 'default' => null, 'key' => 'index', 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'source_path' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 4096, 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'playtime' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 9, 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'track_number' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 5, 'unsigned' => false),
'max_track_number' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 5, 'unsigned' => false),
'disc_number' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 7, 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'max_disc_number' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 5, 'unsigned' => false),
'year' => array('type' => 'integer', 'null' => true, 'default' => null, 'length' => 4, 'unsigned' => false),
'genre' => array('type' => 'string', 'null' => true, 'default' => null, 'key' => 'index', 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'artist' => array('type' => 'string', 'null' => true, 'default' => null, 'key' => 'index', 'collate' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4'),
'created' => array('type' => 'timestamp', 'null' => false, 'default' => null),
'album_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false, 'key' => 'index'),
'updated' => array('type' => 'timestamp', 'null' => false, 'default' => null),
'imported' => array('type' => 'boolean', 'null' => true, 'default' => '1', 'key' => 'index'),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'tracks_albums_id_fk' => array('column' => 'album_id', 'unique' => 0),
'tracks_title_index' => array('column' => 'title', 'unique' => 0),
'tracks_artist_index' => array('column' => 'artist', 'unique' => 0),
'tracks_genre_index' => array('column' => 'genre', 'unique' => 0),
'tracks_imported_index' => array('column' => 'imported', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8mb4', 'collate' => 'utf8mb4_unicode_ci', 'engine' => 'InnoDB')
);

public $users = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 5, 'unsigned' => true, 'key' => 'primary'),
'email' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
Expand Down
55 changes: 55 additions & 0 deletions app/Config/Schema/sonerezh_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ public function before($event = array()) {
public function after($event = array()) {
}

public $albums = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 11, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => null),
'cover' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 37),
'band_id' => array('type' => 'integer', 'null' => true, 'default' => null),
'year' => array('type' => 'integer', 'null' => true, 'default' => null),
'created' => array('type' => 'datetime', 'null' => false, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('unique' => true, 'column' => 'id'),
'albums_bands_id_fk' => array('unique' => false, 'column' => 'band_id'),
'albums_name_index' => array('unique' => false, 'column' => 'name')
),
'tableParameters' => array()
);

public $bands = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 11, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => null),
'created' => array('type' => 'datetime', 'null' => false, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('unique' => true, 'column' => 'id'),
'bands_name_index' => array('unique' => false, 'column' => 'name')
),
'tableParameters' => array()
);

public $playlist_memberships = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 11, 'key' => 'primary'),
'playlist_id' => array('type' => 'integer', 'null' => false, 'default' => null),
Expand Down Expand Up @@ -79,6 +107,33 @@ public function after($event = array()) {
'tableParameters' => array()
);

public $tracks = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 11, 'key' => 'primary'),
'title' => array('type' => 'string', 'null' => true, 'default' => null),
'source_path' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 4096),
'playtime' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 9),
'track_number' => array('type' => 'integer', 'null' => true, 'default' => null),
'max_track_number' => array('type' => 'integer', 'null' => true, 'default' => null),
'disc_number' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 7),
'max_disc_number' => array('type' => 'integer', 'null' => true, 'default' => null),
'year' => array('type' => 'integer', 'null' => true, 'default' => null),
'genre' => array('type' => 'string', 'null' => true, 'default' => null),
'artist' => array('type' => 'string', 'null' => true, 'default' => null),
'created' => array('type' => 'datetime', 'null' => false, 'default' => 'CURRENT_TIMESTAMP'),
'album_id' => array('type' => 'integer', 'null' => true, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => 'CURRENT_TIMESTAMP'),
'imported' => array('type' => 'boolean', 'null' => true, 'default' => true),
'indexes' => array(
'PRIMARY' => array('unique' => true, 'column' => 'id'),
'tracks_albums_id_fk' => array('unique' => false, 'column' => 'album_id'),
'tracks_artist_index' => array('unique' => false, 'column' => 'artist'),
'tracks_genre_index' => array('unique' => false, 'column' => 'genre'),
'tracks_imported_index' => array('unique' => false, 'column' => 'imported'),
'tracks_title_index' => array('unique' => false, 'column' => 'title')
),
'tableParameters' => array()
);

public $users = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 11, 'key' => 'primary'),
'email' => array('type' => 'string', 'null' => false, 'default' => null),
Expand Down
105 changes: 105 additions & 0 deletions app/Model/Album.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

App::uses('AppModel', 'Model');

/**
* The Album Model. Represents an album.
*
* An album is defined with a unique ID, a name, a year and a cover. It belongs
* to a Band identified by the "band_id" key and contains one or several Tracks.
*/
class Album extends AppModel
{
private $bandId;
public $belongsTo = array('Band');
public $hasMany = array('Track');
public $validate = array(
'name' => array(
'rule' => array('maxLength', 255),
'required' => 'create',
'allowEmpty' => false,
'message' => 'Album\'s name cannot be empty or exceed 255 characters.'
),
'cover' => array(
'rule' => array('maxLength', 37),
'allowEmpty' => true,
'message' => 'Album\'s cover cannot exceed 37 characters.'
),
'year' => array(
'rule' => array('date', 'y'),
'allowEmpty' => true,
'message' => 'Invalid album year.'
)
);

/**
* Allow empty value for the "name" field on update.
*
* @param array $options
* @return bool|void
*/
public function beforeValidate($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->validator()->getField('name')->getRule(0)->allowEmpty = true;
}
}

/**
* Override the "created" and the "updated" fields to ensure they are
* properly filled.
*
* @param array $options
* @return bool
*/
public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s');
unset($this->data[$this->alias]['created']);
} else {
$this->data[$this->alias]['created'] = date('Y-m-d H:i:s');
unset($this->data[$this->alias]['updated']);
}
return true;
}

/**
* Stores the Band id the deleted album belongs to, to perform further
* checks once the record is deleted (the ID is used in the afterDelete()
* method).
*
* @param bool $cascade
* @return bool
*/
public function beforeDelete($cascade = false)
{
$album = $this->find('first', array(
'fields' => array('band_id'),
'conditions' => array('id' => $this->id)
));

if (isset($album)) {
$this->bandId = $album[$this->alias]['band_id'];
}
return true;
}

/**
* Checks if the Band the deleted album belongs to still have albums, and
* deletes it if it's not the case.
*/
public function afterDelete()
{
if (!empty($this->bandId)) {
$neighbours = $this->find('count', array(
'conditions' => array('band_id' => $this->bandId)
));

if ($neighbours == 0) {
$band = ClassRegistry::init('Band');
$band->delete($this->bandId);
}
}
}
}
53 changes: 53 additions & 0 deletions app/Model/Band.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

App::uses('AppModel', 'Model');

/**
* The Band Model. Represents a band.
*
* A band is defined with a unique ID and a name. It has one or several albums.
*/
class Band extends AppModel
{
public $hasMany = array('Album');
public $validate = array(
'name' => array(
'rule' => array('maxLength', 255),
'required' => 'create',
'allowEmpty' => false,
'message' => 'The band\'s name cannot be empty or exceed 255 characters.'
)
);

/**
* Allow empty value for the "name" field on update.
*
* @param array $options
* @return bool|void
*/
public function beforeValidate($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->validator()->getField('name')->getRule(0)->allowEmpty = true;
}
}

/**
* Override the "created" and the "updated" fields to ensure they are
* properly filled.
*
* @param array $options
* @return bool
*/
public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s');
unset($this->data[$this->alias]['created']);
} else {
$this->data[$this->alias]['created'] = date('Y-m-d H:i:s');
unset($this->data[$this->alias]['updated']);
}
return true;
}
}
Loading

0 comments on commit aa9b594

Please sign in to comment.