Skip to content

Commit 490f9af

Browse files
jensmeichleresezenMudaafi
authored
feat: add sort option commands (#175)
* feat: add sort option commands * test: add sort option unit tests * docs: add sort option management to README.md * Update test * Newline at the end of file * Remove gson * Lint * update implementation * lint * lint --------- Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com> Co-authored-by: mudaafi <ahmad.mudaafi@constructor.io>
1 parent fe221c5 commit 490f9af

File tree

10 files changed

+1178
-9
lines changed

10 files changed

+1178
-9
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,95 @@ Task response = constructor.task(request);
401401
String response = constructor.taskAsJSON(request);
402402
```
403403

404+
# Managing Sort Options
405+
406+
Sort options allow you to define custom sorting strategies for search and browse results. You can create, update, delete, and retrieve sort options for your catalog.
407+
408+
## Creating a Sort Option
409+
410+
To create a new sort option, you will need to create a `SortOption` object with the required fields and wrap it in a `SortOptionRequest`.
411+
412+
```java
413+
// Create a SortOption with the required fields
414+
SortOption sortOption = new SortOption("price", SortOption.SortOrder.ascending);
415+
sortOption.setDisplayName("Price");
416+
sortOption.setPathInMetadata("price_min");
417+
sortOption.setHidden(false);
418+
419+
// Create a SortOptionRequest with the sort option and section
420+
SortOptionRequest request = new SortOptionRequest(sortOption, "Products");
421+
422+
// Create the sort option
423+
String response = constructor.createSortOption(request);
424+
```
425+
426+
## Updating a Sort Option
427+
428+
To update an existing sort option (or create it if it doesn't exist), use the `updateSortOption` method. The sort option is identified by the combination of `sortBy` and `sortOrder`.
429+
430+
```java
431+
// Create a SortOption with updated fields
432+
SortOption sortOption = new SortOption("price", SortOption.SortOrder.ascending);
433+
sortOption.setDisplayName("Price (Low to High)");
434+
sortOption.setPathInMetadata("price_min");
435+
sortOption.setHidden(false);
436+
sortOption.setPosition(1);
437+
438+
// Create a SortOptionRequest
439+
SortOptionRequest request = new SortOptionRequest(sortOption, "Products");
440+
441+
// Update the sort option
442+
String response = constructor.updateSortOption(request);
443+
```
444+
445+
## Deleting Sort Options
446+
447+
To delete a sort option, you need to specify the `sortBy` and `sortOrder` fields that identify it.
448+
449+
```java
450+
// Delete a single sort option by sortBy and sortOrder
451+
String response = constructor.deleteSortOption("price", SortOption.SortOrder.ascending, "Products");
452+
453+
// Or use the default section "Products"
454+
String response = constructor.deleteSortOption("price", SortOption.SortOrder.ascending);
455+
456+
// To delete multiple sort options at once
457+
SortOption[] sortOptions = new SortOption[] {
458+
new SortOption("price", SortOption.SortOrder.ascending),
459+
new SortOption("relevance", SortOption.SortOrder.descending)
460+
};
461+
String response = constructor.deleteSortOptions(sortOptions, "Products");
462+
```
463+
464+
## Retrieving Sort Options
465+
466+
To retrieve all sort options for a section, you can use the `retrieveSortOptions` method. You can optionally filter by a specific `sortBy` field and control pagination.
467+
468+
```java
469+
// Retrieve all sort options for the default "Products" section
470+
SortOptionsResponse response = constructor.retrieveSortOptions();
471+
472+
// Retrieve sort options filtered by sortBy field
473+
SortOptionsResponse response = constructor.retrieveSortOptions("price");
474+
475+
// Advanced retrieval with pagination and filters
476+
SortOptionGetRequest request = new SortOptionGetRequest();
477+
request.setSection("Products");
478+
request.setSortBy("price");
479+
request.setPage(1);
480+
request.setResultsPerPage(20);
481+
482+
SortOptionsResponse response = constructor.retrieveSortOptions(request);
483+
484+
// Access the results
485+
int totalCount = response.getTotalCount();
486+
List<SortOption> sortOptions = response.getSortOptions();
487+
488+
for (SortOption option : sortOptions) {
489+
System.out.println(option.getDisplayName() + " - " + option.getSortBy() + " " + option.getSortOrder());
490+
}
491+
```
492+
404493
# Testing
405494

406495
Download the repository and run the following commands from `./constructorio-client`

0 commit comments

Comments
 (0)