From 330d7a719800cded4385c633370f782571891f49 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 8 Feb 2015 22:31:17 +0100 Subject: Implement sell action --- app/controllers/Sale.scala | 59 +++++++++++++++++++++- app/views/fragments/ebeMainActions.scala.html | 2 +- .../fragments/forms/labeledTextarea.scala.html | 4 ++ app/views/pages/sales/currentSales.scala.html | 4 +- app/views/pages/sales/sellForm.scala.html | 34 +++++++++++++ conf/routes | 8 +-- 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 app/views/fragments/forms/labeledTextarea.scala.html create mode 100644 app/views/pages/sales/sellForm.scala.html 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 import models._ +case class SaleData(endDate: java.sql.Date, + name: String, + shortDescription: String, + longDescription: String, + initialPrice: 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 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)) } } - def sell(itemUuid: String) = TODO + 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.") + + } + ) + } + } + } 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 @@
- + Sell 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 @@ +@(field: Field, inputType: String, label: String) + +@views.html.fragments.forms.inputLabel(field, label) +@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 @@ @for(sale <- sales) { - @sale.endDate + @sale.endDate.get @sale.itemName - @if(sale.bestOffer.isDefined) {@sale.bestOffer} else {sale.initialPrice} € + @if(sale.bestOffer.isDefined) {@sale.bestOffer} else {@sale.initialPrice} € } 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 @@ +@(saleForm: Form[SaleData])(implicit request: AuthRequest[AnyContent], flash: Flash, token: play.filters.csrf.CSRF.Token) + +@templates.ebe("Sell")(request.account) { + +
+
+ +

Sell a new item

+ + @views.html.fragments.forms.globalErrors(saleForm) + + @helper.form(action = routes.Sale.sellSubmit(), 'class -> "pure-form pure-form-stacked") { + + @helper.CSRF.formField + +
+ @views.html.fragments.forms.labeledField(saleForm("name"), "text", "Item name") + @views.html.fragments.forms.labeledField(saleForm("shortDescription"), "text", "Short description") + @views.html.fragments.forms.labeledTextarea(saleForm("longDescription"), "text", "Long description") +
+ +
+ @views.html.fragments.forms.labeledField(saleForm("endDate"), "date", "End date") + @views.html.fragments.forms.labeledField(saleForm("initialPrice"), "number", "Initial price") +
+ + + + } + +
+
+ +} 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) GET /item/:uuid/bids controllers.Sale.bids(uuid) POST /item/:uuid/bids controllers.Sale.bids(uuid) -GET /sell controllers.Sale.sell(itemUuid = null) -GET /sell/:uuid controllers.Sale.sell(uuid) -POST /sell controllers.Sale.sell(itemUuid = null) -POST /sell/:uuid controllers.Sale.sell(uuid) +GET /sell controllers.Sale.sell +#GET /sell/:uuid controllers.Sale.sell(uuid) +POST /sell controllers.Sale.sellSubmit +#POST /sell/:uuid controllers.Sale.sell # Cheat console POST /console controllers.Console.console -- cgit v1.2.3