Skip to content

Commit

Permalink
Merge 4ce7728 into bcb1d73
Browse files Browse the repository at this point in the history
  • Loading branch information
ChooJeremy committed Nov 1, 2018
2 parents bcb1d73 + 4ce7728 commit a1bc487
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AutoCompleteCommandHelper {
PasswordCommand.COMMAND_WORD,
RedoCommand.COMMAND_WORD,
SelectCommand.COMMAND_WORD,
SelfEditCommand.COMMAND_WORD,
UndoCommand.COMMAND_WORD,
AddAssignmentCommand.COMMAND_WORD,
ModifyPermissionCommand.COMMAND_WORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public CommandResult runBody(Model model, CommandHistory history) throws Command
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
public static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/seedu/address/logic/commands/SelfEditCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.Permission;
import seedu.address.model.person.Person;


/**
* Edits the details of an existing person in the address book.
* This command makes use of EditCommand's classes to perform it's work.
*/
public class SelfEditCommand extends Command {

public static final String COMMAND_WORD = "myself";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of yourself, the currently logged "
+ "in person. Existing values will be overwritten by the input values.\n"
+ "Parameters: "
+ "[" + PREFIX_PHONE + " PHONE] "
+ "[" + PREFIX_EMAIL + " EMAIL] "
+ "[" + PREFIX_ADDRESS + " ADDRESS] "
+ "[" + PREFIX_PROJECT + " PROJECT]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + " 91234567 "
+ PREFIX_EMAIL + " johndoe@example.com";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String ADMIN_EDIT_ERROR = "You're currently logged in as admin, not as an employee!\nAdmin"
+ " cannot be edit. Perhaps you meant edit instead?";

private EditCommand.EditPersonDescriptor editPersonDescriptor;

/**
* @param editPersonDescriptor details to edit the person with
*/
public SelfEditCommand(EditCommand.EditPersonDescriptor editPersonDescriptor) {
requireNonNull(editPersonDescriptor);

requiredPermission.addPermissions(Permission.EDIT_EMPLOYEE);
this.editPersonDescriptor = new EditCommand.EditPersonDescriptor(editPersonDescriptor);
}

@Override
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (model.getLoggedInUser().isAdminUser()) {
throw new CommandException(ADMIN_EDIT_ERROR);
}

Person personToEdit = model.getLoggedInUser().getPerson();
Person editedPerson = EditCommand.createEditedPerson(personToEdit, editPersonDescriptor);

model.updatePerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson));
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof SelfEditCommand)) {
return false;
}

// state check
SelfEditCommand e = (SelfEditCommand) other;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import seedu.address.logic.commands.PasswordCommand;
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.SelfEditCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.commands.ViewPermissionCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -124,6 +125,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ViewPermissionCommand.COMMAND_WORD:
return new ViewPermissionCommandParser().parse(arguments);

case SelfEditCommand.COMMAND_WORD:
return new SelfEditCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;

import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.SelfEditCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.project.Project;

/**
* Parses input arguments and creates a new EditCommand object
*/
public class SelfEditCommandParser implements Parser<SelfEditCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* and returns an EditCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public SelfEditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_PROJECT);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
parseProjectsForEdit(argMultimap.getAllValues(PREFIX_PROJECT)).ifPresent(editPersonDescriptor::setProjects);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(SelfEditCommand.MESSAGE_NOT_EDITED);
}

return new SelfEditCommand(editPersonDescriptor);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<Set<Project>> parseProjectsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseProjects(tagSet));
}

}

0 comments on commit a1bc487

Please sign in to comment.