package com.eddsteel.posts.leastpower

import scala.util.{Try, Success}
import scala.concurrent.Future

package object webapp {
  type Request = String
  type Response = String
  type Action = (Request) => Future[Response]
  sealed trait Method
  case object POST extends Method

  trait Controller {
    def route(method: Method, path: String): Action
  }

  case class NotFoundException[A](a: A) extends RuntimeException(a.toString)
}

object WebApp {
  import webapp._

  def mkResponse[T](t: T): Try[Response] =
    Success(t.toString)

  def mkErrorResponse: PartialFunction[Throwable, Response] = {
    case _ => "An error occurred. Sad face."
  }


  def extractAndValidateUserId(req: Request): Try[Long] =
    Success(1L)

  def postFakeRequest(controller: Controller, method: Method, path: String, req: Request): Future[Response] =
    controller.route(method, path)(req)
}