diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2023-05-06 10:25:36 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2025-08-18 12:10:56 -0400 |
| commit | f191882f821c1ea166fb423ff3bd39158f25da25 (patch) | |
| tree | 720d8e67694481cd4821b4e6017f3ef4b43c3444 | |
| parent | 25e5f367ff9c8ea787eb4406178cb0522f9de4d1 (diff) | |
| download | pantry-f191882f821c1ea166fb423ff3bd39158f25da25.tar.gz pantry-f191882f821c1ea166fb423ff3bd39158f25da25.tar.bz2 pantry-f191882f821c1ea166fb423ff3bd39158f25da25.zip | |
Add PantryItemLabel with GraphQL query and mutation
| -rw-r--r-- | pom.xml | 5 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/model/PantryItem.java | 2 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/model/PantryItemLabel.java | 17 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/view/PantryItemLabelResource.java | 55 | ||||
| -rw-r--r-- | src/main/webui/src/gql/labels.ts | 21 |
5 files changed, 100 insertions, 0 deletions
@@ -87,6 +87,11 @@ <version>${quarkus.quinoa.version}</version> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>${commons-lang3.version}</version> + </dependency> + <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> diff --git a/src/main/java/dev/submelon/model/PantryItem.java b/src/main/java/dev/submelon/model/PantryItem.java index fc4fdf0..30d9851 100644 --- a/src/main/java/dev/submelon/model/PantryItem.java +++ b/src/main/java/dev/submelon/model/PantryItem.java @@ -31,4 +31,6 @@ public class PantryItem extends ReactivePanacheMongoEntityBase { private String quantityUnitType; + private String[] labels; + } diff --git a/src/main/java/dev/submelon/model/PantryItemLabel.java b/src/main/java/dev/submelon/model/PantryItemLabel.java new file mode 100644 index 0000000..96c266a --- /dev/null +++ b/src/main/java/dev/submelon/model/PantryItemLabel.java @@ -0,0 +1,17 @@ +package dev.submelon.model; + +import io.quarkus.mongodb.panache.common.MongoEntity; +import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) +@MongoEntity(collection = "label") +public class PantryItemLabel extends ReactivePanacheMongoEntity { + + private String title; + + private String color; + +} diff --git a/src/main/java/dev/submelon/view/PantryItemLabelResource.java b/src/main/java/dev/submelon/view/PantryItemLabelResource.java new file mode 100644 index 0000000..985e96e --- /dev/null +++ b/src/main/java/dev/submelon/view/PantryItemLabelResource.java @@ -0,0 +1,55 @@ +package dev.submelon.view; + +import java.util.List; +import java.util.Optional; + +import org.apache.commons.lang3.StringUtils; +import org.bson.Document; +import org.eclipse.microprofile.graphql.Description; +import org.eclipse.microprofile.graphql.GraphQLApi; +import org.eclipse.microprofile.graphql.Mutation; +import org.eclipse.microprofile.graphql.Query; + +import dev.submelon.model.PantryItemLabel; +import io.smallrye.mutiny.Multi; +import io.smallrye.mutiny.Uni; + +@GraphQLApi +public class PantryItemLabelResource { + + @Query("allLabels") + @Description("Get all labels that can be assigned to items") + public Uni<List<PantryItemLabel>> getItems() { + return PantryItemLabel.findAll().list(); + } + + @Mutation + @Description("Create any new labels from list of labels") + public Uni<List<PantryItemLabel>> syncLabels(PantryItemLabel[] labels) { + return Multi.createFrom().items(labels) + .filter(label -> StringUtils.isNotBlank(label.getTitle())) + .onItem().transformToUniAndMerge(this::getOrCreateLabel) + .collect().asList(); + } + + private Uni<PantryItemLabel> getOrCreateLabel(PantryItemLabel label) { + return findByTitle(label.getTitle()) + .onItem().transformToUni(opt -> { + if (opt.isPresent()) { + return Uni.createFrom().item(opt.get()); + } else { + return persistNewLabel(label); + } + }); + } + + private Uni<Optional<PantryItemLabel>> findByTitle(String title) { + return PantryItemLabel + .find(new Document("title", title)) + .firstResultOptional(); + } + + private Uni<PantryItemLabel> persistNewLabel(PantryItemLabel label) { + return PantryItemLabel.persist(label).replaceWith(label); + } +} diff --git a/src/main/webui/src/gql/labels.ts b/src/main/webui/src/gql/labels.ts new file mode 100644 index 0000000..801e24b --- /dev/null +++ b/src/main/webui/src/gql/labels.ts @@ -0,0 +1,21 @@ +import { graphql } from "./conf/gql"; + +export const queryAllLabels = graphql(` + query allLabels { + allLabels { + id + title + color + } + } +`); + +export const mutationSyncLabels = graphql(` + mutation syncLabels($labels: [PantryItemLabelInput]) { + syncLabels(labels: $labels) { + id + title + color + } + } +`); |
