Changes Playlist to PlaylistEntity
This commit is contained in:
parent
b5e07a31d8
commit
a93dcb0285
14 changed files with 115 additions and 89 deletions
|
@ -9,7 +9,7 @@ interface PlaylistDao {
|
|||
suspend fun checkPlaylistExists(name: String): List<PlaylistEntity>
|
||||
|
||||
@Insert
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity)
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long
|
||||
|
||||
@Query("SELECT * FROM PlaylistEntity")
|
||||
suspend fun playlists(): List<PlaylistEntity>
|
||||
|
@ -23,4 +23,7 @@ interface PlaylistDao {
|
|||
|
||||
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id = :playlistName AND song_id = :songId")
|
||||
suspend fun checkSongExistsWithPlaylistName(playlistName: String, songId: Int): List<SongEntity>
|
||||
|
||||
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id = :playlistId ORDER BY song_key")
|
||||
suspend fun getSongs(playlistId: Int): List<SongEntity>
|
||||
}
|
|
@ -1,12 +1,16 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Relation
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class PlaylistWithSongs(
|
||||
@Embedded val playlistEntity: PlaylistEntity,
|
||||
@Relation(
|
||||
parentColumn = "playlist_id",
|
||||
entityColumn = "playlist_creator_id"
|
||||
) val songs: List<SongEntity>
|
||||
)
|
||||
)
|
||||
val songs: List<SongEntity>
|
||||
):Parcelable
|
|
@ -4,16 +4,17 @@ import androidx.annotation.WorkerThread
|
|||
|
||||
|
||||
interface RoomPlaylistRepository {
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity)
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long
|
||||
suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity>
|
||||
suspend fun playlists(): List<PlaylistEntity>
|
||||
suspend fun playlistWithSongs(): List<PlaylistWithSongs>
|
||||
suspend fun insertSongs(songs: List<SongEntity>)
|
||||
suspend fun getSongs(playlistEntity: PlaylistEntity): List<SongEntity>
|
||||
}
|
||||
|
||||
class RealRoomPlaylistRepository(private val playlistDao: PlaylistDao) : RoomPlaylistRepository {
|
||||
@WorkerThread
|
||||
override suspend fun createPlaylist(playlistEntity: PlaylistEntity) =
|
||||
override suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long =
|
||||
playlistDao.createPlaylist(playlistEntity)
|
||||
|
||||
@WorkerThread
|
||||
|
@ -29,12 +30,16 @@ class RealRoomPlaylistRepository(private val playlistDao: PlaylistDao) : RoomPla
|
|||
|
||||
@WorkerThread
|
||||
override suspend fun insertSongs(songs: List<SongEntity>) {
|
||||
/* val tempList = ArrayList<SongEntity>(songs)
|
||||
val existingSongs = songs.map {
|
||||
playlistDao.checkSongExistsWithPlaylistName(it.playlistCreatorName, it.songId)
|
||||
}.first()
|
||||
println("Existing ${existingSongs.size}")
|
||||
tempList.removeAll(existingSongs)*/
|
||||
/* val tempList = ArrayList<SongEntity>(songs)
|
||||
val existingSongs = songs.map {
|
||||
playlistDao.checkSongExistsWithPlaylistName(it.playlistCreatorName, it.songId)
|
||||
}.first()
|
||||
println("Existing ${existingSongs.size}")
|
||||
tempList.removeAll(existingSongs)*/
|
||||
playlistDao.insertSongs(songs)
|
||||
}
|
||||
|
||||
override suspend fun getSongs(playlistEntity: PlaylistEntity): List<SongEntity> {
|
||||
return playlistDao.getSongs(playlistEntity.playListId)
|
||||
}
|
||||
}
|
|
@ -1,16 +1,19 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@Entity
|
||||
class SongEntity(
|
||||
@ColumnInfo(name = "song_id")
|
||||
val songId: Int,
|
||||
@ColumnInfo(name = "playlist_creator_id")
|
||||
val playlistCreatorId: Int
|
||||
) {
|
||||
) : Parcelable {
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "song_key")
|
||||
var songPrimaryKey: Long = 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue