Return all valid sections on bad request
This commit is contained in:
parent
c8a66fbe73
commit
43f192178e
|
@ -10,12 +10,20 @@ public enum Section {
|
||||||
PASTA,
|
PASTA,
|
||||||
RICE,
|
RICE,
|
||||||
MEAT,
|
MEAT,
|
||||||
|
FISH,
|
||||||
DAIRY,
|
DAIRY,
|
||||||
HYGIENE;
|
HYGIENE,
|
||||||
|
BEVERAGES;
|
||||||
|
|
||||||
public static Either<String, Section> tryParse(String catName) {
|
public static Either<String, Section> tryParse(String catName) {
|
||||||
return Stream.of(Section.values())
|
return Stream.of(Section.values())
|
||||||
.find(c -> c.name().equalsIgnoreCase(catName))
|
.find(c -> c.name().equalsIgnoreCase(catName))
|
||||||
.toEither("Category “%s” not found".formatted(catName));
|
.toEither("Category “%s” not found".formatted(catName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String valuesAsString() {
|
||||||
|
return Stream.of(Section.values())
|
||||||
|
.map(Section::name)
|
||||||
|
.mkString("\nValid values are: ", ", ", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import io.vavr.control.Either;
|
||||||
|
|
||||||
public record ShoppingItem(String name, Section section) {
|
public record ShoppingItem(String name, Section section) {
|
||||||
public static Either<Resp, ShoppingItem> tryParse(String name, String category) {
|
public static Either<Resp, ShoppingItem> tryParse(String name, String category) {
|
||||||
return Section.tryParse(category).map(c -> new ShoppingItem(name, c));
|
return Section.tryParse(category)
|
||||||
|
.map(c -> new ShoppingItem(name, c))
|
||||||
|
.mapLeft(msg -> Resp.BAD_REQUEST.apply(msg + Section.valuesAsString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import io.vavr.control.Either;
|
||||||
public record Store(String name, Seq<Section>sections) {
|
public record Store(String name, Seq<Section>sections) {
|
||||||
public static Either<Resp, Store> tryParse(String name, String sections) {
|
public static Either<Resp, Store> tryParse(String name, String sections) {
|
||||||
return Either.traverse(List.of(sections.split(",")), Section::tryParse)
|
return Either.traverse(List.of(sections.split(",")), Section::tryParse)
|
||||||
.mapLeft(errors -> Resp.BAD_REQUEST.apply(errors.mkString("\n")))
|
.mapLeft(errors -> Resp.BAD_REQUEST.apply(errors.mkString("\n") + Section.valuesAsString()))
|
||||||
.map(secs -> new Store(name, secs));
|
.map(secs -> new Store(name, secs));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user