parent
bf4d530ac0
commit
e064615a9d
@ -0,0 +1,22 @@
|
|||||||
|
package fr.uca.iut.urbandictionarylight.data
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import fr.uca.iut.urbandictionarylight.model.Definition
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface DefinitionDao {
|
||||||
|
@Query("SELECT * FROM definitions")
|
||||||
|
fun queryAll(): Set<Definition>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM entries WHERE id == :id")
|
||||||
|
fun query(id: Int): Definition?
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insert(entry: Definition)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
fun update(entry: Definition)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
fun delete(entry: Definition)
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package fr.uca.iut.urbandictionarylight.data
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import fr.uca.iut.urbandictionarylight.model.EntryWithDefinitions
|
||||||
|
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface EntryDao {
|
||||||
|
@Query("SELECT * FROM entries")
|
||||||
|
fun queryAll(): List<EntryWithDefinitions>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM entries WHERE id == :id")
|
||||||
|
fun query(id: Int): EntryWithDefinitions?
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insert(entry: EntryWithDefinitions)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
fun update(entry: EntryWithDefinitions)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
fun delete(entry: EntryWithDefinitions)
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
package fr.uca.iut.urbandictionarylight.data
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import fr.uca.iut.urbandictionarylight.model.Definition
|
||||||
|
import fr.uca.iut.urbandictionarylight.model.Entry
|
||||||
|
import fr.uca.iut.urbandictionarylight.model.EntryWithDefinitions
|
||||||
|
|
||||||
|
private const val DB_FILE = "u_d_light.db"
|
||||||
|
|
||||||
|
@Database(entities = [Entry::class, Definition::class], version = 1)
|
||||||
|
abstract class UrbanDictionaryDatabase : RoomDatabase() {
|
||||||
|
|
||||||
|
abstract fun entryDao(): EntryDao
|
||||||
|
abstract fun definitionDao(): DefinitionDao
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private lateinit var application: Application
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var instance: UrbanDictionaryDatabase? = null
|
||||||
|
|
||||||
|
fun getInstance(): UrbanDictionaryDatabase {
|
||||||
|
if (Companion::application.isInitialized) {
|
||||||
|
if (instance == null)
|
||||||
|
synchronized(this) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = Room.databaseBuilder(
|
||||||
|
application.applicationContext,
|
||||||
|
UrbanDictionaryDatabase::class.java,
|
||||||
|
DB_FILE
|
||||||
|
)
|
||||||
|
.allowMainThreadQueries()
|
||||||
|
.build()
|
||||||
|
|
||||||
|
instance?.entryDao()?.let {
|
||||||
|
if (it.queryAll().isEmpty()) emptyDatabaseStub(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance!!
|
||||||
|
} else
|
||||||
|
throw RuntimeException("the database must be initialized first")
|
||||||
|
}
|
||||||
|
|
||||||
|
//????
|
||||||
|
@Synchronized
|
||||||
|
fun initialize(app: Application) {
|
||||||
|
if (Companion::application.isInitialized)
|
||||||
|
throw RuntimeException("the database must not be initialized twice")
|
||||||
|
|
||||||
|
application = app
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun emptyDatabaseStub(
|
||||||
|
entryDao: EntryDao,
|
||||||
|
definitionDao: DefinitionDao
|
||||||
|
): () -> Unit = {
|
||||||
|
|
||||||
|
val def1 = Definition("bleep", "bleep blap bloop", 1)
|
||||||
|
val def2 = Definition("blarp", "bleeeep blap bloop", 7)
|
||||||
|
val def3 = Definition("blep", "bleep blap blooop", 4)
|
||||||
|
val def4 = Definition("blupo", "bleep blaap bloop", 56)
|
||||||
|
|
||||||
|
with(definitionDao) {
|
||||||
|
insert(def1)
|
||||||
|
insert(def2)
|
||||||
|
insert(def3)
|
||||||
|
insert(def4)
|
||||||
|
}
|
||||||
|
with(entryDao) {
|
||||||
|
insert(
|
||||||
|
EntryWithDefinitions(
|
||||||
|
Entry("shrimping"),
|
||||||
|
listOf(def1, def4)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
insert(
|
||||||
|
EntryWithDefinitions(
|
||||||
|
Entry("blunderkegel"),
|
||||||
|
listOf(def2)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
insert(
|
||||||
|
EntryWithDefinitions(
|
||||||
|
Entry("something horrible and dumb"),
|
||||||
|
listOf(def3)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,14 @@
|
|||||||
package fr.uca.iut.urbandictionarylight.model
|
package fr.uca.iut.urbandictionarylight.model
|
||||||
|
|
||||||
data class Definition(val content: String, val example: String) {
|
import androidx.room.Entity
|
||||||
private var upvotes: UInt = 0u
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
fun getUpvotes() = upvotes
|
const val NEW_DEFINITION_ID = 0L
|
||||||
|
|
||||||
fun upvote() {
|
@Entity(tableName = "definitions")
|
||||||
upvotes++
|
data class Definition(
|
||||||
}
|
val content: String,
|
||||||
}
|
val example: String,
|
||||||
|
var upvotes: Int = 0,
|
||||||
|
@PrimaryKey(autoGenerate = true) val id: Long = NEW_DEFINITION_ID
|
||||||
|
)
|
@ -1,61 +1,12 @@
|
|||||||
package fr.uca.iut.urbandictionarylight.model
|
package fr.uca.iut.urbandictionarylight.model
|
||||||
|
|
||||||
import fr.uca.iut.urbandictionarylight.utils.Sorting
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
class Entry(val phrase: String) {
|
const val NEW_ENTRY_ID = 0L
|
||||||
private val definitions: MutableSet<Definition> = mutableSetOf()
|
|
||||||
|
|
||||||
fun createDefinition(definition: Definition) {
|
@Entity(tableName = "entries")
|
||||||
if (definition.content.isNotBlank() && definition.example.isNotBlank()) {
|
data class Entry(
|
||||||
definitions.add(definition)
|
val phrase: String,
|
||||||
}
|
@PrimaryKey(autoGenerate = true) val id: Long = NEW_ENTRY_ID
|
||||||
}
|
|
||||||
|
|
||||||
fun readDefinition(id: Int): Definition {
|
|
||||||
// TODO implement once ORM is in place
|
|
||||||
return Definition(
|
|
||||||
content = "poopy butt",
|
|
||||||
example = "Yeah, that's right -- poopy butt. This method hasn't been implemented yet."
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
fun readAllDefinitions() = definitions.toSet()
|
|
||||||
|
|
||||||
// TODO implement
|
|
||||||
fun readAllDefinitions(criterion: Sorting): Set<Definition> {
|
|
||||||
when (criterion) {
|
|
||||||
Sorting.ASC -> {
|
|
||||||
print("implement asc sorting!")
|
|
||||||
return readAllDefinitions()
|
|
||||||
}
|
|
||||||
Sorting.DESC -> {
|
|
||||||
print("implement desc sorting!")
|
|
||||||
return readAllDefinitions()
|
|
||||||
}
|
|
||||||
else -> return readAllDefinitions()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateDefinition(definition: Definition) {
|
|
||||||
// TODO implement once ORM is in place
|
|
||||||
// removeDefinition(definition.id)
|
|
||||||
createDefinition(definition)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO throw this away soon
|
|
||||||
fun updateDefinition(old: Definition, new: Definition) {
|
|
||||||
deleteDefinition(old)
|
|
||||||
createDefinition(new)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteDefinition(definition: Definition) {
|
|
||||||
definitions.remove(definition)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteDefinition(id: Int) {
|
|
||||||
// TODO implement once ORM is in place
|
|
||||||
// definitions.removeIf { def -> id == def.id }
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO redefine equals and hashset
|
|
||||||
}
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.uca.iut.urbandictionarylight.model
|
||||||
|
|
||||||
|
import androidx.room.Embedded
|
||||||
|
import androidx.room.Relation
|
||||||
|
|
||||||
|
data class EntryWithDefinitions(
|
||||||
|
@Embedded val entry: Entry,
|
||||||
|
@Relation(
|
||||||
|
parentColumn = "id",
|
||||||
|
entityColumn = "entryId"
|
||||||
|
) val definitions: List<Definition>
|
||||||
|
)
|
Loading…
Reference in new issue