Auctions schema and creation
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package controllers
|
||||
|
||||
import controllers.authentication.AuthenticatedAction
|
||||
import dao.{AuctionsDAO, AuthTokensDAO}
|
||||
import models.Auction
|
||||
import play.api.libs.functional.syntax.toFunctionalBuilderOps
|
||||
import play.api.libs.json.{JsError, JsPath, JsValue, Json, Reads, Writes}
|
||||
import play.api.mvc.{BaseController, ControllerComponents}
|
||||
|
||||
import java.security.SecureRandom
|
||||
import javax.inject.{Inject, Singleton}
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
@Singleton
|
||||
class AuctionsController @Inject()(val controllerComponents: ControllerComponents, val auctionsDAO: AuctionsDAO, val authTokensDAO: AuthTokensDAO, implicit val ec: ExecutionContext) extends BaseController {
|
||||
def create = Action.andThen(AuthenticatedAction(authTokensDAO)).async(parse.json) { implicit request =>
|
||||
val auctionCreateDataResult = request.body.validate[AuctionCreateData]
|
||||
auctionCreateDataResult.fold(
|
||||
errors => Future.successful(BadRequest(JsError.toJson(errors))),
|
||||
auctionCreateData => {
|
||||
for {
|
||||
auction <- auctionsDAO.create(auctionCreateData.name, auctionCreateData.startingPrice, request.user)
|
||||
json = Json.toJson(auction)
|
||||
response = Created(json)
|
||||
} yield response
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
case class AuctionCreateData(name: String, startingPrice: BigDecimal)
|
||||
private implicit val auctionCreateDataReads: Reads[AuctionCreateData] = (
|
||||
(JsPath \ "name").read[String] and (JsPath \ "starting_price").read[BigDecimal]
|
||||
)(AuctionCreateData.apply)
|
||||
private implicit val auctionWrites: Writes[Auction] = new Writes[Auction] {
|
||||
override def writes(a: Auction): JsValue = Json.obj(
|
||||
"id" -> a.id,
|
||||
"name" -> a.name,
|
||||
"gid" -> a.gid,
|
||||
"starting_price" -> a.startingPrice,
|
||||
"current_bid" -> a.currentBid,
|
||||
"current_bidder_id" -> a.currentBidderId,
|
||||
"current_bid_at" -> a.currentBidAt
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user