Skip to content

Commit

Permalink
Merge pull request #135 from AY2021S1-CS2103T-T10-3/bugfix/134
Browse files Browse the repository at this point in the history
Fix #134
  • Loading branch information
seowalex authored Oct 24, 2020
2 parents 5244355 + 13eeac7 commit 7954d4a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/main/java/chopchop/commons/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Strings {
public static final ArgName ARG_QUANTITY = new ArgName("qty");
public static final ArgName ARG_EXPIRY = new ArgName("expiry");
public static final ArgName ARG_TAG = new ArgName("tag");
public static final ArgName ARG_NAME = new ArgName("name");

// command names
public static final String COMMAND_ADD = "add";
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/chopchop/logic/edit/RecipeEditDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
package chopchop.logic.edit;

import java.util.List;
import java.util.Optional;

public class RecipeEditDescriptor {

private final List<IngredientEditDescriptor> ingredientEdits;
private final List<StepEditDescriptor> stepEdits;
private final List<TagEditDescriptor> tagEdits;
private final Optional<String> nameEdit;

/**
* Creates a RecipeEditDescriptor to edit a recipe.
*
* @param ingredientEdits the list of edit descriptors for ingredients
* @param stepEdits the list of edit descriptors for steps
* @param tagEdits the list of edit descriptors for tags
* @param nameEdit the edited name (if present)
*/
public RecipeEditDescriptor(List<IngredientEditDescriptor> ingredientEdits,
public RecipeEditDescriptor(Optional<String> nameEdit, List<IngredientEditDescriptor> ingredientEdits,
List<StepEditDescriptor> stepEdits, List<TagEditDescriptor> tagEdits) {

this.ingredientEdits = ingredientEdits;
this.stepEdits = stepEdits;
this.tagEdits = tagEdits;
this.nameEdit = nameEdit;
}

public List<IngredientEditDescriptor> getIngredientEdits() {
Expand All @@ -36,4 +40,8 @@ public List<StepEditDescriptor> getStepEdits() {
public List<TagEditDescriptor> getTagEdits() {
return this.tagEdits;
}

public Optional<String> getNameEdit() {
return this.nameEdit;
}
}
72 changes: 35 additions & 37 deletions src/main/java/chopchop/logic/parser/commands/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import chopchop.model.attributes.Quantity;
import chopchop.model.attributes.Tag;
import chopchop.commons.util.Pair;
import chopchop.commons.util.Result;
import chopchop.commons.util.Strings;

import chopchop.logic.edit.EditDescriptor;
import chopchop.logic.edit.EditOperationType;
import chopchop.logic.edit.IngredientEditDescriptor;
import chopchop.logic.edit.RecipeEditDescriptor;
Expand Down Expand Up @@ -54,7 +52,10 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
})
.then(item -> {

var edits = new ArrayList<Result<EditDescriptor>>();
Optional<String> editedName = Optional.empty();
var tagEdits = new ArrayList<Result<TagEditDescriptor>>();
var stepEdits = new ArrayList<Result<StepEditDescriptor>>();
var ingrEdits = new ArrayList<Result<IngredientEditDescriptor>>();

for (int i = 0; i < args.getAllArguments().size(); i++) {

Expand All @@ -66,13 +67,23 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
return Result.error("'%s' needs an edit-argument in an edit command", argName);
}

if (argName.name().equals(Strings.ARG_TAG.name())) {
if (argName.name().equals(Strings.ARG_NAME.name())) {

edits.add(parseTagEdit(argName, argValue));
if (argValue.isEmpty()) {
return Result.error("expected a name after '/name'");
} else if (editedName.isPresent()) {
return Result.error("only one '/name' should be provided");
}

editedName = Optional.of(argValue);

} else if (argName.name().equals(Strings.ARG_TAG.name())) {

tagEdits.add(parseTagEdit(argName, argValue));

} else if (argName.name().equals(Strings.ARG_STEP.name())) {

edits.add(parseStepEdit(argName, argValue));
stepEdits.add(parseStepEdit(argName, argValue));

} else if (argName.name().equals(Strings.ARG_INGREDIENT.name())) {

Expand All @@ -93,49 +104,36 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
}
}

edits.add(parseIngredientEdit(argName, argValue, qty));
ingrEdits.add(parseIngredientEdit(argName, argValue, qty));

} else {
return Result.error("'edit' command doesn't support '%s'", argName);
}
}

return Result.of(Pair.of(item, edits));
})
.map(pair -> pair.mapSnd(x -> Result.sequence(x)))
.then(pair -> {

var ingrEdits = new ArrayList<IngredientEditDescriptor>();
var stepEdits = new ArrayList<StepEditDescriptor>();
var tagEdits = new ArrayList<TagEditDescriptor>();

if (pair.snd().isError()) {
return Result.error(pair.snd().getError());
}

var list = pair.snd().getValue();
for (var item : list) {
if (item instanceof TagEditDescriptor) {
tagEdits.add((TagEditDescriptor) item);
} else if (item instanceof StepEditDescriptor) {
stepEdits.add((StepEditDescriptor) item);
} else if (item instanceof IngredientEditDescriptor) {
ingrEdits.add((IngredientEditDescriptor) item);
} else {
// unreachable
assert false : "invalid edit descriptor";
}
// ugly AF, but i don't care at the moment.
var tes = Result.sequence(tagEdits);
var ses = Result.sequence(stepEdits);
var ies = Result.sequence(ingrEdits);

if (tes.isError()) {
return Result.error(tes.getError());
} else if (ses.isError()) {
return Result.error(ses.getError());
} else if (ies.isError()) {
return Result.error(ies.getError());
}

return Result.of(new EditCommandStub(pair.fst(),
new RecipeEditDescriptor(ingrEdits, stepEdits, tagEdits)));
return Result.of(new EditCommandStub(item,
new RecipeEditDescriptor(editedName, ies.getValue(), ses.getValue(), tes.getValue())
));
});
}




private static Result<EditDescriptor> parseIngredientEdit(ArgName argName, String ingredientName,
private static Result<IngredientEditDescriptor> parseIngredientEdit(ArgName argName, String ingredientName,
Optional<Quantity> qty) {

var components = argName.getComponents();
Expand Down Expand Up @@ -163,7 +161,7 @@ private static Result<EditDescriptor> parseIngredientEdit(ArgName argName, Strin



private static Result<EditDescriptor> parseTagEdit(ArgName argName, String argValue) {
private static Result<TagEditDescriptor> parseTagEdit(ArgName argName, String argValue) {

var components = argName.getComponents();

Expand All @@ -184,7 +182,7 @@ private static Result<EditDescriptor> parseTagEdit(ArgName argName, String argVa
));
}

private static Result<EditDescriptor> parseStepEdit(ArgName argName, String argValue) {
private static Result<StepEditDescriptor> parseStepEdit(ArgName argName, String argValue) {

var components = argName.getComponents();

Expand Down

0 comments on commit 7954d4a

Please sign in to comment.