diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/Sale.scala | 59 |
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 | |||
14 | import models._ | 14 | import models._ |
15 | 15 | ||
16 | 16 | ||
17 | case class SaleData(endDate: java.sql.Date, | ||
18 | name: String, | ||
19 | shortDescription: String, | ||
20 | longDescription: String, | ||
21 | initialPrice: BigDecimal) | ||
22 | |||
17 | object Sale extends Controller { | 23 | object 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 |