diff --git a/src/main/java/nouritsu/Dao.java b/src/main/java/nouritsu/Dao.java index 3b08470..e2bc6a6 100644 --- a/src/main/java/nouritsu/Dao.java +++ b/src/main/java/nouritsu/Dao.java @@ -44,6 +44,7 @@ public class Dao { return "Cleared list of user ā€œ%sā€œ".formatted(id); } + // The .exists() check is necessary because smembers throws an exception instead of returning null or an empty set. private Option> getSet(String key) { return withRedis(redis -> redis.exists(key) diff --git a/src/main/java/nouritsu/Nouritsu.java b/src/main/java/nouritsu/Nouritsu.java index 798083e..29027c0 100644 --- a/src/main/java/nouritsu/Nouritsu.java +++ b/src/main/java/nouritsu/Nouritsu.java @@ -98,20 +98,20 @@ public class Nouritsu extends NanoHTTPD { .fold(Function.identity(), Resp.OK); } + // Return a Tuple2 containing the values of two eithers if both were Either.Right + private static Either> zip(Either first, Either second) { + return first.flatMap(f -> second.map(s -> Tuple.of(f, s))); + } + private static String sortBySection(HashSet items, Store store) { var itemsBySection = items.groupBy(ShoppingItem::section); var sorted = store.sections().flatMap(s -> itemsBySection.get(s).getOrElse(HashSet.empty())); var rest = items.removeAll(sorted); - var sortedString = sorted.map(ShoppingItem::name).mkString(", "); + var formattedItems = sorted.map(ShoppingItem::name).mkString(", "); if (rest.isEmpty()) { - return sortedString; + return formattedItems; } - return sortedString + rest.mkString("\nItems not available or not classified in this store: ", ", ", ""); - } - - // Return a Tuple2 containing the values of two eithers if both were Either.Right - private static Either> zip(Either first, Either second) { - return first.flatMap(f -> second.map(s -> Tuple.of(f, s))); + return formattedItems + rest.mkString("\nItems not available or not classified in this store: ", ", ", ""); } private static Resp addToList(Map params, Dao dao) {