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 #173 from aliciamichellew/alicia/tag-note
Add note `tag` feature
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/seedu/address/logic/commands/NoteTagCommand.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,84 @@ | ||
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.Note; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.seplendidui.UiUtil; | ||
|
||
/** | ||
* Adds a tag to a note to the NoteList. | ||
*/ | ||
public class NoteTagCommand extends NoteCommand { | ||
|
||
public static final String NOTE_TAG_MESSAGE_USAGE = COMMAND_WORD | ||
+ " tag [index] [tag]: Add a tag to a note."; | ||
public static final String MESSAGE_NONEXISTENT_NOTE = "Note not found, please put a valid index."; | ||
public static final String ACTION_WORD = "tag"; | ||
public static final String MESSAGE_SUCCESS = "Note tagged: %1$s"; | ||
private final Integer noteIndexToUpdate; | ||
private final Tag addTag; | ||
|
||
/** | ||
* Creates a NoteTagCommand to update the specified {@code Note} | ||
* | ||
* @param noteIndexToUpdate The index of Note to be updated into Storage. | ||
* @param addTag The tag to be added to the note. | ||
*/ | ||
public NoteTagCommand(int noteIndexToUpdate, Tag addTag) { | ||
super(); | ||
requireNonNull(noteIndexToUpdate); | ||
this.noteIndexToUpdate = noteIndexToUpdate; | ||
this.addTag = addTag; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(SeplendidModel seplendidModel) throws CommandException { | ||
requireNonNull(seplendidModel); | ||
int noteListSize = seplendidModel.getNoteCatalogue().getNoteList().size(); | ||
if (noteListSize < this.noteIndexToUpdate) { | ||
throw new CommandException(MESSAGE_NONEXISTENT_NOTE); | ||
} | ||
Note oldNote = seplendidModel.getNoteCatalogue().getNoteList().get(this.noteIndexToUpdate - 1); | ||
oldNote.getTags().add(addTag); | ||
Note newNote = new Note(oldNote.getContent(), oldNote.getTags(), oldNote.getIndex()); | ||
seplendidModel.setNote(oldNote, newNote); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(newNote)), | ||
UiUtil.ListViewModel.NOTE_LIST); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof NoteTagCommand)) { | ||
return false; | ||
} | ||
|
||
NoteTagCommand otherNoteTagCommand = (NoteTagCommand) other; | ||
return noteIndexToUpdate.equals(otherNoteTagCommand.noteIndexToUpdate) | ||
&& addTag.equals(otherNoteTagCommand.addTag); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("noteToUpdate", noteIndexToUpdate) | ||
.add("tag", addTag) | ||
.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
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/parser/NoteTagCommandParser.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,49 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PARAMETER_INDEX; | ||
import static seedu.address.logic.parser.CliSyntax.PARAMETER_TAGS; | ||
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty; | ||
|
||
import seedu.address.logic.commands.NoteTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the TagCommand | ||
* and returns a TagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public class NoteTagCommandParser implements Parser<NoteTagCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the NoteTagCommand | ||
* and returns a NoteTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public NoteTagCommand parse(String args) throws ParseException { | ||
if (!areValuesEnclosedAndNonEmpty(args)) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteTagCommand.NOTE_TAG_MESSAGE_USAGE)); | ||
} | ||
|
||
SeplendidArgumentMap parameterToArgMap = | ||
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_INDEX, PARAMETER_TAGS); | ||
|
||
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_INDEX, PARAMETER_TAGS)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteTagCommand.NOTE_TAG_MESSAGE_USAGE)); | ||
} | ||
|
||
// All arguments should be a non-empty {@code Optional} | ||
|
||
try { | ||
Integer noteIndex = Integer.valueOf(parameterToArgMap.getValue(PARAMETER_INDEX).get()); | ||
Tag newTag = new Tag(parameterToArgMap.getValue(PARAMETER_TAGS).get()); | ||
return new NoteTagCommand(noteIndex, newTag); | ||
} catch (NumberFormatException e) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteTagCommand.NOTE_TAG_MESSAGE_USAGE)); | ||
} | ||
} | ||
} |
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