Skip to content

Commit

Permalink
[+] Added top sightings by country, updates for the mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
adavis committed May 26, 2018
1 parent 5d2eb60 commit 151eede
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
49 changes: 34 additions & 15 deletions src/main/kotlin/info/adavis/dao/UFOSightingDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ fun ResultRow.toCountrySightings() = CountrySightings(numOccurrences = this[2]).
country = this@toCountrySightings[1]
}

class UFOSightingDatabase(val db: DatabaseConnection = H2Connection.createMemoryConnection()) : UFOSightingStorage {
class UFOSightingDatabase(
val db: DatabaseConnection = H2Connection.createMemoryConnection(catalogue = "DB_CLOSE_DELAY=-1")
) : UFOSightingStorage {

init {
db.transaction {
Expand All @@ -47,7 +49,7 @@ class UFOSightingDatabase(val db: DatabaseConnection = H2Connection.createMemory
row?.toUFOSighting()
}

override fun getAll(size: Long): List<UFOSighting> = db.transaction {
override fun getAll(size: Long): List<UFOSighting> = db.transaction {
from(UFOSightings)
.select()
.orderBy(UFOSightings.date, ascending = false)
Expand All @@ -57,29 +59,46 @@ class UFOSightingDatabase(val db: DatabaseConnection = H2Connection.createMemory
.toList()
}

override fun getTopSightings(): List<CountrySightings> = db.transaction {
override fun getTopSightings(): List<CountrySightings> = db.transaction {
from(UFOSightings)
.select(UFOSightings.state, UFOSightings.country, UFOSightings.state.count())
.groupBy(UFOSightings.state, UFOSightings.country)
.orderBy(UFOSightings.state.count(), ascending = false)
.where { UFOSightings.country eq "" }
.limit(10)
.execute()
.map { it.toCountrySightings() }
.toList()
}

override fun createSighting(sighting: UFOSighting) = db.transaction {
insertInto(UFOSightings).values {
it[date] = sighting.date
it[city] = sighting.city
it[state] = sighting.state
it[country] = sighting.country
it[shape] = sighting.shape
it[duration] = sighting.duration.toBigDecimal()
it[comments] = sighting.comments
it[latitude] = sighting.latitude.toBigDecimal()
it[longitude] = sighting.longitude.toBigDecimal()
}.fetch(id).execute()
override fun getTopCountrySightings(): List<CountrySightings> = db.transaction {
from(UFOSightings)
.select(UFOSightings.country, UFOSightings.country.count())
.groupBy(UFOSightings.country)
.orderBy(UFOSightings.country.count(), ascending = false)
.limit(10)
.execute()
.map { CountrySightings(numOccurrences = it[1]).apply { country = it[0] } }
.toList()
}

override fun createSighting(sighting: UFOSighting): UFOSighting {
val id = db.transaction {
insertInto(UFOSightings).values {
it[date] = sighting.date
it[city] = sighting.city
it[state] = sighting.state
it[country] = sighting.country
it[shape] = sighting.shape
it[duration] = sighting.duration.toBigDecimal()
it[comments] = sighting.comments
it[latitude] = sighting.latitude.toBigDecimal()
it[longitude] = sighting.longitude.toBigDecimal()
}.fetch(id).execute()
}

sighting.id = id
return sighting
}

override fun close() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/info/adavis/dao/UFOSightingStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import java.io.Closeable

interface UFOSightingStorage : Closeable {

fun createSighting(sighting: UFOSighting): Int
fun createSighting(sighting: UFOSighting): UFOSighting

fun getSighting(id: Int): UFOSighting?

fun getAll(size: Long): List<UFOSighting>

fun getTopSightings(): List<CountrySightings>

fun getTopCountrySightings(): List<CountrySightings>
}
10 changes: 10 additions & 0 deletions src/main/kotlin/info/adavis/graphql/AppSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ class AppSchema(private val storage: UFOSightingStorage) {
}

query("sightings") {
description = "Returns a subset of the UFO Sighting records"

resolver { size: Int? -> storage.getAll(size?.toLong() ?: 10) }.withArgs {
arg<Long> { name = "size"; defaultValue = 10; description = "The number of records to return" }
}
}

query("sighting") {
description = "Returns a single UFO Sighting record based on the id"

resolver { id: Int -> storage.getSighting(id) ?: throw NotFoundException("Sighting with id: $id does not exist") }
}

Expand All @@ -36,6 +40,12 @@ class AppSchema(private val storage: UFOSightingStorage) {
resolver(storage::getTopSightings)
}

query("topCountrySightings") {
description = "Returns a list of the top 10 countries based on the number of sightings"

resolver(storage::getTopCountrySightings)
}

mutation("createUFOSighting") {
description = "Adds a new UFO Sighting to the database"

Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/info/adavis/graphql/CreateUFOSightingInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import java.time.LocalDate

fun CreateUFOSightingInput.toUFOSighting() : UFOSighting {
return UFOSighting(
date = this.date,
date = this.date ?: LocalDate.now(),
city = this.city,
state = this.state,
country = this.country,
shape = this.shape,
duration = this.duration,
duration = this.duration ?: 0.0,
comments = this.comments,
latitude = this.latitude,
longitude = this.longitude
latitude = this.latitude ?: 0.0,
longitude = this.longitude ?: 0.0
)
}

data class CreateUFOSightingInput(
var date: LocalDate = LocalDate.now(),
var date: LocalDate? = LocalDate.now(),
var city: String? = "",
var state: String? = "",
var country: String? = "",
var shape: String? = "",
var duration: Double = 0.0,
var duration: Double? = 0.0,
var comments: String? = "",
var latitude: Double = 0.0,
var longitude: Double = 0.0
var latitude: Double? = 0.0,
var longitude: Double? = 0.0
)

0 comments on commit 151eede

Please sign in to comment.