Auctions show
This commit is contained in:
@@ -1,31 +1,35 @@
|
||||
package dao
|
||||
|
||||
import models.{Auction, AuctionStatus}
|
||||
import schemas.Auctions
|
||||
import schemas.given
|
||||
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
|
||||
import slick.jdbc.PostgresProfile
|
||||
import slick.jdbc.PostgresProfile.api.*
|
||||
|
||||
import java.time.Instant
|
||||
import javax.inject.Inject
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.util.Random
|
||||
|
||||
import models.{Auction, AuctionStatus, User}
|
||||
import schemas.Auctions
|
||||
import schemas.given
|
||||
|
||||
class AuctionsDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit val ec: ExecutionContext)
|
||||
extends HasDatabaseConfigProvider[PostgresProfile] {
|
||||
|
||||
def list_available(user: models.User): Future[Seq[Auction]] = {
|
||||
def list_available(user: User): Future[Seq[Auction]] = {
|
||||
db.run(availableScope(user).result)
|
||||
}
|
||||
|
||||
def showAvailable(user: User, id: Long): Future[Option[Auction]] = {
|
||||
db.run {
|
||||
Auctions.filter(row => (row.status === AuctionStatus.Ready) && (row.authorId =!= user.id.get)).result
|
||||
availableScope(user).filter(_.id === id).take(1).result.headOption
|
||||
}
|
||||
}
|
||||
|
||||
def list_created_by(user: models.User): Future[Seq[Auction]] = {
|
||||
def list_created_by(user: User): Future[Seq[Auction]] = {
|
||||
db.run(Auctions.filter(_.authorId === user.id).result)
|
||||
}
|
||||
|
||||
def create(name: String, startingPrice: BigDecimal, author: models.User): Future[Auction] = {
|
||||
def create(name: String, startingPrice: BigDecimal, author: User): Future[Auction] = {
|
||||
val auction = Auction(
|
||||
id = None, name = name,
|
||||
gid = Random.alphanumeric.take(24).mkString,
|
||||
@@ -45,6 +49,10 @@ class AuctionsDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvid
|
||||
|
||||
def changeStatus(auctionId: Long, status: AuctionStatus): Future[Boolean] = {
|
||||
db.run(Auctions.filter(_.id === auctionId).map(_.status).update(status))
|
||||
.map(_ == 1)
|
||||
.map(_ == 1)
|
||||
}
|
||||
|
||||
private def availableScope(user: User) = {
|
||||
Auctions.filter(row => (row.status === AuctionStatus.Ready) && (row.authorId =!= user.id.get))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user