Skip to content

Commit

Permalink
Merge pull request #173 from aliciamichellew/alicia/tag-note
Browse files Browse the repository at this point in the history
Add note `tag` feature
  • Loading branch information
aliciamichellew authored Nov 2, 2023
2 parents 92e1bda + e33e5df commit c833013
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/seedu/address/logic/commands/NoteTagCommand.java
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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public boolean equals(Object other) {
public String toString() {
return new ToStringBuilder(this)
.add("noteToUpdate", noteIndexToUpdate)
.add("content", updateContent)
.toString();
}
}
49 changes: 49 additions & 0 deletions src/main/java/seedu/address/logic/parser/NoteTagCommandParser.java
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));
}
}
}
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 @@ -25,6 +25,7 @@
import seedu.address.logic.commands.NoteDeleteCommand;
import seedu.address.logic.commands.NoteListCommand;
import seedu.address.logic.commands.NoteSearchCommand;
import seedu.address.logic.commands.NoteTagCommand;
import seedu.address.logic.commands.NoteUpdateCommand;
import seedu.address.logic.commands.PartnerCourseAddCommand;
import seedu.address.logic.commands.PartnerCourseCommand;
Expand Down Expand Up @@ -260,6 +261,8 @@ private NoteCommand getNoteCommandWithArg(String userInput, String actionWord, S
return new NoteDeleteCommandParser().parse(arguments);
case NoteUpdateCommand.ACTION_WORD:
return new NoteUpdateCommandParser().parse(arguments);
case NoteTagCommand.ACTION_WORD:
return new NoteTagCommandParser().parse(arguments);
default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down

0 comments on commit c833013

Please sign in to comment.