aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-02-09 00:14:18 +0100
committerPacien TRAN-GIRARD2015-02-09 00:14:18 +0100
commitd79239a89c795035ea8d8a971aa9953d9e9a8e48 (patch)
tree0f910aeaafa3781cd436c0af130ef93379afda0a /app/controllers
parent330d7a719800cded4385c633370f782571891f49 (diff)
downloadminibay-d79239a89c795035ea8d8a971aa9953d9e9a8e48.tar.gz
Implement item pages and bids
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/Sale.scala81
1 files changed, 78 insertions, 3 deletions
diff --git a/app/controllers/Sale.scala b/app/controllers/Sale.scala
index e6244c3..d6dab2d 100644
--- a/app/controllers/Sale.scala
+++ b/app/controllers/Sale.scala
@@ -1,5 +1,6 @@
1package controllers 1package controllers
2 2
3import org.omg.CosNaming._BindingIteratorImplBase
3import play.api._ 4import play.api._
4import play.api.data._ 5import play.api.data._
5import play.api.data.Forms._ 6import play.api.data.Forms._
@@ -20,6 +21,8 @@ case class SaleData(endDate: java.sql.Date,
20 longDescription: String, 21 longDescription: String,
21 initialPrice: BigDecimal) 22 initialPrice: BigDecimal)
22 23
24case class BidData(offer: BigDecimal)
25
23object Sale extends Controller { 26object Sale extends Controller {
24 27
25 def sales = Auth { implicit request => 28 def sales = Auth { implicit request =>
@@ -82,11 +85,83 @@ object Sale extends Controller {
82 } 85 }
83 } 86 }
84 87
88 val bidForm = Form(
89 mapping(
90 "offer" -> bigDecimal(precision = 8, scale = 2)
91 )(BidData.apply)(BidData.unapply)
92 )
85 93
86 def item(itemUuid: String) = TODO 94 def item(itemUuid: String) = Auth { implicit request =>
95 DB.withSession { implicit session =>
96 val item = Some(Tables.Items.filter(_.uuid === itemUuid).first)
97
98 if (item.isEmpty) {
99 Redirect(routes.Sale.sales())
100 .flashing("error" -> "Item not found")
101 } else {
102 val seller = Tables.Users.filter(_.uuid === item.get.userUuid).first
103 val bids = Tables.Bids.filter(_.itemUuid === item.get.uuid).sortBy(_.bidDate.desc)
104 val bidders = Tables.Users
105 val bidsWithBidders = (bids join bidders on (_.userUuid === _.uuid)).run
106 Ok(views.html.pages.sales.itemPage(item.get, seller, bidsWithBidders, bidForm))
107 }
108 }
109 }
87 110
88 def bids(itemUuid: String) = TODO 111 def bidSubmit(itemUuid: String) = Auth { implicit request =>
112 if (request.account.isEmpty) {
113 Redirect(routes.Authentication.login())
114 .flashing("error" -> "Authentication required")
115 } else {
116 DB.withSession { implicit session =>
117 val item = Some(Tables.Items.filter(_.uuid === itemUuid).first)
118 val currentTimestamp = new java.sql.Timestamp((new java.util.Date).getTime)
119
120 if (item.isEmpty) {
121 Redirect(routes.Sale.sales())
122 .flashing("error" -> "Item not found")
123 } else if (item.get.endDate before currentTimestamp) {
124 Redirect(routes.Sale.item(itemUuid))
125 .flashing("error" -> "This sale has already ended")
126 } else {
127
128 bidForm.bindFromRequest.fold(
129 formsWithErrors => {
130 Redirect(routes.Sale.item(itemUuid))
131 .flashing("error" -> "Invalid bid")
132 },
133 validForm => {
134
135 val bids = Tables.Bids.filter(_.itemUuid === itemUuid).sortBy(_.bidDate.desc).run
136 val offersEnough = validForm.offer > item.get.initialPrice && (bids.isEmpty || validForm.offer > bids.head.offer)
137
138 val userEquity = Views.Accounts.filter(_.userUuid === request.account.get.userUuid).map(_.equity).first.get
139 val enoughFunds = validForm.offer < userEquity
140
141 if (!enoughFunds) {
142 Redirect(routes.Sale.item(itemUuid))
143 .flashing("error" -> "You are too poor!")
144 } else if (!offersEnough) {
145 Redirect(routes.Sale.item(itemUuid))
146 .flashing("error" -> "Please offer more.")
147 } else {
148 Tables.Bids += new Tables.Bid(
149 itemUuid = itemUuid,
150 userUuid = request.account.get.userUuid.get,
151 bidDate = currentTimestamp,
152 offer = validForm.offer
153 )
154
155 Redirect(routes.Sale.item(itemUuid))
156 .flashing("success" -> "Thanks!")
157 }
158 }
159 )
160
161 }
89 162
90 def bidSubmit(itemUuid: String) = TODO 163 }
164 }
165 }
91 166
92} 167}