-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from guardian/catalog_2
added Catalog classes and some dummy catalog response
- Loading branch information
Showing
3 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Catalog.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ers/new-product-api/src/test/scala/com/gu/newproduct/api/productcatalog/CatalogTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} |