From 49228524659add95970807f640dacd95d3e549fa Mon Sep 17 00:00:00 2001 From: Steve Chaloner Date: Mon, 29 Apr 2024 16:59:08 +0200 Subject: [PATCH 1/2] Update category - API support - Documented example --- README.md | 12 +++ .../seatsio/charts/CategoryUpdateParams.java | 28 +++++++ src/main/java/seatsio/charts/Charts.java | 8 +- .../seatsio/charts/ManageCategoriesTest.java | 79 ++++++++++++++++++- 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seatsio/charts/CategoryUpdateParams.java diff --git a/README.md b/README.md index f41d238..e231ae2 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,18 @@ categories.forEach(category -> { }); ``` +### Updating a category +```java +import seatsio.charts.CategoryKey; +import seatsio.charts.CategoryUpdateParams; +import seatsio.SeatsioClient; + +SeatsioClient client = new SeatsioClient(Region.EU, ""); + +client.charts.updateCategory("the chart key", CategoryKey.of("the category key"), + new CategoryUpdateParams("New label", "#cccccc", false)); +``` + ## Error handling When an API call results in a 4xx or 5xx error (e.g. when a chart could not be found), a SeatsioException is thrown. diff --git a/src/main/java/seatsio/charts/CategoryUpdateParams.java b/src/main/java/seatsio/charts/CategoryUpdateParams.java new file mode 100644 index 0000000..afd201d --- /dev/null +++ b/src/main/java/seatsio/charts/CategoryUpdateParams.java @@ -0,0 +1,28 @@ +package seatsio.charts; + +import com.google.gson.JsonObject; + +import static seatsio.json.JsonObjectBuilder.aJsonObject; + +public class CategoryUpdateParams { + + private final String label; + private final String color; + private final Boolean accessible; + + public CategoryUpdateParams(String label, + String color, + Boolean accessible) { + this.label = label; + this.color = color; + this.accessible = accessible; + } + + public JsonObject toJson() { + return aJsonObject() + .withPropertyIfNotNull("label", label) + .withPropertyIfNotNull("color", color) + .withPropertyIfNotNull("accessible", accessible) + .build(); + } +} diff --git a/src/main/java/seatsio/charts/Charts.java b/src/main/java/seatsio/charts/Charts.java index 167c80d..d6b9a9e 100644 --- a/src/main/java/seatsio/charts/Charts.java +++ b/src/main/java/seatsio/charts/Charts.java @@ -94,7 +94,6 @@ public void update(String key, String name, List categories) { )); } - public void addCategory(String chartKey, Category category) { unirest.stringResponse(UnirestWrapper.post(baseUrl + "/charts/{key}/categories") .routeParam("key", chartKey) @@ -107,6 +106,13 @@ public void removeCategory(String chartKey, CategoryKey categoryKey) { .routeParam("categoryKey", categoryKey.toString())); } + public void updateCategory(String chartKey, CategoryKey categorykey, CategoryUpdateParams updateParams) { + unirest.stringResponse(UnirestWrapper.post(baseUrl + "/charts/{chartKey}/categories/{categoryKey}") + .routeParam("chartKey", chartKey) + .routeParam("categoryKey", categorykey.toString()) + .body(updateParams.toJson())); + } + public List listCategories(String chartKey) { final String response = unirest.stringResponse(get(baseUrl + "/charts/{key}/categories") .routeParam("key", chartKey)); diff --git a/src/test/java/seatsio/charts/ManageCategoriesTest.java b/src/test/java/seatsio/charts/ManageCategoriesTest.java index 03e6ef4..3524fd6 100644 --- a/src/test/java/seatsio/charts/ManageCategoriesTest.java +++ b/src/test/java/seatsio/charts/ManageCategoriesTest.java @@ -72,9 +72,86 @@ public void listCategories_unknownChart() { assertThrows(SeatsioException.class, () -> client.charts.listCategories("unknownChart")); } + @Test + public void updateCategory() { + List categories = newArrayList( + new Category(CategoryKey.of(1L), "Category 1", "#aaaaaa"), + new Category(CategoryKey.of("cat2"), "Category 2", "#bbbbbb", true) + ); + Chart chart = client.charts.create("aChart", "BOOTHS", categories); + + client.charts.updateCategory(chart.key, CategoryKey.of("cat2"), + new CategoryUpdateParams("New label", "#cccccc", false)); + + Chart retrievedChart = client.charts.retrieve(chart.key); + Map drawing = client.charts.retrievePublishedVersion(retrievedChart.key); + assertThat(categories(drawing)).containsExactly( + ImmutableMap.of( + "key", 1.0, + "label", "Category 1", + "color", "#aaaaaa", + "accessible", false + ), + ImmutableMap.of( + "key", "cat2", + "label", "New label", + "color", "#cccccc", + "accessible", false + ) + ); + } + + @Test + public void updateCategory_doNotChangeAnything() { + List categories = newArrayList( + new Category(CategoryKey.of(1L), "Category 1", "#aaaaaa"), + new Category(CategoryKey.of("cat2"), "Category 2", "#bbbbbb", true) + ); + Chart chart = client.charts.create("aChart", "BOOTHS", categories); + + client.charts.updateCategory(chart.key, CategoryKey.of("cat2"), + new CategoryUpdateParams(null, null, null)); + + Chart retrievedChart = client.charts.retrieve(chart.key); + Map drawing = client.charts.retrievePublishedVersion(retrievedChart.key); + assertThat(categories(drawing)).containsExactly( + ImmutableMap.of( + "key", 1.0, + "label", "Category 1", + "color", "#aaaaaa", + "accessible", false + ), + ImmutableMap.of( + "key", "cat2", + "label", "Category 2", + "color", "#bbbbbb", + "accessible", true + ) + ); + } + + @Test + public void updateCategory_unknownChart() { + SeatsioException e = assertThrows(SeatsioException.class, () -> client.charts.updateCategory("unknownChart", CategoryKey.of("cat2"), + new CategoryUpdateParams("New label", "#cccccc", false))); + assertThat(e.getMessage()).isEqualTo("Chart not found: unknownChart"); + } + + @Test + public void updateCategory_unknownCategory() { + List categories = newArrayList( + new Category(CategoryKey.of(1L), "Category 1", "#aaaaaa"), + new Category(CategoryKey.of("cat2"), "Category 2", "#bbbbbb", true) + ); + Chart chart = client.charts.create("aChart", "BOOTHS", categories); + + SeatsioException e = assertThrows(SeatsioException.class, () -> client.charts.updateCategory(chart.key, CategoryKey.of(3L), + new CategoryUpdateParams("New label", "#cccccc", false))); + assertThat(e.getMessage()).isEqualTo("category not found"); + } + private List> categories(Map drawing) { Map categoriesMap = (Map) drawing.get("categories"); return (List>) categoriesMap.get("list"); } - } From 4bdae7f12b9c99b62f02c45e45ddcebc8485dace Mon Sep 17 00:00:00 2001 From: Steve Chaloner Date: Tue, 30 Apr 2024 09:25:38 +0200 Subject: [PATCH 2/2] - Added builder methods to `CategoryUpdateParams` --- .../seatsio/charts/CategoryUpdateParams.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/seatsio/charts/CategoryUpdateParams.java b/src/main/java/seatsio/charts/CategoryUpdateParams.java index afd201d..c4858b3 100644 --- a/src/main/java/seatsio/charts/CategoryUpdateParams.java +++ b/src/main/java/seatsio/charts/CategoryUpdateParams.java @@ -6,9 +6,12 @@ public class CategoryUpdateParams { - private final String label; - private final String color; - private final Boolean accessible; + private String label; + private String color; + private Boolean accessible; + + public CategoryUpdateParams() { + } public CategoryUpdateParams(String label, String color, @@ -18,6 +21,21 @@ public CategoryUpdateParams(String label, this.accessible = accessible; } + public CategoryUpdateParams withLabel(String label) { + this.label = label; + return this; + } + + public CategoryUpdateParams withColor(String color) { + this.color = color; + return this; + } + + public CategoryUpdateParams withAccessible(boolean accessible) { + this.accessible = accessible; + return this; + } + public JsonObject toJson() { return aJsonObject() .withPropertyIfNotNull("label", label)