Skip to content

Commit

Permalink
delete items
Browse files Browse the repository at this point in the history
  • Loading branch information
barneyb committed Dec 28, 2023
1 parent a574f62 commit 88938c3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public Plan deleteBucket(Long planId, Long bucketId) {
return planService.deleteBucket(planId, bucketId).getPlan();
}

public PlanItem deleteItem(Long id) {
return planService.deleteItemForParent(id);
}

public boolean deletePlan(Long id) {
planService.deleteItem(id);
planService.deletePlan(id);
return true;
}

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/brennaswitzer/cookbook/services/PlanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,27 +355,39 @@ private PlanMessage buildUpdateMessage(PlanItem item) {

public PlanMessage setItemStatus(Long id, PlanItemStatus status) {
if (PlanItemStatus.COMPLETED.equals(status) || PlanItemStatus.DELETED.equals(status)) {
return deleteItem(id);
return deleteItemForMessage(id);
}
PlanItem item = getPlanItemById(id, AccessLevel.CHANGE);
item.setStatus(status);
return buildUpdateMessage(item);
}

public PlanMessage deleteItem(Long id) {
public PlanItem deleteItemForParent(Long id) {
val item = getPlanItemById(id, AccessLevel.CHANGE);
if (item.hasParent()) {
val parent = item.getParent();
item.moveToTrash();
return parent;
} else {
// a plan
itemRepo.delete(item);
throw new IllegalArgumentException(String.format(
"ID '%s' is a plan",
id));
}
}

public PlanMessage deleteItemForMessage(Long id) {
deleteItemForParent(id);
val m = new PlanMessage();
m.setId(id);
m.setType("delete");
return m;
}

public void deletePlan(Long id) {
val plan = getPlanById(id, AccessLevel.ADMINISTER);
planRepo.delete(plan);
}

public void severLibraryLinks(Recipe r) {
itemRepo.findByIngredient(r).forEach(t -> {
if (!t.hasNotes()) t.setNotes(r.getDirections());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public PlanMessage setStatus(
public void deletePlan(
@PathVariable("planId") Long planId
) {
planService.deleteItem(planId);
planService.deletePlan(planId);
}

@DeleteMapping("/{planId}/{id}")
Expand All @@ -212,7 +212,7 @@ public void deleteItem(
if (!planId.equals(item.getPlan().getId())) {
throw new IllegalArgumentException("Item belongs to a different plan");
}
planService.deleteItem(id);
planService.deleteItemForParent(id);
}

@PostMapping("/{id}/buckets")
Expand Down
27 changes: 16 additions & 11 deletions src/main/resources/graphqls/planner.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@ type PlannerQuery {
interface CorePlanItem implements Node {
id: ID!
name: String!
childCount: NonNegativeInt
plan: Plan!
childCount: NonNegativeInt!
children: [PlanItem!]!
descendantCount: NonNegativeInt
descendantCount: NonNegativeInt!
descendants: [PlanItem!]!
}

type Plan implements Node & Owned & AccessControlled {
type Plan implements Node & Owned & AccessControlled & CorePlanItem {
id: ID!
owner: User!
name: String!
"""A plan's plan is always itself."""
plan: Plan!
grants: [AccessControlEntry!]!
childCount: NonNegativeInt
childCount: NonNegativeInt!
children: [PlanItem!]!
descendantCount: NonNegativeInt
descendantCount: NonNegativeInt!
descendants: [PlanItem!]!
bucketCount: NonNegativeInt
bucketCount: NonNegativeInt!
buckets: [PlanBucket!]!
}

"""Represents a single item on a plan"""
type PlanItem implements Node {
type PlanItem implements Node & CorePlanItem {
id: ID!
name: String!
quantity: Quantity
preparation: String
notes: String
plan: Plan!
parent: PlanItem
childCount: NonNegativeInt
childCount: NonNegativeInt!
children: [PlanItem!]!
descendantCount: NonNegativeInt
descendantCount: NonNegativeInt!
descendants: [PlanItem!]!
aggregate: PlanItem
componentCount: NonNegativeInt
componentCount: NonNegativeInt!
components: [PlanItem!]!
bucket: PlanBucket
status: PlanItemStatus!
Expand Down Expand Up @@ -85,9 +88,11 @@ type PlannerMutation {
deleteBucket(planId: ID!, bucketId: ID!): Plan!
"""Deletes the grant for a user w/in a plan, if one exists."""
deleteGrant(planId: ID!, userId: ID!): Plan!
"""Deletes an item from a plan, and return its former parent (plan or item). This operation cascades."""
deleteItem(id: ID!): PlanItem
"""Deletes the given plan, and all its related data."""
deletePlan(id: ID!): Boolean
"""Update the name of the given plan item."""
"""Update the name of the given plan or plan item (but not bucket)."""
rename(id: ID!, name: String!): PlanItem!
"""Set the access level granted to a user w/in a plan."""
setGrant(planId: ID!, userId: ID!, accessLevel:AccessLevel): Plan!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,26 @@ void deleteBucket() {
assertSame(plan, result);
}

@Test
void deleteItem() {
long itemId = 123L;
PlanItem parent = mock(PlanItem.class);
when(planService.deleteItemForParent(itemId))
.thenReturn(parent);

PlanItem result = mutation.deleteItem(itemId);

assertSame(parent, result);
}

@Test
void deletePlan() {
long planId = 123L;

boolean result = mutation.deletePlan(planId);

assertTrue(result);
verify(planService).deleteItem(planId);
verify(planService).deletePlan(planId);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ public void deleteItem() {

assertEquals(4, itemRepo.count());

service.deleteItem(oj.getId());
service.deleteItemForParent(oj.getId());
itemRepo.flush();
entityManager.clear();

assertEquals(4, itemRepo.count());
assertEquals(3, itemRepo.countByStatusNot(PlanItemStatus.DELETED));

service.deleteItem(groceries.getId());
service.deletePlan(groceries.getId());
itemRepo.flush();
entityManager.clear();

Expand Down

0 comments on commit 88938c3

Please sign in to comment.