-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from samsonasik/add-track-table
adding track detail of album
- Loading branch information
Showing
32 changed files
with
986 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php namespace Album\Controllers; | ||
|
||
use Album\Domain\Album\AlbumRepository; | ||
use Album\Domain\Exception\RecordNotFoundException; | ||
use Album\Domain\Track\TrackRepository; | ||
use App\Controllers\BaseController; | ||
use CodeIgniter\Exceptions\PageNotFoundException; | ||
use Config\Services; | ||
|
||
class Track extends BaseController | ||
{ | ||
/** @var AlbumRepository */ | ||
private $albumRepository; | ||
|
||
/** @var TrackRepository */ | ||
private $trackRepository; | ||
|
||
public function __construct() | ||
{ | ||
$this->albumRepository = Services::albumRepository(); | ||
$this->trackRepository = Services::trackRepository(); | ||
} | ||
|
||
public function index(int $albumId) | ||
{ | ||
try | ||
{ | ||
$album = $this->albumRepository->findAlbumOfId($albumId); | ||
} | ||
catch (RecordNotFoundException $e) | ||
{ | ||
throw new PageNotFoundException($e->getMessage()); | ||
} | ||
|
||
$data['keyword'] = $this->request->getGet('keyword') ?? ''; | ||
$data['album'] = $album; | ||
$data['tracks'] = $this->trackRepository->findPaginatedData($album, $data['keyword']); | ||
$data['pager'] = $this->trackRepository->pager(); | ||
|
||
return view('Album\Views\track\index', $data); | ||
} | ||
|
||
public function add(int $albumId) | ||
{ | ||
try | ||
{ | ||
$album = $this->albumRepository->findAlbumOfId($albumId); | ||
} | ||
catch (RecordNotFoundException $e) | ||
{ | ||
throw new PageNotFoundException($e->getMessage()); | ||
} | ||
|
||
if ($this->request->getMethod() === 'post') | ||
{ | ||
$data = $this->request->getPost(); | ||
if ($this->trackRepository->save($data)) | ||
{ | ||
session()->setFlashdata('status', 'New album track has been added'); | ||
return redirect()->route('track-index', [$albumId]); | ||
} | ||
|
||
session()->setFlashdata('errors', $this->trackRepository->errors()); | ||
return redirect()->withInput()->back(); | ||
} | ||
|
||
return view('Album\Views\track\add', [ | ||
'album' => $album, | ||
'errors' => session()->getFlashData('errors'), | ||
]); | ||
} | ||
|
||
public function edit(int $albumId, int $trackId) | ||
{ | ||
try | ||
{ | ||
$album = $this->albumRepository->findAlbumOfId($albumId); | ||
$track = $this->trackRepository->findTrackOfId($trackId); | ||
} | ||
catch (RecordNotFoundException $e) | ||
{ | ||
throw new PageNotFoundException($e->getMessage()); | ||
} | ||
|
||
if ($this->request->getMethod() === 'post') | ||
{ | ||
$data = $this->request->getPost(); | ||
if ($this->trackRepository->save($data)) | ||
{ | ||
session()->setFlashdata('status', 'Album track has been updated'); | ||
return redirect()->route('track-index', [$albumId]); | ||
} | ||
|
||
session()->setFlashdata('errors', $this->trackRepository->errors()); | ||
return redirect()->withInput()->back(); | ||
} | ||
|
||
return view('Album\Views\track\edit', [ | ||
'album' => $album, | ||
'track' => $track, | ||
'errors' => session()->getFlashData('errors'), | ||
]); | ||
} | ||
|
||
public function delete(int $albumId, int $trackId) | ||
{ | ||
try | ||
{ | ||
$this->albumRepository->findAlbumOfId($albumId); | ||
$this->trackRepository->deleteOfId($trackId); | ||
} | ||
catch (RecordNotFoundException $e) | ||
{ | ||
throw new PageNotFoundException($e->getMessage()); | ||
} | ||
|
||
session()->setFlashdata('status', 'Album track has been deleted'); | ||
return redirect()->route('track-index', [$albumId]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php namespace Album\Database\Migrations; | ||
|
||
use CodeIgniter\Database\Migration; | ||
|
||
class Track extends Migration | ||
{ | ||
public function up() | ||
{ | ||
$this->forge->addField([ | ||
'id' => [ | ||
'type' => 'BIGINT', | ||
'unsigned' => true, | ||
'auto_increment' => true, | ||
], | ||
'album_id' => [ | ||
'type' => 'BIGINT', | ||
'unsigned' => true, | ||
], | ||
'title' => [ | ||
'type' => 'VARCHAR', | ||
'constraint' => '255', | ||
], | ||
'author' => [ | ||
'type' => 'VARCHAR', | ||
'constraint' => '255', | ||
], | ||
]); | ||
$this->forge->addKey('id', true); | ||
$this->forge->addForeignKey('album_id', 'album', 'id', 'CASCADE', 'CASCADE'); | ||
$this->forge->createTable('track'); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->forge->dropTable('track'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php namespace Album\Database\Seeds; | ||
|
||
use CodeIgniter\Database\Seeder; | ||
|
||
class TrackSeeder extends Seeder | ||
{ | ||
public function run() | ||
{ | ||
$row = [ | ||
'id' => 1, | ||
'album_id' => 1, | ||
'title' => 'Sebuah Kisah Klasik', | ||
'author' => 'Eross Chandra', | ||
]; | ||
|
||
$this->db->table('track')->insert($row); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
<?php namespace Album\Domain\Album; | ||
|
||
use CodeIgniter\Pager\PagerInterface; | ||
use Album\Domain\Repository; | ||
|
||
interface AlbumRepository | ||
interface AlbumRepository extends Repository | ||
{ | ||
public function findPaginatedData(string $keyword = ''): ?array; | ||
public function pager(): ?PagerInterface; | ||
public function findAlbumOfId(int $id): Album; | ||
public function save(array $data): bool; | ||
public function errors(): ?array; | ||
public function deleteOfId(int $id): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php namespace Album\Domain; | ||
|
||
use CodeIgniter\Pager\PagerInterface; | ||
|
||
interface Repository | ||
{ | ||
public function pager(): ?PagerInterface; | ||
public function save(array $data): bool; | ||
public function errors(): ?array; | ||
public function deleteOfId(int $id): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php namespace Album\Domain\Track; | ||
|
||
use CodeIgniter\Entity; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $album_id | ||
* @property string $title | ||
*/ | ||
class Track extends Entity | ||
{ | ||
protected $attributes = [ | ||
'id' => null, | ||
'album_id' => null, | ||
'title' => null, | ||
'author' => null, | ||
]; | ||
|
||
public function setAlbumId(int $albumId): self | ||
{ | ||
$this->attributes['album_id'] = $albumId; | ||
return $this; | ||
} | ||
|
||
public function setTitle(string $title): self | ||
{ | ||
$this->attributes['title'] = ucwords($title); | ||
return $this; | ||
} | ||
|
||
public function setAuthor(string $author): self | ||
{ | ||
$this->attributes['author'] = ucwords($author); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php namespace Album\Domain\Track; | ||
|
||
use Album\Domain\Exception\RecordNotFoundException; | ||
|
||
class TrackNotFoundException extends RecordNotFoundException | ||
{ | ||
/** @var string */ | ||
public $message = 'The album track you requested does not exist.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php namespace Album\Domain\Track; | ||
|
||
use Album\Domain\Album\Album; | ||
use Album\Domain\Repository; | ||
|
||
interface TrackRepository extends Repository | ||
{ | ||
public function findPaginatedData(Album $album, string $keyword = ''): ?array; | ||
public function findTrackOfId(int $id): Track; | ||
} |
Oops, something went wrong.