Skip to content

Commit

Permalink
[WIP] Add new models (implement #347).
Browse files Browse the repository at this point in the history
  • Loading branch information
lGuillaume124 committed Sep 8, 2018
1 parent b671134 commit e55b367
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 0 deletions.
53 changes: 53 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,31 @@ 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' => true, 'default' => null),
'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)
),
'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
74 changes: 74 additions & 0 deletions app/Model/Album.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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

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.'
)
);

public function beforeValidate($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->validator()->getField('name')->getRule(0)->allowEmpty = true;
}
}

public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s');
} else {
unset($this->data[$this->alias]['updated']);
}
unset($this->data[$this->alias]['created']);
return true;
}

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;
}

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);
}
}
return true;
}
}
34 changes: 34 additions & 0 deletions app/Model/Band.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

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.'
)
);

public function beforeValidate($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->validator()->getField('name')->getRule(0)->allowEmpty = true;
}
}

public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s');
} else {
unset($this->data[$this->alias]['updated']);
}
unset($this->data[$this->alias]['created']);
return true;
}
}
94 changes: 94 additions & 0 deletions app/Model/Track.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

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

class Track extends AppModel
{
private $albumId;
public $belongsTo = array('Album');
public $validate = array(
'title' => array(
'rule' => array('maxLength', 255),
'message' => 'The title of a track cannot exceed 255 characters.'
),
'source_path' => array(
'rule' => array('maxLength', 4096),
'allowEmpty' => false,
'required' => 'create',
'message' => 'The source path of a file cannot be empty or exceed 4096 characters.'
),
'playtime' => array(
'rule' => array('maxLength', 9),
'message' => 'The playtime cannot exceed 9 characters.'
),
'track_number' => array(
'rule' => array('naturalNumber', true),
'message' => 'The track number must be a natural integer.'
),
'max_track_number' => array(
'rule' => array('naturalNumber', true),
'message' => 'The max track number must be a natural integer.'
),
'disc_number' => array(
'rule' => array('naturalNumber', true),
'message' => 'The disc number must be a natural integer.'
),
'max_disc_number' => array(
'rule' => array('naturalNumber', true),
'message' => 'The max disc number must be a natural integer.'
),
'year' => array(
'rule' => array('date', 'y'),
'allowEmpty' => true,
'message' => 'Invalid track year.'

),
'genre' => array(
'rule' => array('maxLength', 255),
'message' => 'The genre of a track cannot exceed 255 characters.'
),
'artist' => array(
'rule' => array('maxLength', 255),
'message' => 'The artist value cannot exceed 255 characters.'
)
);

public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s');
} else {
unset($this->data[$this->alias]['updated']);
}
unset($this->data[$this->alias]['created']);
return true;
}

public function beforeDelete($cascade = false)
{
$track = $this->find('first', array(
'fields' => array('album_id'),
'conditions' => array('id' => $this->id)
));

if (isset($track)) {
$this->albumId = $track[$this->alias]['album_id'];
}
return true;
}

public function afterDelete()
{
if (!empty($this->albumId)) {
$neighbours = $this->find('count', array(
'conditions' => array('album_id' => $this->albumId)
));

if ($neighbours == 0) {
$album = ClassRegistry::init('Album');
$album->delete($this->albumId);
}
}
return true;
}
}

0 comments on commit e55b367

Please sign in to comment.