Skip to content

Commit

Permalink
Merge pull request #167 from guardian/catalog_2
Browse files Browse the repository at this point in the history
added Catalog classes and some dummy catalog response
  • Loading branch information
pvighi committed Aug 3, 2018
2 parents dcbae23 + bae4a43 commit 7195262
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.gu.newproduct.api.productcatalog

import play.api.libs.json.{JsString, Json, Writes}

sealed trait DayOfWeek

case object Monday extends DayOfWeek

case object Tuesday extends DayOfWeek

case object Wednesday extends DayOfWeek

case object Thursday extends DayOfWeek

case object Friday extends DayOfWeek

case object Saturday extends DayOfWeek

case object Sunday extends DayOfWeek

case class ProductInfo(
id: String,
label: String,
startDateRules: Option[StartDateRules] = None
)

case class StartDateRules(
daysOfWeek: Option[List[DayOfWeek]] = None,
cutOffDayInclusive: Option[DayOfWeek] = None,
minDaysAfterCutOff: Option[Int] = None,
windowSizeDays: Option[Int] = None,
)


case class Group(label: String, products: List[ProductInfo])

case class Catalog(groups: List[Group])

object DayOfWeek {
implicit val writes: Writes[DayOfWeek] = { (day: DayOfWeek) => JsString(day.toString) }
}

object StartDateRules{
implicit val writes = Json.writes[StartDateRules]
}
object ProductInfo {
implicit val writes = Json.writes[ProductInfo]
}

object Group {
implicit val writes = Json.writes[Group]
}

object Catalog {
implicit val writes = Json.writes[Catalog]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,46 @@ object Handler extends Logging {
ApiGatewayHandler(LambdaIO(inputStream, outputStream, context)) {
ContinueProcessing(
Operation.noHealthcheck {
Req: ApiGatewayRequest => ApiGatewayResponse.successfulExecution
Req: ApiGatewayRequest => ApiGatewayResponse(body = catalog, statusCode = "200")
}
)
}
}

val catalog = {
val voucherEverydayRules = StartDateRules(
cutOffDayInclusive = Some(Tuesday),
daysOfWeek = Some(List(Monday)),
minDaysAfterCutOff = Some(20),
windowSizeDays = Some(28)
)

val voucherEveryday = ProductInfo(
id = "voucher_everyday",
label = "Every day",
startDateRules = Some(voucherEverydayRules)
)

val weekendsRule = voucherEverydayRules.copy(
daysOfWeek = Some(List(Saturday, Sunday))
)
val voucherWeekend = ProductInfo(
id = "voucher_weekend",
label = "Weekend",
startDateRules = Some(weekendsRule)
)

val contributionRules = StartDateRules(
windowSizeDays = Some(1)
)

val monthlyContribution = ProductInfo(
id = "monthly_contribution",
label = "Monthly",
startDateRules = Some(contributionRules)
)
val voucherGroup = Group("Voucher", List(voucherWeekend, voucherEveryday))
val contributionGroup = Group("Contribution", List(monthlyContribution))
Catalog(List(voucherGroup, contributionGroup))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.gu.newproduct.api.productcatalog

import org.scalatest.{FlatSpec, Matchers}
import play.api.libs.json.Json

class CatalogTest extends FlatSpec with Matchers {
it should "serialise catalog" in {

val everyDayRules = StartDateRules(
cutOffDayInclusive = Some(Tuesday),
daysOfWeek = Some(List(Monday)),
minDaysAfterCutOff = Some(20),
windowSizeDays = Some(28)
)

val voucherEveryday = ProductInfo(
id = "voucher_everyday",
label = "Every day",
startDateRules = Some(everyDayRules)
)

val weekendsRule = everyDayRules.copy(
daysOfWeek = Some(List(Saturday, Sunday))
)
val voucherWeekend = ProductInfo(
id = "voucher_weekend",
label = "Weekend",
startDateRules = Some(weekendsRule)
)

val monthlyContribution = ProductInfo(
id = "monthly_contribution",
label = "Monthly"
)

val voucherGroup = Group("Voucher", List(voucherWeekend, voucherEveryday))
val contributionGroup = Group("Contribution", List(monthlyContribution))
val catalog = Catalog(List(voucherGroup, contributionGroup))
val expected =
"""
|{
|"groups": [{
| "label": "Voucher",
| "products": [{
| "id": "voucher_weekend",
| "label": "Weekend",
| "startDateRules" : {
| "cutOffDayInclusive": "Tuesday",
| "minDaysAfterCutOff" : 20,
| "windowSizeDays" : 28,
| "daysOfWeek": ["Saturday", "Sunday"]
| }
| },
| {
| "id": "voucher_everyday",
| "label": "Every day",
| "startDateRules" : {
| "cutOffDayInclusive": "Tuesday",
| "minDaysAfterCutOff" : 20,
| "windowSizeDays" : 28,
| "daysOfWeek": ["Monday"]
| }
| }
| ]
|}, {
| "label": "Contribution",
| "products": [{
| "id": "monthly_contribution",
| "label": "Monthly"
| }]
|}]
|}
""".stripMargin
Json.toJson(catalog) shouldBe Json.parse(expected)

}
}

0 comments on commit 7195262

Please sign in to comment.