Fix song update logic

This commit is contained in:
An Phan 2016-05-27 11:32:52 +08:00
parent 711d1d1464
commit 688e8203ef
2 changed files with 14 additions and 12 deletions

View file

@ -119,16 +119,17 @@ class Song extends Model
*/
$updatedSongs = [];
foreach ((array) $ids as $id) {
$ids = (array) $ids;
// If we're updating only one song, take into account the title, lyrics, and track number.
$single = count($ids) === 1;
foreach ($ids as $id) {
if (!$song = self::with('album', 'album.artist')->find($id)) {
continue;
}
// If we're updating only one song, take into account the title, lyrics, and track number.
$single = count($ids) === 1;
$updatedSongs[] = $song->updateSingle(
trim($data['title']) ?: $song->title,
$single ? trim($data['title']) : $song->title,
trim($data['albumName'] ?: $song->album->name),
trim($data['artistName']) ?: $song->artist->name,
$single ? trim($data['lyrics']) : $song->lyrics,

View file

@ -83,11 +83,11 @@ class SongTest extends TestCase
->put('/api/songs', [
'songs' => $songIds,
'data' => [
'title' => 'Foo Bar',
'title' => 'foo',
'artistName' => 'John Cena',
'albumName' => 'One by One',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
'lyrics' => 'bar',
'track' => 9999,
'compilationState' => 0,
],
])
@ -95,10 +95,11 @@ class SongTest extends TestCase
$songs = Song::orderBy('id', 'desc')->take(3)->get();
// Even though we post the title and lyrics, we don't expect them to take any effect
$this->assertNotEquals('Foo Bar', $songs[0]->title);
$this->assertNotEquals('Lorem ipsum dolor sic amet.', $songs[2]->lyrics);
$this->assertNotEquals(1, $songs[2]->track);
// Even though we post the title, lyrics, and tracks, we don't expect them to take any effect
// because we're updating multiple songs here.
$this->assertNotEquals('foo', $songs[0]->title);
$this->assertNotEquals('bar', $songs[2]->lyrics);
$this->assertNotEquals(9999, $songs[2]->track);
// But all of these songs must now belong to a new album and artist set
$this->assertEquals('One by One', $songs[0]->album->name);