aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
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 /app/controllers
parent1ec4626a01da4e21b7e654cb0b6df2f52f6c5d91 (diff)
downloadminibay-330d7a719800cded4385c633370f782571891f49.tar.gz
Implement sell action
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/Sale.scala59
1 files changed, 57 insertions, 2 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