aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-02-08 22:31:17 +0100
committerPacien TRAN-GIRARD2015-02-08 22:31:17 +0100
commit330d7a719800cded4385c633370f782571891f49 (patch)
treec231da0bec9cf7c70adf6ccd1e9dc6b58d1b0e2d
parent1ec4626a01da4e21b7e654cb0b6df2f52f6c5d91 (diff)
downloadminibay-330d7a719800cded4385c633370f782571891f49.tar.gz
Implement sell action
-rw-r--r--app/controllers/Sale.scala59
-rw-r--r--app/views/fragments/ebeMainActions.scala.html2
-rw-r--r--app/views/fragments/forms/labeledTextarea.scala.html4
-rw-r--r--app/views/pages/sales/currentSales.scala.html4
-rw-r--r--app/views/pages/sales/sellForm.scala.html34
-rw-r--r--conf/routes8
6 files changed, 102 insertions, 9 deletions
diff --git a/app/controllers/Sale.scala b/app/controllers/Sale.scala
index b5e4622..e6244c3 100644
--- a/app/controllers/Sale.scala
+++ b/app/controllers/Sale.scala
@@ -14,18 +14,73 @@ import scala.concurrent.Future
14import models._ 14import models._
15 15
16 16
17case class SaleData(endDate: java.sql.Date,
18 name: String,
19 shortDescription: String,
20 longDescription: String,
21 initialPrice: BigDecimal)
22
17object Sale extends Controller { 23object Sale extends Controller {
18 24
19 def sales = Auth { implicit request => 25 def sales = Auth { implicit request =>
20 DB.withSession { implicit session => 26 DB.withSession { implicit session =>
21 val currentDateTime = new java.sql.Timestamp((new java.util.Date).getTime) 27 val currentDateTime = new java.sql.Timestamp((new java.util.Date).getTime)
22 val sales = Views.Sales.filter(_.endDate > currentDateTime).sortBy(_.endDate.asc).run 28 val sales = Views.Sales.filter(_.endDate > currentDateTime).sortBy(_.endDate.asc).run
23 29
24 Ok(views.html.pages.sales.currentSales(sales)) 30 Ok(views.html.pages.sales.currentSales(sales))
25 } 31 }
26 } 32 }
27 33
28 def sell(itemUuid: String) = TODO 34 val saleForm = Form(
35 mapping(
36 "endDate" -> sqlDate,
37 "name" -> nonEmptyText(minLength = 5, maxLength = 20),
38 "shortDescription" -> nonEmptyText(minLength = 10, maxLength = 30),
39 "longDescription" -> nonEmptyText,
40 "initialPrice" -> bigDecimal(precision = 8, scale = 2)
41 )(SaleData.apply)(SaleData.unapply)
42 )
43
44 def sell = Auth { implicit request =>
45 if (request.account.isEmpty) {
46 Redirect(routes.Authentication.login())
47 .flashing("error" -> "Authentication required")
48 } else {
49 Ok(views.html.pages.sales.sellForm(saleForm))
50 }
51 }
52
53 def sellSubmit = Auth { implicit request =>
54 if (request.account.isEmpty) {
55 Redirect(routes.Authentication.login())
56 .flashing("error" -> "Authentication required")
57 } else {
58 DB.withSession { implicit session =>
59 saleForm.bindFromRequest.fold(
60 formWithErrors => {
61 BadRequest(views.html.pages.sales.sellForm(formWithErrors))
62 },
63 validForm => {
64
65 val items = Tables.Items returning Tables.Items.map(_.uuid)
66 val uuid = items += Tables.Item(
67 userUuid = request.account.get.userUuid.get,
68 startDate = new java.sql.Timestamp(new java.util.Date().getTime),
69 endDate = new java.sql.Timestamp(validForm.endDate.getTime),
70 itemName = validForm.name,
71 shortDesc = validForm.shortDescription,
72 longDesc = validForm.longDescription,
73 initialPrice = validForm.initialPrice
74 )
75
76 Redirect(routes.Application.index())
77 .flashing("success" -> "Your item is now on sale.")
78
79 }
80 )
81 }
82 }
83 }
29 84
30 85
31 def item(itemUuid: String) = TODO 86 def item(itemUuid: String) = TODO
diff --git a/app/views/fragments/ebeMainActions.scala.html b/app/views/fragments/ebeMainActions.scala.html
index 9976d17..36c3192 100644
--- a/app/views/fragments/ebeMainActions.scala.html
+++ b/app/views/fragments/ebeMainActions.scala.html
@@ -1,5 +1,5 @@
1<div class="pure-u-1 pure-u-lg-1-2"> 1<div class="pure-u-1 pure-u-lg-1-2">
2 <a class="pure-button blue-bg" href="@routes.Sale.sell(null)"> 2 <a class="pure-button blue-bg" href="@routes.Sale.sell()">
3 <i class="fa fa-gavel fa-5x"></i> 3 <i class="fa fa-gavel fa-5x"></i>
4 <span>Sell</span> 4 <span>Sell</span>
5 </a> 5 </a>
diff --git a/app/views/fragments/forms/labeledTextarea.scala.html b/app/views/fragments/forms/labeledTextarea.scala.html
new file mode 100644
index 0000000..a086278
--- /dev/null
+++ b/app/views/fragments/forms/labeledTextarea.scala.html
@@ -0,0 +1,4 @@
1@(field: Field, inputType: String, label: String)
2
3@views.html.fragments.forms.inputLabel(field, label)
4@views.html.fragments.forms.textarea(field, label)
diff --git a/app/views/pages/sales/currentSales.scala.html b/app/views/pages/sales/currentSales.scala.html
index 30cea78..bb1851c 100644
--- a/app/views/pages/sales/currentSales.scala.html
+++ b/app/views/pages/sales/currentSales.scala.html
@@ -26,9 +26,9 @@
26 <tbody> 26 <tbody>
27 @for(sale <- sales) { 27 @for(sale <- sales) {
28 <tr> 28 <tr>
29 <td>@sale.endDate</td> 29 <td>@sale.endDate.get</td>
30 <td><a href="@routes.Sale.item(sale.itemUuid.get)">@sale.itemName</a></td> 30 <td><a href="@routes.Sale.item(sale.itemUuid.get)">@sale.itemName</a></td>
31 <td>@if(sale.bestOffer.isDefined) {@sale.bestOffer} else {sale.initialPrice} €</td> 31 <td>@if(sale.bestOffer.isDefined) {@sale.bestOffer} else {@sale.initialPrice} €</td>
32 </tr> 32 </tr>
33 } 33 }
34 </tbody> 34 </tbody>
diff --git a/app/views/pages/sales/sellForm.scala.html b/app/views/pages/sales/sellForm.scala.html
new file mode 100644
index 0000000..d3988ba
--- /dev/null
+++ b/app/views/pages/sales/sellForm.scala.html
@@ -0,0 +1,34 @@
1@(saleForm: Form[SaleData])(implicit request: AuthRequest[AnyContent], flash: Flash, token: play.filters.csrf.CSRF.Token)
2
3@templates.ebe("Sell")(request.account) {
4
5 <div class="pure-g">
6 <div class="pure-u-1 pure-u-lg-1-2 centered">
7
8 <h2>Sell a new item</h2>
9
10 @views.html.fragments.forms.globalErrors(saleForm)
11
12 @helper.form(action = routes.Sale.sellSubmit(), 'class -> "pure-form pure-form-stacked") {
13
14 @helper.CSRF.formField
15
16 <fieldset>
17 @views.html.fragments.forms.labeledField(saleForm("name"), "text", "Item name")
18 @views.html.fragments.forms.labeledField(saleForm("shortDescription"), "text", "Short description")
19 @views.html.fragments.forms.labeledTextarea(saleForm("longDescription"), "text", "Long description")
20 </fieldset>
21
22 <fieldset>
23 @views.html.fragments.forms.labeledField(saleForm("endDate"), "date", "End date")
24 @views.html.fragments.forms.labeledField(saleForm("initialPrice"), "number", "Initial price")
25 </fieldset>
26
27 <button type="submit" class="pure-button pure-input-1 pure-button-primary">Save</button>
28
29 }
30
31 </div>
32 </div>
33
34}
diff --git a/conf/routes b/conf/routes
index c245163..903480d 100644
--- a/conf/routes
+++ b/conf/routes
@@ -42,10 +42,10 @@ GET /item/:uuid controllers.Sale.item(uuid)
42GET /item/:uuid/bids controllers.Sale.bids(uuid) 42GET /item/:uuid/bids controllers.Sale.bids(uuid)
43POST /item/:uuid/bids controllers.Sale.bids(uuid) 43POST /item/:uuid/bids controllers.Sale.bids(uuid)
44 44
45GET /sell controllers.Sale.sell(itemUuid = null) 45GET /sell controllers.Sale.sell
46GET /sell/:uuid controllers.Sale.sell(uuid) 46#GET /sell/:uuid controllers.Sale.sell(uuid)
47POST /sell controllers.Sale.sell(itemUuid = null) 47POST /sell controllers.Sale.sellSubmit
48POST /sell/:uuid controllers.Sale.sell(uuid) 48#POST /sell/:uuid controllers.Sale.sell
49 49
50# Cheat console 50# Cheat console
51POST /console controllers.Console.console 51POST /console controllers.Console.console