Skip to content

Commit

Permalink
Merge pull request #174 from aliciamichellew/alicia/clear-tag-note
Browse files Browse the repository at this point in the history
Add note `cleartag` feature
  • Loading branch information
aliciamichellew authored Nov 2, 2023
2 parents 6714d73 + ce383b3 commit 1fc0733
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
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();
}
}

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));
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/parser/SeplendidParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.commons.core.SeplendidLogsCenter;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.NoteClearTagCommand;
import seedu.address.logic.commands.localcourse.LocalCourseAddCommand;
import seedu.address.logic.commands.localcourse.LocalCourseCommand;
import seedu.address.logic.commands.localcourse.LocalCourseDeleteCommand;
Expand Down Expand Up @@ -292,6 +293,8 @@ private NoteCommand getNoteCommandWithArg(String userInput, String actionWord, S
return new NoteUpdateCommandParser().parse(arguments);
case NoteTagCommand.ACTION_WORD:
return new NoteTagCommandParser().parse(arguments);
case NoteClearTagCommand.ACTION_WORD:
return new NoteClearTagCommandParser().parse(arguments);
default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down

0 comments on commit 1fc0733

Please sign in to comment.