aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/Sale.scala
blob: e6244c3dae97b4edd4b00d4b3aa15a145c4f3bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package controllers

import play.api._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc._

import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._
import play.api.Play.current

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 sales = Views.Sales.filter(_.endDate > currentDateTime).sortBy(_.endDate.asc).run

      Ok(views.html.pages.sales.currentSales(sales))
    }
  }

  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

  def bids(itemUuid: String) = TODO

  def bidSubmit(itemUuid: String) = TODO

}