Skip to content

Commit

Permalink
WIP Fix graphql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Nov 22, 2024
1 parent 344d121 commit 908f4b9
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective

//Create
ids.putAll(createActivityDirectives(planId, toAdd, activityToGoalId, schedulerModel));
modifyActivityDirectives(planId, toModify, activityToGoalId);
deleteActivityDirectives(planId, toDelete);
modifyActivityDirectives(planId, toModify);
// deleteActivityDirectives(planId, toDelete);
return ids;
}

Expand Down Expand Up @@ -638,25 +638,28 @@ mutation createAllPlanActivityDirectives($activities: [activity_directive_insert
return activityToDirectiveId;
}



private void modifyActivityDirectives(
final PlanId planId,
final List<SchedulingActivity> activities,
final Map<SchedulingActivity, GoalId> activityToGoalId
final List<SchedulingActivity> activities
)
throws IOException, NoSuchPlanException, MerlinServiceException
{
ensurePlanExists(planId);
final var request = new StringBuilder();
request.append("mutation updatePlanActivityDirectives($activities: [activity_directive_set_input!]!) {");
var index = 0;
final var activitiesObject = Json.createArrayBuilder();
request.append("mutation updatePlanActivityDirectives(");
request.append(String.join(
",",
activities.stream().map($ -> "$activity_%d: activity_directive_set_input!".formatted($.id().id())).toList()
));
request.append(") {");
final var arguments = Json.createObjectBuilder();
for (final var act : activities) {
final var id = act.id().id();
request.append("""
update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activities[%d]) {}
""".formatted(act.id().id(), act.id().id(), planId.id(), index));
index++;
update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activity_%d) {
affected_rows
}
""".formatted(id, id, planId.id(), id));

final var activityObject = Json
.createObjectBuilder()
Expand All @@ -669,13 +672,9 @@ private void modifyActivityDirectives(
insertionObjectArguments.add(arg.getKey(), serializedValueP.unparse(arg.getValue()));
}
activityObject.add("arguments", insertionObjectArguments.build());
activitiesObject.add(activityObject);
arguments.add("activity_%d".formatted(id), activityObject);
}
final var arguments = Json
.createObjectBuilder()
.add("activities", activitiesObject.build())
.build();
postRequest(request.toString(), arguments).orElseThrow(() -> new NoSuchPlanException(planId));
postRequest(request.toString(), arguments.build()).orElseThrow(() -> new NoSuchPlanException(planId));
}

private void deleteActivityDirectives(
Expand All @@ -689,9 +688,10 @@ private void deleteActivityDirectives(
request.append("mutation deletePlanActivityDirectives {");
for (final var id : ids) {
request.append("""
delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {}
delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {affected_rows}
""".formatted(id.id(), id.id(), planId.id()));
}
request.append("}");
postRequest(request.toString()).orElseThrow(() -> new NoSuchPlanException(planId));
}

Expand Down

0 comments on commit 908f4b9

Please sign in to comment.