forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from aliciamichellew/alicia/note-search-tags-…
…feature Add `search` by tags feature for Note
- Loading branch information
Showing
11 changed files
with
224 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/seedu/address/logic/commands/NoteSearchCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.SeplendidModel; | ||
import seedu.address.model.notes.NoteTagContainsKeywordsPredicate; | ||
import seedu.address.seplendidui.UiUtil; | ||
|
||
/** | ||
* Finds and lists all notes in SEPlendid whose tags contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public class NoteSearchCommand extends NoteCommand { | ||
public static final String ACTION_WORD = "search"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Notes searched: %1$s"; | ||
|
||
public static final String MESSAGE_NONEXISTENT_NOTES = "This note does not exist in SEPlendid"; | ||
|
||
public static final String NOTE_SEARCH_MESSAGE_USAGE = COMMAND_WORD | ||
+ " search [note_tag_keyword]: Search notes with the same tag keyword"; | ||
private final NoteTagContainsKeywordsPredicate predicate; | ||
|
||
public NoteSearchCommand(NoteTagContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
throw new CommandException("TBD: this is a stub and should be removed after morph."); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(SeplendidModel model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
model.getSearchNoteIfExists(predicate); | ||
|
||
if (model.getFilteredLocalCourseList().isEmpty()) { | ||
throw new CommandException(MESSAGE_NONEXISTENT_NOTES); | ||
} | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(model.getFilteredNoteList())), | ||
UiUtil.ListViewModel.NOTE_LIST); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof NoteSearchCommand)) { | ||
return false; | ||
} | ||
|
||
NoteSearchCommand otherNoteSearchCommand = (NoteSearchCommand) other; | ||
return predicate.equals(otherNoteSearchCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/address/logic/parser/NoteSearchCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PARAMETER_TAGS; | ||
|
||
import seedu.address.logic.commands.NoteSearchCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.notes.NoteTagContainsKeywordsPredicate; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SearchCommand | ||
* and returns a SearchCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public class NoteSearchCommandParser implements Parser<NoteSearchCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the NoteSearchCommand | ||
* and returns a NoteSearchCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public NoteSearchCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (!ParserUtil.areValuesEnclosedAndNonEmpty(args)) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteSearchCommand.NOTE_SEARCH_MESSAGE_USAGE) | ||
); | ||
} | ||
|
||
SeplendidArgumentMap parameterToArgMap = | ||
SeplendidArgumentTokenizer.tokenize(args, | ||
PARAMETER_TAGS); | ||
|
||
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, | ||
PARAMETER_TAGS)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteSearchCommand.NOTE_SEARCH_MESSAGE_USAGE)); | ||
} | ||
|
||
Tag tag = ParserUtil.parseTag( | ||
parameterToArgMap.getValue(PARAMETER_TAGS).get()); | ||
return new NoteSearchCommand(new NoteTagContainsKeywordsPredicate(tag.toString())); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/seedu/address/model/notes/NoteTagContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package seedu.address.model.notes; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Note}'s {@code Tag} matches any of the keywords given. | ||
*/ | ||
public class NoteTagContainsKeywordsPredicate implements Predicate<Note> { | ||
private final String keyword; | ||
public NoteTagContainsKeywordsPredicate(String keywords) { | ||
this.keyword = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Note note) { | ||
return note.getTags().toString().toLowerCase() | ||
.contains(keyword.toLowerCase()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof seedu.address.model.notes.NoteTagContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
NoteTagContainsKeywordsPredicate otherTagContainsKeywordsPredicate = | ||
(NoteTagContainsKeywordsPredicate) other; | ||
return keyword.equals(otherTagContainsKeywordsPredicate.keyword); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keyword", keyword).toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters