Add converter from api MusicDirectory to domain MusicDirectory.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-08-20 14:53:04 +02:00
parent f54ecd29fb
commit 768616d14a
3 changed files with 99 additions and 0 deletions

View File

@ -416,6 +416,10 @@ public class MusicDirectory
}
}
public void setCreated(Date created) {
this.created = created;
}
public int getCloseness()
{
return closeness;

View File

@ -3,11 +3,14 @@
package org.moire.ultrasonic.data
import org.moire.ultrasonic.api.subsonic.models.Index
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.Indexes
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.MusicFolder
import org.moire.ultrasonic.api.subsonic.models.Artist as APIArtist
import org.moire.ultrasonic.api.subsonic.models.Indexes as APIIndexes
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory as APIMusicDirectory
import org.moire.ultrasonic.api.subsonic.models.MusicFolder as APIMusicFolder
fun APIMusicFolder.toDomainEntity(): MusicFolder = MusicFolder(this.id.toString(), this.name)
@ -26,3 +29,36 @@ fun APIArtist.toDomainEntity(): Artist = Artist().apply {
id = this@toDomainEntity.id.toString()
name = this@toDomainEntity.name
}
fun MusicDirectoryChild.toDomainEntity(): MusicDirectory.Entry = MusicDirectory.Entry().apply {
id = this@toDomainEntity.id.toString()
parent = this@toDomainEntity.parent.toString()
setIsDirectory(this@toDomainEntity.isDir)
title = this@toDomainEntity.title
album = this@toDomainEntity.album
albumId = this@toDomainEntity.albumId.toString()
artist = this@toDomainEntity.artist
artistId = this@toDomainEntity.artistId.toString()
track = this@toDomainEntity.track
year = this@toDomainEntity.year
genre = this@toDomainEntity.genre
contentType = this@toDomainEntity.contentType
suffix = this@toDomainEntity.suffix
transcodedContentType = this@toDomainEntity.transcodedContentType
transcodedSuffix = this@toDomainEntity.transcodedSuffix
coverArt = this@toDomainEntity.coverArt.toString()
size = this@toDomainEntity.size
duration = this@toDomainEntity.duration
bitRate = this@toDomainEntity.bitRate
path = this@toDomainEntity.path
setIsVideo(this@toDomainEntity.isVideo)
setCreated(this@toDomainEntity.created?.time)
starred = this@toDomainEntity.starred != null
discNumber = this@toDomainEntity.discNumber
type = this@toDomainEntity.type
}
fun APIMusicDirectory.toDomainEntity(): MusicDirectory = MusicDirectory().apply {
name = this@toDomainEntity.name
addAll(this@toDomainEntity.childList.map { it.toDomainEntity() })
}

View File

@ -8,6 +8,8 @@ import org.junit.Test
import org.moire.ultrasonic.api.subsonic.models.Artist
import org.moire.ultrasonic.api.subsonic.models.Index
import org.moire.ultrasonic.api.subsonic.models.Indexes
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
import java.util.Calendar
@ -78,6 +80,63 @@ class APIConverterTest {
}
}
@Test
fun `Should convert MusicDirectory entity`() {
val entity = MusicDirectory(id = 1982L, parent = 345L, name = "some-name", userRating = 3,
averageRating = 3.4f, starred = Calendar.getInstance(), playCount = 10,
childList = listOf(MusicDirectoryChild(1L), MusicDirectoryChild(2L)))
val convertedEntity = entity.toDomainEntity()
with(convertedEntity) {
name `should equal to` entity.name
children.size `should equal to` entity.childList.size
children `should equal` entity.childList.map { it.toDomainEntity() }.toMutableList()
}
}
@Test
fun `Should convert MusicDirectoryChild entity`() {
val entity = MusicDirectoryChild(id = 929L, parent = 11L, title = "some-title",
album = "some-album", albumId = 231L, artist = "some-artist", artistId = 1233L,
track = 12, year = 2002, genre = "some-genre", coverArt = 952L, size = 9418123L,
contentType = "some-content-type", suffix = "some-suffix",
transcodedContentType = "some-transcoded-content-type",
transcodedSuffix = "some-transcoded-suffix", duration = 11, bitRate = 256,
path = "some-path", isDir = true, isVideo = true, playCount = 323, discNumber = 2,
created = Calendar.getInstance(), type = "some-type", starred = Calendar.getInstance())
val convertedEntity = entity.toDomainEntity()
with(convertedEntity) {
id `should equal to` entity.id.toString()
parent `should equal to` entity.parent.toString()
isDirectory `should equal to` entity.isDir
title `should equal` entity.title
album `should equal` entity.album
albumId `should equal to` entity.albumId.toString()
artist `should equal to` entity.artist
artistId `should equal to` entity.artistId.toString()
track `should equal to` entity.track
year `should equal to` entity.year!!
genre `should equal to` entity.genre
contentType `should equal to` entity.contentType
suffix `should equal to` entity.suffix
transcodedContentType `should equal to` entity.transcodedContentType
transcodedSuffix `should equal to` entity.transcodedSuffix
coverArt `should equal to` entity.coverArt.toString()
size `should equal to` entity.size
duration `should equal to` entity.duration
bitRate `should equal to` entity.bitRate
path `should equal to` entity.path
isVideo `should equal to` entity.isVideo
created `should equal` entity.created?.time
starred `should equal to` (entity.starred != null)
discNumber `should equal to` entity.discNumber
type `should equal to` entity.type
}
}
private fun createMusicFolder(id: Long = 0, name: String = ""): MusicFolder =
MusicFolder(id, name)