Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.19 KB | None | 0 0
  1. import java.time.Instant
  2.  
  3. import akka.actor.ActorSystem
  4. import akka.http.scaladsl.Http
  5. import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
  6. import akka.http.scaladsl.model.StatusCodes
  7. import akka.http.scaladsl.server.Directives._
  8. import akka.http.scaladsl.server.Route
  9. import akka.stream.ActorMaterializer
  10. import spray.json.DefaultJsonProtocol._
  11. import spray.json.RootJsonFormat
  12.  
  13. import scala.concurrent.{ExecutionContextExecutor, Future}
  14.  
  15. object SimpleScheduler {
  16.  
  17.   final case class Schedule(id: Long, cron: String, start: Instant, finish: Instant)
  18.  
  19.   final case class ScheduleDto(scheduleId: Long, cron: String, start: String, finish: String)
  20.  
  21.   final case class ScheduleCreateDto(cron: String, start: String, finish: String)
  22.  
  23.   object ScheduleIdentifier {
  24.     private var idCounter = 0L
  25.  
  26.     def next(): Long = {
  27.       idCounter += 1
  28.       idCounter
  29.     }
  30.   }
  31.  
  32.   implicit val system: ActorSystem = ActorSystem("simple-scheduler")
  33.   implicit val materializer: ActorMaterializer = ActorMaterializer()
  34.   implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  35.   implicit val scheduleDtoFormat: RootJsonFormat[ScheduleDto] = jsonFormat4(ScheduleDto)
  36.   implicit val scheduleCreateDtoFormat: RootJsonFormat[ScheduleCreateDto] = jsonFormat3(ScheduleCreateDto)
  37.  
  38.   var schedules: List[Schedule] = Nil
  39.  
  40.   def fetchScheduleById(scheduleId: Long): Future[Option[ScheduleDto]] = Future {
  41.     schedules
  42.       .find(p => p.id == scheduleId)
  43.       .map(s => ScheduleDto(s.id, s.cron, s.start.toString, s.finish.toString))
  44.   }
  45.  
  46.   def fetchSchedules(): List[ScheduleDto] = {
  47.     schedules.map(s => ScheduleDto(s.id, s.cron, s.start.toString, s.finish.toString))
  48.   }
  49.  
  50.   def saveSchedule(create: ScheduleCreateDto): Future[ScheduleDto] = Future {
  51.     val schedule = Schedule(ScheduleIdentifier.next(), create.cron, Instant.parse(create.start), Instant.parse(create.finish))
  52.     schedules = schedules ::: List(schedule)
  53.     ScheduleDto(schedule.id, schedule.cron, schedule.start.toString, schedule.finish.toString)
  54.   }
  55.  
  56.   def main(args: Array[String]): Unit = {
  57.     schedules = schedules ::: List(
  58.       Schedule(ScheduleIdentifier.next(), "0 0 10 ? * * *", Instant.now(), Instant.now()),
  59.       Schedule(ScheduleIdentifier.next(), "0 60 * ? * * *", Instant.now(), Instant.now()))
  60.  
  61.     Http().bindAndHandle(route, "127.0.0.1", 8080)
  62.     println("Akka HTTP server online at http://127.0.0.1:8080/")
  63.   }
  64.  
  65.  
  66.   def getAllSchedulesRoute: Route = get {
  67.     path("schedules") {
  68.       complete(fetchSchedules())
  69.     }
  70.   }
  71.  
  72.   def getOneScheduleByIdRoute: Route = get {
  73.     path("schedules" / LongNumber) { id => {
  74.       val maybeSchedule: Future[Option[ScheduleDto]] = fetchScheduleById(id)
  75.       onSuccess(maybeSchedule) {
  76.         case Some(schedule) => complete(schedule)
  77.         case None => complete(StatusCodes.NotFound)
  78.       }
  79.     }
  80.     }
  81.   }
  82.  
  83.   def postOneScheduleRoute: Route = post {
  84.     path("schedules") {
  85.       entity(as[ScheduleCreateDto]) {
  86.         schedule => {
  87.           complete(saveSchedule(schedule))
  88.         }
  89.       }
  90.     }
  91.   }
  92.  
  93.   def route: Route =
  94.     getAllSchedulesRoute ~
  95.       getOneScheduleByIdRoute ~
  96.       postOneScheduleRoute
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement