Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete tag bug #204

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.stream.Collectors;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagContainsKeywordsPredicate;

Expand All @@ -22,6 +21,7 @@ public class DeleteTagCommand extends DeleteAbstractCommand {
+ "Example: " + COMMAND_WORD + " friend";

public static final String MESSAGE_DELETE_TAG_SUCCESS = "Deleted all contacts with tag: %1$s";
public static final String MESSAGE_TAG_DOES_NOT_EXIST = "The following tags do not exist: %1$s";

private final TagContainsKeywordsPredicate tagToDelete;

Expand All @@ -43,16 +43,25 @@ public DeleteTagCommand(TagContainsKeywordsPredicate tagToDelete) {
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Tag> tags = tagToDelete.getTags();
System.out.println("Deleting all contacts with tag: " + tags);
for (Tag tag : tags) {
System.out.println(tag);
model.getFilteredPersonList().stream()
.filter(person -> person.getTags().contains(tag))
.peek(person -> System.out.println(person))
.forEach(person -> model.deletePerson(person));
List<Tag> allTags = model.getAddressBook().getPersonList().stream()
.flatMap(person -> person.getTags().stream())
.distinct()
.collect(Collectors.toList());

if (!allTags.containsAll(tags)) {
List<Tag> nonExistentTags = tags.stream()
.filter(tag -> !allTags.contains(tag))
.collect(Collectors.toList());
throw new CommandException(String.format(MESSAGE_TAG_DOES_NOT_EXIST, nonExistentTags));
}

List<Person> personsToDelete = model.getFilteredPersonList().stream()
.filter(person -> tagToDelete.test(person))
.collect(Collectors.toList());

personsToDelete.forEach(model::deletePerson);

return new CommandResult(String.format(MESSAGE_DELETE_TAG_SUCCESS, tagToDelete));
}

Expand All @@ -62,19 +71,12 @@ public boolean equals(Object other) {
return true;
}

// instanceof handles nulls
if (!(other instanceof DeleteCommand)) {
if (!(other instanceof DeleteTagCommand)) {
return false;
}

DeleteTagCommand otherDeleteTagCommand = (DeleteTagCommand) other;
return tagToDelete.equals(otherDeleteTagCommand.tagToDelete);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("tagToDelete", tagToDelete)
.toString();
}
}
Loading