Skip to content

Commit

Permalink
Answer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cigith Pillai committed Oct 11, 2017
1 parent ee365fb commit 953a9bf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
28 changes: 28 additions & 0 deletions app/controllers/AnswerController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package controllers

import javax.inject.Singleton

import play.api.libs.json.{Format, JsValue, Json}
import play.api.mvc._
import services.logic.AnswerHandler
import services.model.Answer
import services.model.Answer.AnswerReads
import scala.concurrent.ExecutionContext.Implicits.global

/**
* Created by pillaci on 10/11/17.
*/

@Singleton
class AnswerController extends Controller{

implicit val answerParser: Format[Answer] = Json.format[Answer]

def create = Action.async(BodyParsers.parse.json){ request =>
val k = AnswerReads.reads(request.body)
AnswerHandler.create(k.get.authorId, k.get.questionId, k.get.body) map { a =>
Created(a)
}
}
}

21 changes: 21 additions & 0 deletions app/services/logic/AnswerHandler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services.logic

import java.util.UUID

import services.model.Answer

import scala.concurrent.Future

/**
* Created by pillaci on 10/11/17.
*/
object AnswerHandler {

def create(authorId: UUID, questionId: UUID, body: String): Future[String] = {
val a = Answer(authorId, questionId, body)
Future.successful(a.id.toString)
}

def get

}
40 changes: 35 additions & 5 deletions app/services/model/Answer.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
package services.model

/**
* Created by v723840 on 10/11/17.
*/
class Answer {
import java.util.UUID

}
import org.joda.time.DateTime
import play.api.libs.json._

case class Answer(
id: UUID = UUID.randomUUID(),
authorId: UUID,
questionId: UUID,
body: String,
createdAt: DateTime)

object Answer {
implicit object AnswerReads extends Format[Answer]{
override def reads (json: JsValue): JsResult[Answer] = JsSuccess{
Answer(
(json \ "authorId").as[UUID],
(json \ "questionId").as[UUID],
(json \ "body").as[String]
)
}

override def writes(ts: Answer): JsValue =
JsObject(Seq(
"id" -> JsString(ts.id.toString),
"authorId" -> JsString(ts.authorId.toString),
"questionId" -> JsString(ts.questionId.toString),
"body" -> JsString(ts.body.toString),
"createdAt" -> JsString(ts.createdAt.toString)
))
}

def apply(authorId: UUID, questionId: UUID, body: String): Answer = {
Answer(UUID.randomUUID(), authorId, questionId, body, new DateTime())
}
}

0 comments on commit 953a9bf

Please sign in to comment.