From 90a52cb2da970a7cb4f260fef6d964d119ab0d13 Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Fri, 4 Oct 2024 12:34:20 -0700 Subject: [PATCH] support a String-typed color property on Plan --- .../brennaswitzer/cookbook/domain/Plan.java | 32 +++++++++++++++++++ .../cookbook/graphql/PlannerMutation.java | 4 +++ .../cookbook/services/PlanService.java | 6 ++++ .../resources/db/changelog/gobrennas-2024.sql | 4 +++ src/main/resources/graphqls/planner.graphqls | 8 ++++- 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/brennaswitzer/cookbook/domain/Plan.java b/src/main/java/com/brennaswitzer/cookbook/domain/Plan.java index 48848674..bdce6b12 100644 --- a/src/main/java/com/brennaswitzer/cookbook/domain/Plan.java +++ b/src/main/java/com/brennaswitzer/cookbook/domain/Plan.java @@ -9,6 +9,7 @@ import lombok.Getter; import lombok.Setter; import org.hibernate.annotations.BatchSize; +import org.springframework.util.StringUtils; import java.util.HashSet; import java.util.Set; @@ -31,6 +32,8 @@ public class Plan extends PlanItem implements AccessControlled { @BatchSize(size = 50) private Set trashBinItems; + private String color; + public Plan() { } @@ -83,4 +86,33 @@ public User getOwner() { return getAcl().getOwner(); } + public String getColor() { + if (color == null) { + // generated at http://medialab.github.io/iwanthue/ + color = switch ((int) (get_eqkey() % 16)) { + case 0 -> "#cb4771"; + case 1 -> "#caa29e"; + case 2 -> "#783b32"; + case 3 -> "#d14f32"; + case 4 -> "#c9954c"; + case 5 -> "#cbd152"; + case 6 -> "#56713c"; + case 7 -> "#6dce55"; + case 8 -> "#8dd4aa"; + case 9 -> "#77adc2"; + case 10 -> "#6a7dc8"; + case 11 -> "#3b3a41"; + case 12 -> "#7145ca"; + case 13 -> "#552b6b"; + case 14 -> "#c583bd"; + default -> "#cc4ac0"; + }; + } + return color; + } + + public void setColor(String color) { + this.color = StringUtils.hasText(color) ? color : null; + } + } diff --git a/src/main/java/com/brennaswitzer/cookbook/graphql/PlannerMutation.java b/src/main/java/com/brennaswitzer/cookbook/graphql/PlannerMutation.java index f8f9a778..e5ef8b62 100644 --- a/src/main/java/com/brennaswitzer/cookbook/graphql/PlannerMutation.java +++ b/src/main/java/com/brennaswitzer/cookbook/graphql/PlannerMutation.java @@ -81,6 +81,10 @@ public PlanBucket updateBucket(Long planId, Long bucketId, String name, LocalDat return planService.updateBucket(planId, bucketId, name, date); } + public Plan setColor(Long planId, String color) { + return planService.setColor(planId, color); + } + public Plan setGrant(Long planId, Long userId, AccessLevel accessLevel) { return planService.setGrantOnPlan(planId, userId, accessLevel); } diff --git a/src/main/java/com/brennaswitzer/cookbook/services/PlanService.java b/src/main/java/com/brennaswitzer/cookbook/services/PlanService.java index 2e2b99ae..8dc745d8 100644 --- a/src/main/java/com/brennaswitzer/cookbook/services/PlanService.java +++ b/src/main/java/com/brennaswitzer/cookbook/services/PlanService.java @@ -514,4 +514,10 @@ public Plan revokeGrantFromPlan(Long planId, Long userId) { return plan; } + public Plan setColor(Long planId, String color) { + Plan plan = getPlanById(planId, AccessLevel.ADMINISTER); + plan.setColor(color); + return plan; + } + } diff --git a/src/main/resources/db/changelog/gobrennas-2024.sql b/src/main/resources/db/changelog/gobrennas-2024.sql index 2fa310ec..78c89a3c 100644 --- a/src/main/resources/db/changelog/gobrennas-2024.sql +++ b/src/main/resources/db/changelog/gobrennas-2024.sql @@ -461,3 +461,7 @@ where exists (select * --changeset barneyb:index-cook-history-owner-status CREATE INDEX idx_planned_recipe_history_owner_status ON planned_recipe_history (owner_id, status_id); + +--changeset barneyb:plan-color +ALTER TABLE plan_item + ADD color VARCHAR; diff --git a/src/main/resources/graphqls/planner.graphqls b/src/main/resources/graphqls/planner.graphqls index f8c60af3..b7e9d0e2 100644 --- a/src/main/resources/graphqls/planner.graphqls +++ b/src/main/resources/graphqls/planner.graphqls @@ -24,6 +24,10 @@ type Plan implements Node & Owned & AccessControlled & CorePlanItem { id: ID! owner: User! name: String! + """The color associated with the plan, expressed as a number sign and six + hex digits (e.g., '#F57F17'). + """ + color: String! """A plan's plan is always itself.""" plan: Plan! share: ShareInfo @@ -110,8 +114,10 @@ type PlannerMutation { an item under a different parent is included in the list, it will be moved under this item.""" reorderSubitems(parentId:ID!, itemIds: [ID!]!): PlanItem + """Set the plan's color (e.g., '#F57F17'), or reset it with a null or empty string.""" + setColor(planId: ID!, color: String): Plan! """Set the access level granted to a user w/in a plan.""" - setGrant(planId: ID!, userId: ID!, accessLevel:AccessLevel): Plan! + setGrant(planId: ID!, userId: ID!, accessLevel: AccessLevel): Plan! """Sets the status of the given item. This will always return the updated item, though it may immediately moved to the trash (in the background).""" setStatus(id: ID!, status: PlanItemStatus!, doneAt: DateTime): PlanItem!