Skip to content

Instantly share code, notes, and snippets.

@HamsterofDeath
Created July 4, 2023 19:45
Show Gist options
  • Save HamsterofDeath/cfe101f0eccd71ca6055099b69c47696 to your computer and use it in GitHub Desktop.
Save HamsterofDeath/cfe101f0eccd71ca6055099b69c47696 to your computer and use it in GitHub Desktop.
package hod.btlg.spacefighter
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.*
import akka.http.scaladsl.server.Directives.*
import io.getquill.{PostgresJdbcContext, SnakeCase}
import spray.json.DefaultJsonProtocol.*
import spray.json.*
import io.getquill.*
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.*
import akka.http.scaladsl.server.Directives.*
import spray.json.DefaultJsonProtocol.*
import spray.json.*
// Define your data structure
case class Score(name: String, score: Int)
// Define SpaceFighter object
object SpaceFighter {
implicit val scoreFormat: RootJsonFormat[Score] = jsonFormat2(Score.apply)
val ctx = new SqlMirrorContext(MirrorSqlDialect, Literal)
import ctx._
// Define a Quill query schema for your `scores` table
val scores = quote {
querySchema[Score]("spaceFighterScores")
}
ctx.run(scores)
lazy val routes = pathPrefix("spacefighter" / "scores") {
concat(
path("store") {
post {
entity(as[Score]) { score =>
// Use Quill to insert the score into the database
val insertScore = quote {
query[Score].insertValue(lift(score))
}
println(ctx.run(insertScore))
complete(score)
}
}
},
path("top") {
get {
// Use Quill to query the top 10 scores from the database
val ret = ArrayBuffer.empty[Score]
val selectTopScores = quote {
query[Score]
.sortBy(s => -s.score)
.take(10)
}
println(ctx.run(selectTopScores))
val data = ret.toArray
complete(data)
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment