-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cigith Pillai
committed
Oct 11, 2017
1 parent
ee365fb
commit 953a9bf
Showing
3 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |