package com.pixelized.biblib.database.dao

import androidx.paging.DataSource
import androidx.room.*
import com.pixelized.biblib.database.data.BookDbo
import com.pixelized.biblib.database.relation.BookRelation

@Dao
interface BookDao {

    @Query("SELECT COUNT(${BookDbo.ID}) FROM ${BookDbo.TABLE}")
    fun count(): Int

    @Transaction
    @Query("SELECT * FROM ${BookDbo.TABLE}")
    fun getAll(): List<BookRelation>

    @Transaction
    @Query("SELECT * FROM ${BookDbo.TABLE} WHERE ${BookDbo.IS_NEW} = 1 ORDER BY ${BookDbo.NEW_ORDER}")
    fun getNews(): DataSource.Factory<Int, BookRelation>

    @Transaction
    @Query("SELECT * FROM ${BookDbo.TABLE} ORDER BY ${BookDbo.SORT}")
    fun getBook(): DataSource.Factory<Int, BookRelation>

    @Transaction
    @Query("SELECT * FROM ${BookDbo.TABLE} WHERE ${BookDbo.ID} LIKE :id")
    fun getBook(id: Int): BookRelation

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(vararg books: BookDbo)

    @Delete
    fun delete(book: BookDbo)
}