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 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'app/controllers') 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 -- cgit v1.2.3