Skip to content

Commit

Permalink
Refactor sponsor database
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Feb 1, 2018
1 parent 2a9e080 commit aa186e6
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "0a5f68c7db582871e77d067060b90ea6",
"identityHash": "5b9cb42652b6813a5d5e0b8abc21fa21",
"entities": [
{
"tableName": "contributor",
Expand Down Expand Up @@ -343,155 +343,11 @@
]
}
]
},
{
"tableName": "sponsor_plan",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `type` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "sponsor_group",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `plan_id` INTEGER NOT NULL, FOREIGN KEY(`plan_id`) REFERENCES `sponsor_plan`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "planId",
"columnName": "plan_id",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_sponsor_group_plan_id",
"unique": false,
"columnNames": [
"plan_id"
],
"createSql": "CREATE INDEX `index_sponsor_group_plan_id` ON `${TABLE_NAME}` (`plan_id`)"
}
],
"foreignKeys": [
{
"table": "sponsor_plan",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"plan_id"
],
"referencedColumns": [
"id"
]
}
]
},
{
"tableName": "sponsor",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `group_id` INTEGER NOT NULL, `link` TEXT NOT NULL, `base64_img` TEXT, `img_url` TEXT, FOREIGN KEY(`group_id`) REFERENCES `sponsor_group`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "groupId",
"columnName": "group_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "link",
"columnName": "link",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "base64Img",
"columnName": "base64_img",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "imgUrl",
"columnName": "img_url",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_sponsor_group_id",
"unique": false,
"columnNames": [
"group_id"
],
"createSql": "CREATE INDEX `index_sponsor_group_id` ON `${TABLE_NAME}` (`group_id`)"
}
],
"foreignKeys": [
{
"table": "sponsor_group",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"group_id"
],
"referencedColumns": [
"id"
]
}
]
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"0a5f68c7db582871e77d067060b90ea6\")"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"5b9cb42652b6813a5d5e0b8abc21fa21\")"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 8,
"identityHash": "a8182d92b66f2427d46cbb6ce712f988",
"identityHash": "ae0e2eb76da85daac1fce81e17c4a66b",
"entities": [
{
"tableName": "contributor",
Expand Down Expand Up @@ -332,41 +332,9 @@
],
"foreignKeys": []
},
{
"tableName": "sponsor_plan",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `type` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "sponsor",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `group_index` INTEGER NOT NULL, `plan_id` INTEGER NOT NULL, `link` TEXT NOT NULL, `base64_img` TEXT, `img_url` TEXT, FOREIGN KEY(`plan_id`) REFERENCES `sponsor_plan`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `group_index` INTEGER NOT NULL, `link` TEXT NOT NULL, `base64_img` TEXT, `img_url` TEXT, `sponsor_plan_name` TEXT NOT NULL, `sponsor_plan_type` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
Expand All @@ -380,12 +348,6 @@
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "planId",
"columnName": "plan_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "link",
"columnName": "link",
Expand All @@ -403,6 +365,18 @@
"columnName": "img_url",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "plan.name",
"columnName": "sponsor_plan_name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "plan.type",
"columnName": "sponsor_plan_type",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
Expand All @@ -411,34 +385,13 @@
],
"autoGenerate": true
},
"indices": [
{
"name": "index_sponsor_plan_id",
"unique": false,
"columnNames": [
"plan_id"
],
"createSql": "CREATE INDEX `index_sponsor_plan_id` ON `${TABLE_NAME}` (`plan_id`)"
}
],
"foreignKeys": [
{
"table": "sponsor_plan",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"plan_id"
],
"referencedColumns": [
"id"
]
}
]
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"a8182d92b66f2427d46cbb6ce712f988\")"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"ae0e2eb76da85daac1fce81e17c4a66b\")"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package io.github.droidkaigi.confsched2018.data.api.response.mapper

import io.github.droidkaigi.confsched2018.data.api.response.SponsorGroup
import io.github.droidkaigi.confsched2018.data.api.response.SponsorPlan
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorEntity
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorPlanEntity


fun List<SponsorGroup>.toSponsorGroupEntities(planId: Int): List<SponsorEntity> =
mapIndexed { index, group ->
group.sponsors.map { sponsor ->
SponsorEntity(
groupIndex = index,
planId = planId,
link = sponsor.link,
base64Img = sponsor.base64Img,
imgUrl = sponsor.imgUrl)
}
fun List<SponsorPlan>.toSponsorsEntities(): List<SponsorEntity> =
map { plan ->
plan.groups.mapIndexed { index, group ->
group.sponsors.map { sponsor ->
SponsorEntity(
groupIndex = index,
plan = plan.toSponsorEntity(),
link = sponsor.link,
base64Img = sponsor.base64Img,
imgUrl = sponsor.imgUrl)
}
}.flatten()
}.flatten()

fun List<SponsorPlan>.toSponsorPlanEntities(): List<SponsorPlanEntity> =
map { plan ->
SponsorPlanEntity(name = plan.planName,
type = plan.planType.toSponsorType())
}
fun SponsorPlan.toSponsorEntity(): SponsorPlanEntity {
return SponsorPlanEntity(name = planName,
type = planType.toSponsorType())
}

private fun SponsorPlan.Type.toSponsorType(): SponsorPlanEntity.Type = when (this) {
SponsorPlan.Type.PLATINUM -> SponsorPlanEntity.Type.PLATINUM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import io.github.droidkaigi.confsched2018.data.db.entity.SessionFeedbackEntity
import io.github.droidkaigi.confsched2018.data.db.entity.SessionSpeakerJoinEntity
import io.github.droidkaigi.confsched2018.data.db.entity.SpeakerEntity
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorEntity
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorPlanEntity
import io.github.droidkaigi.confsched2018.data.db.entity.mapper.Converters

@Database(
Expand All @@ -25,7 +24,6 @@ import io.github.droidkaigi.confsched2018.data.db.entity.mapper.Converters
(SpeakerEntity::class),
(SessionSpeakerJoinEntity::class),
(SessionFeedbackEntity::class),
(SponsorPlanEntity::class),
(SponsorEntity::class)
],
version = 8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.github.droidkaigi.confsched2018.data.db

import io.github.droidkaigi.confsched2018.data.api.response.SponsorPlan
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorPlanWithSponsor
import io.github.droidkaigi.confsched2018.data.db.entity.SponsorEntity
import io.reactivex.Flowable

interface SponsorDatabase {

fun getAllSponsorPlan(): Flowable<List<SponsorPlanWithSponsor>>
fun getSponsors(): Flowable<List<SponsorEntity>>

fun save(plans: List<SponsorPlan>)
fun save(sponsorEntities: List<SponsorEntity>)
}
Loading

0 comments on commit aa186e6

Please sign in to comment.