Better Auction rendering for Auctions index
This commit is contained in:
@@ -3,13 +3,13 @@ package controllers
|
||||
import actors.auctions.AuctionPublisher
|
||||
import controllers.authentication.AuthenticatedAction
|
||||
import dao.{AuctionsDAO, AuthTokensDAO}
|
||||
import models.Auction
|
||||
import models.AuctionStatus.Ready
|
||||
import models.{Auction, User}
|
||||
import org.apache.pekko.actor.typed.ActorRef
|
||||
import play.api.libs.functional.syntax.toFunctionalBuilderOps
|
||||
import play.api.libs.json.{JsError, JsPath, JsValue, Json, Reads, Writes}
|
||||
import play.api.libs.json.*
|
||||
import play.api.mvc.{BaseController, ControllerComponents}
|
||||
|
||||
import java.security.SecureRandom
|
||||
import javax.inject.{Inject, Singleton}
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
@@ -21,6 +21,8 @@ class AuctionsController @Inject()(val controllerComponents: ControllerComponent
|
||||
val auctionPublisher: ActorRef[AuctionPublisher.Command],
|
||||
implicit val ec: ExecutionContext) extends BaseController {
|
||||
def index = authenticatedAction.async { implicit request =>
|
||||
given Writes[Auction] = auctionWrites(request.user)
|
||||
|
||||
for {
|
||||
auctions <- auctionsDAO.list_available(request.user)
|
||||
json = Json.toJson(auctions)
|
||||
@@ -35,15 +37,28 @@ class AuctionsController @Inject()(val controllerComponents: ControllerComponent
|
||||
for {
|
||||
auction <- auctionsDAO.create(auctionCreateData.name, auctionCreateData.startingPrice, request.user)
|
||||
json = Json.toJson(auction)
|
||||
response = Created(json)
|
||||
_ = auctionPublisher ! AuctionPublisher.Publish(auction, request.user)
|
||||
} yield response
|
||||
} yield Created(json)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private 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 given Reads[AuctionCreateData] = Json.reads[AuctionCreateData]
|
||||
|
||||
private def auctionWrites(user: User): OWrites[Auction] = (o: Auction) => {
|
||||
Json.toJsObject(o)(using Auction.writes) ++ Json.obj(
|
||||
"actions" -> Json.obj(
|
||||
"bid" -> (
|
||||
(o.authorId != user.id.get) &&
|
||||
(o.currentBidderId match {
|
||||
case None => true // If there's no bidder, we can bid
|
||||
case Some(bidderId) => bidderId != user.id.get
|
||||
}) &&
|
||||
(o.status == Ready)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user