Skip to content

Commit

Permalink
Implement a basic Finagle service
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvansson committed Feb 29, 2020
1 parent d79aee6 commit 16291ea
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
11 changes: 7 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
name := "socialnetwork"

version := "0.1"
version := "0.2"

scalaVersion := "2.13.1"
scalaVersion := "2.12.10"

libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "42.2.8",
"io.getquill" %% "quill-jdbc" % "3.5.0"
"com.github.finagle" %% "finch-core" % "0.31.0",
"com.github.finagle" %% "finch-circe" % "0.31.0",
"io.circe" %% "circe-generic" % "0.13.0",
"io.getquill" %% "quill-jdbc" % "3.5.0",
"org.postgresql" % "postgresql" % "42.2.10",
)
7 changes: 7 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jdbc {
dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource"
dataSource.user = "tom_from_myspace"
dataSource.databaseName = "socialnetwork"
dataSource.portNumber = 5432
dataSource.serverName = "localhost"
}
40 changes: 40 additions & 0 deletions src/main/scala/com/github/sylvansson/socialnetwork/Endpoints.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.sylvansson.socialnetwork
import java.util.UUID

import com.github.sylvansson.socialnetwork.Responses._
import com.twitter.util.Future
import io.circe.generic.auto._
import io.finch.circe._
import io.finch.syntax.{get, post}
import io.finch.{Endpoint, Ok, param, _}

object Endpoints {
def service = friendships.toService

private def friendships = {
val listAcceptedFriendships: Endpoint[Success[Seq[Friendship]]] =
get("friendships.accepted" :: param[UUID]("user")) { userId: UUID =>
Future(Friendship.findAccepted(userId))
.map(fs => Ok(Success("acceptedFriendships", fs)))
}

val listPendingFriendships: Endpoint[Success[Seq[Friendship]]] =
get("friendships.pending" :: param[UUID]("user")) { userId: UUID =>
Future(Friendship.findPending(userId))
.map(fs => Ok(Success("pendingFriendships", fs)))
}

// TODO: Ensure that only the requestee can accept a friendship,
// once authentication has been implemented.
val acceptFriendship: Endpoint[EmptySuccess] =
post("friendships.accept" :: param[UUID]("requester") :: param[UUID]("requestee")) {
(requesterId: UUID, requesteeId: UUID) =>
Future(Friendship.accept(requesterId, requesteeId))
.map(_ => Ok(EmptySuccess()))
}

listAcceptedFriendships :+:
listPendingFriendships :+:
acceptFriendship
}
}
25 changes: 25 additions & 0 deletions src/main/scala/com/github/sylvansson/socialnetwork/Responses.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.sylvansson.socialnetwork

import io.circe.{Encoder, Json}
import io.circe.syntax._

/**
* Case classes for responses. The response format is based on Slack's
* Web API: https://api.slack.com/web
*/
object Responses {
case class EmptySuccess()
object EmptySuccess {
implicit def encode: Encoder[EmptySuccess] = (_: EmptySuccess) =>
Json.obj("ok" -> Json.fromBoolean(true))
}

case class Success[T](property: String, data: T)
object Success {
implicit def encode[T](implicit encodeT: Encoder[T]): Encoder[Success[T]] = (s: Success[T]) =>
Json.obj(
"ok" -> Json.fromBoolean(true),
s.property -> s.data.asJson
)
}
}
11 changes: 11 additions & 0 deletions src/main/scala/com/github/sylvansson/socialnetwork/Service.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.sylvansson.socialnetwork

import com.github.sylvansson.socialnetwork.Endpoints._
import com.twitter.finagle.Http
import com.twitter.util.Await

object Service extends App {
Await.ready(
Http.serve(":8080", service)
)
}
11 changes: 10 additions & 1 deletion src/main/scala/com/github/sylvansson/socialnetwork/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.UUID
import io.getquill._

package object socialnetwork {
val ctx = new PostgresJdbcContext(NamingStrategy(SnakeCase, PluralizedTableNames), "ctx")
val ctx = new PostgresJdbcContext(NamingStrategy(SnakeCase, PluralizedTableNames), "jdbc")
import ctx._

case class User(id: UUID) {
Expand Down Expand Up @@ -53,5 +53,14 @@ package object socialnetwork {
def findPending(userId: UUID) = find(userId, Types.Pending)
def findAccepted(userId: UUID) = find(userId, Types.Accepted)
def findAll(userId: UUID) = find(userId, Types.All)

def accept(requesterId: UUID, requesteeId: UUID): Long =
ctx.run(
query
.filter(_.requesterId == lift(requesterId))
.filter(_.requesteeId == lift(requesteeId))
.filter(_.since.isEmpty)
.update(_.since -> lift(Option(LocalDateTime.now)))
)
}
}

0 comments on commit 16291ea

Please sign in to comment.