This commit is contained in:
Sebastien Deleuze
2016-03-21 07:35:14 +01:00
parent e2e0f0b9a3
commit 9b7fbcfc9a
4 changed files with 24 additions and 17 deletions

View File

@@ -17,30 +17,30 @@ open class MessageRepository @Autowired constructor(val db: Database) {
}
open fun create(m: Message) = db.transaction {
m.id = Messages.insert(map(m)).get(Messages.id)
m.id = Messages.insert(toRow(m)).get(Messages.id)
m
}
open fun findAll() = db.transaction {
unmap(Messages.selectAll())
Messages.selectAll().map { fromRow(it) }
}
open fun findByBoundingBox(box: PGbox2d) = db.transaction {
unmap(Messages.select { Messages.location within box })
Messages.select { Messages.location within box }.map { fromRow(it) }
}
open fun deleteAll() = db.transaction {
Messages.deleteAll()
}
private fun map(m: Message): Messages.(UpdateBuilder<*>) -> Unit = {
fun toRow(m: Message): Messages.(UpdateBuilder<*>) -> Unit = {
if (m.id != null) it[id] = m.id
it[content] = m.content
it[author] = m.author
it[location] = m.location
}
private fun unmap(rows: SizedIterable<ResultRow>): List<Message> =
rows.map { Message(it[Messages.content], it[Messages.author], it[Messages.location], it[Messages.id]) }
fun fromRow(r: ResultRow) =
Message(r[Messages.content], r[Messages.author], r[Messages.location], r[Messages.id])
}

View File

@@ -17,10 +17,8 @@ open class UserRepository @Autowired constructor(val db: Database) {
create(Users)
}
open fun create(user: User) {
db.transaction {
Users.insert( map(user) )
}
open fun create(user: User) = db.transaction {
Users.insert( toRow(user) )
}
open fun updateLocation(userName:String, location: Point) = db.transaction {
@@ -29,25 +27,25 @@ open class UserRepository @Autowired constructor(val db: Database) {
}
open fun findAll() = db.transaction {
unmap(Users.selectAll())
Users.selectAll().map { fromRow(it) }
}
open fun findByBoundingBox(box: PGbox2d) = db.transaction {
unmap(Users.select { Users.location within box })
Users.select { Users.location within box }.map { fromRow(it) }
}
open fun deleteAll() = db.transaction {
Users.deleteAll()
}
private fun map(u: User): Users.(UpdateBuilder<*>) -> Unit = {
fun toRow(u: User): Users.(UpdateBuilder<*>) -> Unit = {
it[userName] = u.userName
it[firstName] = u.firstName
it[lastName] = u.lastName
it[location] = u.location
}
private fun unmap(rows: SizedIterable<ResultRow>): List<User> =
rows.map { User(it[Users.userName], it[Users.firstName], it[Users.lastName], it[Users.location]) }
fun fromRow(r: ResultRow) =
User(r[Users.userName], r[Users.firstName], r[Users.lastName], r[Users.location])
}

View File

@@ -2,6 +2,7 @@ package io.spring.messenger.web
import org.springframework.http.MediaType
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import java.io.IOException
import java.util.*
import java.util.Collections.synchronizedSet
@@ -18,7 +19,10 @@ class SseBroadcaster {
fun send(o:Any) {
synchronized (sseEmitters) {
sseEmitters.iterator().forEach { it.send(o, MediaType.APPLICATION_JSON) }
sseEmitters.iterator().forEach {
// Servlet containers don't always detect ghost connection, so we must catch exceptions ...
try { it.send(o, MediaType.APPLICATION_JSON) } catch (e: IOException) { }
}
}
}
}

View File

@@ -1,7 +1,12 @@
logging:
level:
org.springframework.web.servlet: INFO
spring:
datasource:
platform: "postgis"
driver-class-name: "org.postgis.DriverWrapper"
url: "jdbc:postgresql_postGIS://localhost/seb"
username: "postgres"
mvc:
async:
request-timeout: 1000000