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 #174 from aliciamichellew/alicia/clear-tag-note
Add note `cleartag` feature
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/seedu/address/logic/commands/NoteClearTagCommand.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,78 @@ | ||
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.logic.commands.note.NoteCommand; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.SeplendidModel; | ||
import seedu.address.model.notes.Note; | ||
import seedu.address.seplendidui.UiUtil; | ||
|
||
/** | ||
* Clears all tags to a note to the NoteList. | ||
*/ | ||
public class NoteClearTagCommand extends NoteCommand { | ||
|
||
public static final String NOTE_CLEAR_TAG_MESSAGE_USAGE = COMMAND_WORD | ||
+ " cleartag [index] : Removes all tags to a note."; | ||
public static final String MESSAGE_NONEXISTENT_NOTE = "Note not found, please put a valid index."; | ||
public static final String ACTION_WORD = "cleartag"; | ||
public static final String MESSAGE_SUCCESS = "Removed tag for Note: %1$s"; | ||
private final Integer noteIndexToUpdate; | ||
/** | ||
* Creates a NoteClearTagCommand to update the specified {@code Note} | ||
* | ||
* @param noteIndexToUpdate The index of Note to be updated into Storage. | ||
*/ | ||
public NoteClearTagCommand(int noteIndexToUpdate) { | ||
super(); | ||
requireNonNull(noteIndexToUpdate); | ||
this.noteIndexToUpdate = noteIndexToUpdate; | ||
} | ||
|
||
@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().clear(); | ||
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 NoteClearTagCommand)) { | ||
return false; | ||
} | ||
|
||
NoteClearTagCommand otherNoteClearTagCommand = (NoteClearTagCommand) other; | ||
return noteIndexToUpdate.equals(otherNoteClearTagCommand.noteIndexToUpdate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("noteToUpdate", noteIndexToUpdate) | ||
.toString(); | ||
} | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/address/logic/parser/NoteClearTagCommandParser.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_INDEX; | ||
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty; | ||
|
||
import seedu.address.logic.commands.NoteClearTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ClearTagCommand | ||
* and returns a ClearTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public class NoteClearTagCommandParser implements Parser<NoteClearTagCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the NoteClearTagCommand | ||
* and returns a NoteClearTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public NoteClearTagCommand parse(String args) throws ParseException { | ||
if (!areValuesEnclosedAndNonEmpty(args)) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteClearTagCommand.NOTE_CLEAR_TAG_MESSAGE_USAGE)); | ||
} | ||
|
||
SeplendidArgumentMap parameterToArgMap = | ||
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_INDEX); | ||
|
||
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_INDEX)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteClearTagCommand.NOTE_CLEAR_TAG_MESSAGE_USAGE)); | ||
} | ||
|
||
// All arguments should be a non-empty {@code Optional} | ||
|
||
try { | ||
Integer noteIndex = Integer.valueOf(parameterToArgMap.getValue(PARAMETER_INDEX).get()); | ||
return new NoteClearTagCommand(noteIndex); | ||
} catch (NumberFormatException e) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
NoteClearTagCommand.NOTE_CLEAR_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