Skip to content

Commit

Permalink
Update category (#176)
Browse files Browse the repository at this point in the history
* Update category

- API support
- Documented example

* - Added builder methods to `CategoryUpdateParams`

---------

Co-authored-by: Steve Chaloner <[email protected]>
  • Loading branch information
schaloner and Steve Chaloner authored Apr 30, 2024
1 parent 9569761 commit 01a37f0
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<WORKSPACE SECRET KEY>");

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.
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/seatsio/charts/CategoryUpdateParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seatsio.charts;

import com.google.gson.JsonObject;

import static seatsio.json.JsonObjectBuilder.aJsonObject;

public class CategoryUpdateParams {

private String label;
private String color;
private Boolean accessible;

public CategoryUpdateParams() {
}

public CategoryUpdateParams(String label,
String color,
Boolean accessible) {
this.label = label;
this.color = color;
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)
.withPropertyIfNotNull("color", color)
.withPropertyIfNotNull("accessible", accessible)
.build();
}
}
8 changes: 7 additions & 1 deletion src/main/java/seatsio/charts/Charts.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public void update(String key, String name, List<Category> categories) {
));
}


public void addCategory(String chartKey, Category category) {
unirest.stringResponse(UnirestWrapper.post(baseUrl + "/charts/{key}/categories")
.routeParam("key", chartKey)
Expand All @@ -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<Category> listCategories(String chartKey) {
final String response = unirest.stringResponse(get(baseUrl + "/charts/{key}/categories")
.routeParam("key", chartKey));
Expand Down
79 changes: 78 additions & 1 deletion src/test/java/seatsio/charts/ManageCategoriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,86 @@ public void listCategories_unknownChart() {
assertThrows(SeatsioException.class, () -> client.charts.listCategories("unknownChart"));
}

@Test
public void updateCategory() {
List<Category> 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<Category> 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<Category> 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<Map<?, ?>> categories(Map<?, ?> drawing) {
Map<?, ?> categoriesMap = (Map<?, ?>) drawing.get("categories");
return (List<Map<?, ?>>) categoriesMap.get("list");
}

}

0 comments on commit 01a37f0

Please sign in to comment.