Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch reminder edit #246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.address.logic.commands.reminder;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.List;

Expand All @@ -12,8 +15,6 @@
import seedu.address.model.person.Person;
import seedu.address.model.reminder.Reminder;



/**
* Adds a reminder to the address book.
*/
Expand All @@ -22,13 +23,13 @@ public class AddReminderCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a reminder to the address book. "
+ "Parameters: "
+ "Person "
+ "DATE and TIME "
+ "Description\n"
+ PREFIX_NAME + "NAME "
+ PREFIX_DATE_TIME + "DATE and TIME "
+ PREFIX_DESCRIPTION + "DESCRIPTION\n"
+ "Example: " + COMMAND_WORD + " "
+ "John "
+ "2021-12-31 23:59"
+ "New Year's Eve";
+ PREFIX_NAME + "John "
+ PREFIX_DATE_TIME + "2021-12-31 23:59"
+ PREFIX_DESCRIPTION + "New Year's Eve";

public static final String MESSAGE_SUCCESS = "New reminder added: %1$s";
public static final String MESSAGE_NONEXISTENT_PERSON = "This person doesn't exist in the address book.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,113 @@
package seedu.address.logic.commands.reminder;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javafx.collections.ObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsDeletePredicate;
import seedu.address.model.person.Person;
import seedu.address.model.reminder.Reminder;
import seedu.address.model.reminder.ReminderDescription;

/**
* Edits a reminder in the address book.
*/
public class EditReminderCommand extends Command {
public static final String COMMAND_WORD = "editReminder";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book. "
+ "Parameters: "
+ "INDEX "
+ "DATE "
+ "TIME "
+ "MESSAGE\n"
+ "Example: " + COMMAND_WORD + " "
+ "1 "
+ "2021-12-31 "
+ "23:59 "
+ "New Year's Eve";
public static final String COMMAND_WORD = "re"; // reminder edit

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book identified "

Check warning on line 30 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L30

Added line #L30 was not covered by tests
+ "by the index number displayed in the reminder list. "
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear structure of edit reminder command!

+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be positive integer)"
+ PREFIX_DATE_TIME + "DATE and TIME "
+ PREFIX_DESCRIPTION + "DESCRIPTION\n"
+ "Example: "
+ COMMAND_WORD + " 1 "
+ PREFIX_DATE_TIME + " 2022-01-01 00:00 "
+ PREFIX_DESCRIPTION + " New Year's";

public static final String MESSAGE_EDIT_REMINDER_SUCCESS = "Edited reminder %1$s";
public static final String MESSAGE_REMINDER_NOT_EDITED = "At least one field must be edited";

private final Index index;
private final EditReminderFields editReminderFields;

/**
* @param index of the reminder in the reminder list to edit
* @param editReminderFields details of the edited reminder
*/
public EditReminderCommand(Index index, EditReminderFields editReminderFields) {
requireNonNull(index);
requireNonNull(editReminderFields);

Check warning on line 53 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L51-L53

Added lines #L51 - L53 were not covered by tests

this.index = index;
this.editReminderFields = new EditReminderFields(editReminderFields);
}

Check warning on line 57 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L55-L57

Added lines #L55 - L57 were not covered by tests

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Reminder> lastShownList = model.getDisplayReminders();

Check warning on line 62 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L61-L62

Added lines #L61 - L62 were not covered by tests

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX);

Check warning on line 65 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L65

Added line #L65 was not covered by tests
}

Reminder reminderToEdit = lastShownList.get(index.getZeroBased());
Reminder editedReminder = createEditedReminder(reminderToEdit, editReminderFields);

Check warning on line 69 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L68-L69

Added lines #L68 - L69 were not covered by tests

if (!reminderToEdit.isSameReminder(editedReminder) && model.hasReminder(editedReminder)) {
throw new CommandException(MESSAGE_REMINDER_NOT_EDITED);

Check warning on line 72 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L72

Added line #L72 was not covered by tests
}

// Parse input using the NameContainsKeywordsPredicate
String fullName = reminderToEdit.getPersonName() + "/";
String[] nameKeywords = fullName.split("\\s+");
NameContainsKeywordsDeletePredicate predicate = new NameContainsKeywordsDeletePredicate(
List.of(nameKeywords));
ObservableList<Person> persons = model.getClientHub().getPersonList();
List<Person> matchingPersons = persons.filtered(predicate);

Check warning on line 81 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L76-L81

Added lines #L76 - L81 were not covered by tests

// Check if there is exactly one match
if (matchingPersons.size() == 1) {
Person person = matchingPersons.get(0);
person.deleteReminder(reminderToEdit);
person.addReminder(editedReminder);
model.setReminder(reminderToEdit, editedReminder);
model.updateFilteredReminderList(Model.PREDICATE_SHOW_ALL_REMINDERS);
return new CommandResult(String.format(MESSAGE_EDIT_REMINDER_SUCCESS, Messages.format(reminderToEdit)));

Check warning on line 90 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L85-L90

Added lines #L85 - L90 were not covered by tests
} else {
throw new CommandException("More than one person with the specified name found. Please be more specific.");

Check warning on line 92 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L92

Added line #L92 was not covered by tests
}
}

/**
* Creates and returns an edited a {@code Reminder} with the details of {@code reminderToEdit}
* edited with the details of {@code editReminderFields}.
*/
public static Reminder createEditedReminder(Reminder reminderToEdit, EditReminderFields editReminderFields) {
assert reminderToEdit != null;

// EditReminder does not allow editing of personName
String personName = reminderToEdit.getPersonName();
LocalDateTime updatedDateTime = editReminderFields.getDateTime().orElse(reminderToEdit.getDateTime());
ReminderDescription updatedDescription = editReminderFields.getDescription()
.orElse(reminderToEdit.getDescription());

Check warning on line 107 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L104-L107

Added lines #L104 - L107 were not covered by tests

return new Reminder(personName, updatedDateTime, updatedDescription);

Check warning on line 109 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L109

Added line #L109 was not covered by tests
}

@Override
public boolean equals(Object other) {
Expand All @@ -35,14 +121,66 @@
}

/**
* Executes the command to edit a reminder to the model.
*
* @param model The {@code Model} which the command should operate on.
* @return A {@code CommandResult} indicating the outcome of the command execution.
* @throws CommandException If there is an issue during command execution.
* Stores the details to edit the reminder with. Each non-empty field value will replace the
* corresponding field value of the reminder.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
return null;
public static class EditReminderFields {
private LocalDateTime dateTime;
private ReminderDescription description;

/**
* Default constructor for {@code EditReminderFields}.
* Initializes an instance with no specified fields.
*/
public EditReminderFields() {}

Check warning on line 135 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L135

Added line #L135 was not covered by tests

/**
* Copy constructor for {@code EditReminderFields}.
* Creates a new instance by copying the fields from the specified {@code EditReminderFields} instance.
*
* @param toCopy The {@code EditReminderFields} instance to copy from. Must not be {@code null}.
*/
public EditReminderFields(EditReminderFields toCopy) {
setDateTime(toCopy.dateTime);
setDescription(toCopy.description);
}

Check warning on line 146 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L143-L146

Added lines #L143 - L146 were not covered by tests

/**
* @return true if any field is edited
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(dateTime, description);

Check warning on line 152 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L152

Added line #L152 was not covered by tests
}

public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

Check warning on line 157 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L156-L157

Added lines #L156 - L157 were not covered by tests

public Optional<LocalDateTime> getDateTime() {
return Optional.ofNullable(dateTime);

Check warning on line 160 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L160

Added line #L160 was not covered by tests
}
public void setDescription(ReminderDescription description) {
this.description = description;
}

Check warning on line 164 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L163-L164

Added lines #L163 - L164 were not covered by tests

public Optional<ReminderDescription> getDescription() {
return Optional.ofNullable(description);

Check warning on line 167 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L167

Added line #L167 was not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this || other instanceof EditReminderFields) {
return true;

Check warning on line 173 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L173

Added line #L173 was not covered by tests
}

EditReminderFields otherEditReminderFields = (EditReminderFields) other;

Check warning on line 176 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L176

Added line #L176 was not covered by tests
return Objects.equals(dateTime, otherEditReminderFields.dateTime)
&& Objects.equals(description, otherEditReminderFields.description);
}

@Override
public String toString() {
return "EditReminderFields";

Check warning on line 183 in src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java#L183

Added line #L183 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package seedu.address.logic.parser.reminder;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.reminder.EditReminderCommand;
import seedu.address.logic.commands.reminder.EditReminderCommand.EditReminderFields;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses user input to create a new {@code EditReminderCommand}.
Expand All @@ -11,7 +22,36 @@
}

@Override
public EditReminderCommand parse(String userInput) {
return null;
public EditReminderCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_DATE_TIME, PREFIX_DESCRIPTION);

Check warning on line 28 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L26-L28

Added lines #L26 - L28 were not covered by tests

Index index;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,

Check warning on line 35 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L33-L35

Added lines #L33 - L35 were not covered by tests
EditReminderCommand.MESSAGE_USAGE), pe);
}

Check warning on line 37 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L37

Added line #L37 was not covered by tests

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_DATE_TIME, PREFIX_DESCRIPTION);

Check warning on line 39 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L39

Added line #L39 was not covered by tests

EditReminderFields editReminderFields = new EditReminderFields();

Check warning on line 41 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L41

Added line #L41 was not covered by tests

if (argMultimap.getValue(PREFIX_DATE_TIME).isPresent()) {
editReminderFields.setDateTime(ParserUtil.parseDateTime(argMultimap.getValue(PREFIX_DATE_TIME).get()));

Check warning on line 44 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L44

Added line #L44 was not covered by tests
}
if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
editReminderFields.setDescription(ParserUtil.parseReminderDescription(
argMultimap.getValue(PREFIX_DESCRIPTION).get()));

Check warning on line 48 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L47-L48

Added lines #L47 - L48 were not covered by tests
}

if (!editReminderFields.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);

Check warning on line 52 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L52

Added line #L52 was not covered by tests
}

return new EditReminderCommand(index, editReminderFields);

Check warning on line 55 in src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java#L55

Added line #L55 was not covered by tests
}
}