<commit_before>package component

import core._

import akka.actor.ActorSelection
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.headers.Accept
import akka.http.scaladsl.server.{Route, RouteResult}
import akka.http.scaladsl.server.Directives._
import java.util.UUID
import org.joda.time.DateTime

trait BlogDirectives extends CommentDirectives
  with CommonDirectives with BlogFormats with CommentFormats
{
  def modelBlog: ActorSelection

  def handleBlogs = pathEnd {
    respondWithLinks(
      blogListLink("self"),
      blogItemLink("item", methods = List(GET, PUT, DELETE)),
      blogItemLink("new", "new"))
      {
        headComplete ~
        getList[Blog](modelBlog, Blog)()
      }
  }

  def handleNewBlogs = path("new") {
    get {
      val uuid = UUID.randomUUID.toString
      respondWithLinks(blogItemLink("self", uuid, methods = List(GET, PUT))) {
        complete(Blog(uuid, "john", DateTime.now, "", ""))
      }
    }
  }

  def handleBlog(blogId: String) = pathEnd {
    respondWithLinks(
      blogListLink("blogs"),
      blogItemLink("self", blogId, methods = List(GET, PUT, DELETE)),
      commentListLink(blogId, "comments", GET),
      commentItemLink(blogId, "new", PUT))
      {
        headComplete ~
        getEntity[Blog](modelBlog, blogId) ~
        putEntity[Blog](modelBlog, _.copy(id = blogId), blogId) ~
        deleteEntity[Blog](modelBlog, blogId)
      }
  } ~
  pathPrefix("comments") {
    blogLinks { handleComments(blogId) } ~
    pathPrefix(Segment)(handleComment(blogId) _)
  }

  def blogListLink(rel: String, methods: List[HttpMethod] = List(GET)) =
    collectionLink("/blogs", rel, "List Blogs",
      "accountId date:date title note", methods:_*)

  def blogItemLink(rel: String, blogId: String = ":blogId",
    methods: List[HttpMethod] = List(GET)) =
    mtLink(s"/blogs/$blogId", rel, `application/vnd.enpassant.blog+json`,
      methods:_*)<commit_msg>Control Blog item methods<commit_after>package component

import core._

import akka.actor.ActorSelection
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.headers.Accept
import akka.http.scaladsl.server.{Directive1, Route, RouteResult}
import akka.http.scaladsl.server.Directives._
import akka.pattern.ask
import java.util.UUID
import org.joda.time.DateTime
import scala.concurrent.Future

trait BlogDirectives extends CommentDirectives
  with CommonDirectives with BlogFormats with CommentFormats
{
  import scala.concurrent.ExecutionContext.Implicits.global

  def modelBlog: ActorSelection

  def handleBlogs = pathEnd {
    respondWithLinks(
      blogListLink("self"),
      blogItemLink("item", methods = List(GET, PUT, DELETE)),
      blogItemLink("new", "new"))
      {
        headComplete ~
        getList[Blog](modelBlog, Blog)()
      }
  }

  def handleNewBlogs = path("new") {
    get {
      val uuid = UUID.randomUUID.toString
      respondWithLinks(blogItemLink("self", uuid, methods = List(PUT))) {
        complete(Blog(uuid, "john", DateTime.now, "", ""))
      }
    }
  }

  def handleBlog(blogId: String) = pathEnd {
    getBlogDirective(blogId) {
      case Some(blog: Blog) => {
        respondBlogLinks(blogId, GET, PUT, DELETE) {
          headComplete ~
          getEntity[Blog](modelBlog, blogId) ~
          putEntity[Blog](modelBlog, _.copy(id = blogId), blogId) ~
          deleteEntity[Blog](modelBlog, blogId)
        }
      }
      case None =>
        respondBlogLinks(blogId, PUT) {
          headComplete ~
          putEntity[Blog](modelBlog, _.copy(id = blogId), blogId)
        }
    }
  } ~
  pathPrefix("comments") {
    blogLinks { handleComments(blogId) } ~
    pathPrefix(Segment)(handleComment(blogId) _)
  }

  def respondBlogLinks(blogId: String, methods: HttpMethod*) = respondWithLinks(
      blogListLink("blogs"),
      blogItemLink("self", blogId, methods.toList),
      commentListLink(blogId, "comments", GET),
      commentItemLink(blogId, "new", GET)
  )

  def getBlogDirective(blogId: String): Directive1[Option[Blog]] = {
    onSuccess(modelBlog ? GetEntity(blogId)) flatMap {
      case Some(blog: Blog) => provide(Some(blog))
      case None => provide(None)
    }
  }

  def blogListLink(rel: String, methods: List[HttpMethod] = List(GET)) =
    collectionLink("/blogs", rel, "List Blogs",
      "accountId date:date title note", methods:_*)

  def blogItemLink(rel: String, blogId: String = ":blogId",
    methods: List[HttpMethod] = List(GET)) =
    mtLink(s"/blogs/$blogId", rel, `application/vnd.enpassant.blog+json`,
      methods:_*)
{'fraction_non_alphanumeric': 0.12871287128712872, 'fraction_numerical': 0.00043047783039173483, 'mean_word_length': 3.5425219941348973, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '361764', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1460, 'n_words': 287}