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
package models | |
import java.sql.Date | |
import play.Logger | |
import scala.util.{ Try, Success, Failure } | |
import play.cache.Cache | |
import org.mindrot.jbcrypt.BCrypt | |
import play.api.libs.Crypto | |
import play.libs.Scala | |
import be.objectify.deadbolt.core.models.Subject |
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
package models | |
//import play.api.db._ | |
import java.sql.Date | |
import play.Logger | |
import scala.util.{ Try, Success, Failure } | |
import play.cache.Cache | |
import org.mindrot.jbcrypt.BCrypt | |
import play.api.libs.Crypto | |
import play.libs.Scala |
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
implicit val ArticleCreate = Json.reads[ArticleForCreation] | |
def create = IsAuthenticated { username => | |
implicit request => | |
userIdOpt.map { userId => | |
request.body.asJson.map { json => | |
json.validate[ArticleForCreation].fold( | |
invalid => { | |
BadRequest(Json.toJson(Map("error" -> invalid.head.toString))) | |
}, |
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
implicit object ArticlePublishedReads extends Reads[ArticlePublishedState] { | |
def reads(json: JsValue) = json match { | |
case JsString(cat) => { | |
val res = articlePublishedMappings collectFirst { case (s, a) if cat.equals(s) => JsSuccess(a) } | |
res.getOrElse(JsError(Seq(JsPath() -> Seq(ValidationError("no such category"))))) | |
} | |
case _ => JsError(Seq(JsPath() -> Seq(ValidationError("validate.error.expected.jsnumber")))) | |
} | |
} |
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
sealed trait ArticlePublishedState | |
case object UNPUBLISHED extends ArticlePublishedState | |
case object PUBLISHED extends ArticlePublishedState | |
case object DIRTY extends ArticlePublishedState | |
val articlePublishedMappings = Map(UNPUBLISHED.toString() -> UNPUBLISHED, |
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
def update(article: ArticleForUpdate): Try[Unit] = database.withSession { implicit db: Session => | |
Try((ArticleTable.filter(a => a.id === article.id).map(a => a.name ~ a.body ~ a.displayOrder)) | |
.update(article.name, article.body, article.displayOrder)) | |
} |
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
val ArticleTable = new Table[Article]("Article") { | |
def id = column[Long]("id", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("name") | |
def body = column[Option[String]]("body") | |
def publishedBody = column[Option[String]]("publishedBody") | |
def userID = column[Long]("user_Id") | |
def created = column[Date]("created") | |
def category = column[String]("category") | |
def published = column[Boolean]("published") | |
def displayOrder = column[Int]("displayOrder") |
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
case class ArticleId(id: Long) | |
case class ArticleForDisplay(id: Option[Long] = None, | |
publishedBody: Option[String], | |
userID: Long, | |
created: Date, | |
category: String, | |
published: Boolean) | |
case class ArticleForCreation(name: String, body: Option[String], category: ArticleCategory, published: Boolean = false) |
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
Articles.update(valid) match { | |
case Success(a) => { | |
cache.Cache.remove("blog") | |
Ok(Json.toJson(ArticleId(valid.id))) | |
} | |
case Failure(e) => { | |
Logger.error("update error:", e) | |
BadRequest(Json.toJson(Map("error" -> e.getMessage()))) | |
} | |
} |
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
def create(userId: Long, articleForCreation: ArticleForCreation): Try[ArticleId] = database.withSession { implicit db: Session => | |
Try( | |
ArticleId( | |
ArticleTable.forInsert.insert( | |
Article(None, | |
articleForCreation.name, | |
articleForCreation.body, | |
userId, | |
new java.sql.Date(System.currentTimeMillis()), | |
articleForCreation.category.toString(), |
NewerOlder