1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package controllers
import org.omg.CosNaming._BindingIteratorImplBase
import play.api._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc._
import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._
import play.api.Play.current
import scala.concurrent.Future
import models._
case class SaleData(endDate: java.sql.Date,
name: String,
shortDescription: String,
longDescription: String,
initialPrice: BigDecimal)
case class BidData(offer: BigDecimal)
object Sale extends Controller {
def sales = Auth { implicit request =>
DB.withSession { implicit session =>
val currentDateTime = new java.sql.Timestamp((new java.util.Date).getTime)
val sales = Views.Sales.filter(_.endDate > currentDateTime).sortBy(_.endDate.asc).run
Ok(views.html.pages.sales.currentSales(sales))
}
}
val saleForm = Form(
mapping(
"endDate" -> sqlDate,
"name" -> nonEmptyText(minLength = 5, maxLength = 20),
"shortDescription" -> nonEmptyText(minLength = 10, maxLength = 30),
"longDescription" -> nonEmptyText,
"initialPrice" -> bigDecimal(precision = 8, scale = 2)
)(SaleData.apply)(SaleData.unapply)
)
def sell = Auth { implicit request =>
if (request.account.isEmpty) {
Redirect(routes.Authentication.login())
.flashing("error" -> "Authentication required")
} else {
Ok(views.html.pages.sales.sellForm(saleForm))
}
}
def sellSubmit = Auth { implicit request =>
if (request.account.isEmpty) {
Redirect(routes.Authentication.login())
.flashing("error" -> "Authentication required")
} else {
DB.withSession { implicit session =>
saleForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(views.html.pages.sales.sellForm(formWithErrors))
},
validForm => {
val items = Tables.Items returning Tables.Items.map(_.uuid)
val uuid = items += Tables.Item(
userUuid = request.account.get.userUuid.get,
startDate = new java.sql.Timestamp(new java.util.Date().getTime),
endDate = new java.sql.Timestamp(validForm.endDate.getTime),
itemName = validForm.name,
shortDesc = validForm.shortDescription,
longDesc = validForm.longDescription,
initialPrice = validForm.initialPrice
)
Redirect(routes.Application.index())
.flashing("success" -> "Your item is now on sale.")
}
)
}
}
}
val bidForm = Form(
mapping(
"offer" -> bigDecimal(precision = 8, scale = 2)
)(BidData.apply)(BidData.unapply)
)
def item(itemUuid: String) = Auth { implicit request =>
DB.withSession { implicit session =>
val item = Some(Tables.Items.filter(_.uuid === itemUuid).first)
if (item.isEmpty) {
Redirect(routes.Sale.sales())
.flashing("error" -> "Item not found")
} else {
val seller = Tables.Users.filter(_.uuid === item.get.userUuid).first
val bids = Tables.Bids.filter(_.itemUuid === item.get.uuid).sortBy(_.bidDate.desc)
val bidders = Tables.Users
val bidsWithBidders = (bids join bidders on (_.userUuid === _.uuid)).run
Ok(views.html.pages.sales.itemPage(item.get, seller, bidsWithBidders, bidForm))
}
}
}
def bidSubmit(itemUuid: String) = Auth { implicit request =>
if (request.account.isEmpty) {
Redirect(routes.Authentication.login())
.flashing("error" -> "Authentication required")
} else {
DB.withSession { implicit session =>
val item = Some(Tables.Items.filter(_.uuid === itemUuid).first)
val currentTimestamp = new java.sql.Timestamp((new java.util.Date).getTime)
if (item.isEmpty) {
Redirect(routes.Sale.sales())
.flashing("error" -> "Item not found")
} else if (item.get.endDate before currentTimestamp) {
Redirect(routes.Sale.item(itemUuid))
.flashing("error" -> "This sale has already ended")
} else {
bidForm.bindFromRequest.fold(
formsWithErrors => {
Redirect(routes.Sale.item(itemUuid))
.flashing("error" -> "Invalid bid")
},
validForm => {
val bids = Tables.Bids.filter(_.itemUuid === itemUuid).sortBy(_.bidDate.desc).run
val offersEnough = validForm.offer > item.get.initialPrice && (bids.isEmpty || validForm.offer > bids.head.offer)
val userEquity = Views.Accounts.filter(_.userUuid === request.account.get.userUuid).map(_.equity).first.get
val enoughFunds = validForm.offer < userEquity
if (!enoughFunds) {
Redirect(routes.Sale.item(itemUuid))
.flashing("error" -> "You are too poor!")
} else if (!offersEnough) {
Redirect(routes.Sale.item(itemUuid))
.flashing("error" -> "Please offer more.")
} else {
Tables.Bids += new Tables.Bid(
itemUuid = itemUuid,
userUuid = request.account.get.userUuid.get,
bidDate = currentTimestamp,
offer = validForm.offer
)
Redirect(routes.Sale.item(itemUuid))
.flashing("success" -> "Thanks!")
}
}
)
}
}
}
}
}
|