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

Fix Edit and EditUser #285

Merged
merged 2 commits into from
Nov 11, 2023
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
Binary file modified bin/main/seedu/address/logic/commands/edit/EditCommand.class
Binary file not shown.
Binary file modified bin/main/seedu/address/logic/commands/edit/EditUserCommand.class
Binary file not shown.
Binary file modified bin/main/seedu/address/logic/parser/AddCommandParser.class
Binary file not shown.
Binary file modified bin/main/seedu/address/model/ModelManager.class
Binary file not shown.
Binary file modified bin/main/seedu/address/model/person/timetable/Schedule.class
Binary file not shown.
Binary file modified bin/main/seedu/address/model/user/UserData.class
Binary file not shown.
Binary file modified bin/test/seedu/address/logic/commands/edit/EditCommandTest.class
Binary file not shown.
Binary file not shown.
Binary file modified bin/test/seedu/address/testutil/EditPersonDescriptorBuilder.class
Binary file not shown.
Binary file modified bin/test/seedu/address/testutil/EditUserDescriptorBuilder.class
Binary file not shown.
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/commands/edit/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class EditCommand extends Command {
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 MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
public static final String MESSAGE_DUPLICATE_PHONE = "This phone number already exists in the address book.";
public static final String MESSAGE_DUPLICATE_EMAIL = "This email already exists in the address book.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand Down Expand Up @@ -84,6 +86,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

if (!personToEdit.isSamePhone(editedPerson) && model.hasPhone(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PHONE);
}

if (!personToEdit.isSameEmail(editedPerson) && model.hasEmail(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_EMAIL);
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class EditUserCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";

public static final String MESSAGE_DUPLICATE_USER = "No changes to user.";
public static final String MESSAGE_DUPLICATE_PHONE = "This phone number already exists in the address book.";
public static final String MESSAGE_DUPLICATE_EMAIL = "This email already exists in the address book.";

private final EditUserDescriptor editUserDescriptor;

Expand All @@ -75,6 +77,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_USER);
}

if (!userToEdit.isSamePhone(editedUser) && model.hasPhone(editedUser)) {
throw new CommandException(MESSAGE_DUPLICATE_PHONE);
}

if (!userToEdit.isSameEmail(editedUser) && model.hasEmail(editedUser)) {
throw new CommandException(MESSAGE_DUPLICATE_EMAIL);
}

model.setUser(editedUser);
return new CommandResult(String.format(MESSAGE_EDIT_USER_SUCCESS,
Messages.format(editedUser)), false, false, true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public AddCommand parse(String args) throws ParseException {
PREFIX_BIRTHDAY, PREFIX_TAG);

// Check if all prefixes are present
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_BIRTHDAY)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME , PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_BIRTHDAY)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
Expand All @@ -49,7 +49,6 @@ public AddCommand parse(String args) throws ParseException {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_BIRTHDAY);

// Check if all present prefixes are valid, if not present put empty string.
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ public ReadOnlyAddressBook getAddressBook() {
@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return addressBook.hasPerson(person);
return addressBook.hasPerson(person) || userData.sameAsUser(person);
}

@Override
public boolean hasPhone(Person person) {
requireNonNull(person);
return addressBook.hasPhone(person);
return addressBook.hasPhone(person) || userData.sameAsUserPhone(person);
}

@Override
public boolean hasEmail(Person person) {
requireNonNull(person);
return addressBook.hasEmail(person);
return addressBook.hasEmail(person) || userData.sameAsUserEmail(person);
}

@Override
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/seedu/address/model/user/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.person.Person;
import seedu.address.model.person.timetable.DatedEvent;

/**
Expand All @@ -29,10 +30,6 @@ public UserData(ReadOnlyUserData userData) {
this(userData.getUser());
}

public void setUser(User user) {
this.user = user;
}

public void setDatedEvents(ArrayList<DatedEvent> datedEvents) {
this.user.setDatedEvents(datedEvents);
}
Expand All @@ -47,10 +44,26 @@ public void resetData(ReadOnlyUserData newData) {
this.setDatedEvents(newData.getDatedEvents());
}

public boolean sameAsUserPhone(Person person) {
return user.isSamePhone(person);
}

public boolean sameAsUserEmail(Person person) {
return user.isSameEmail(person);
}

public boolean sameAsUser(Person person) {
return user.isSamePerson(person);
}

public ObservableList<User> getUserView() {
return internalUnmodifiableList;
}

public void setUser(User user) {
this.user = user;
}

@Override
public User getUser() {
return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.person.Email;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.user.UserData;
import seedu.address.model.user.UserPrefs;
import seedu.address.testutil.EditPersonDescriptorBuilder;
Expand Down Expand Up @@ -136,6 +138,15 @@ public void execute_duplicatePersonFilteredList_failure() {
assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}

@Test
public void execute_sameAsUser_failure() {
Person user = model.getUser();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(user).build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}

@Test
public void execute_invalidPersonIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
Expand All @@ -162,6 +173,76 @@ public void execute_invalidPersonIndexFilteredList_failure() {
assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void execute_duplicatePhoneUnfilteredList_failure() {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Phone duplicatePhone = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased()).getPhone();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).withPhone(duplicatePhone)
.build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PHONE);
}

@Test
public void execute_duplicatePhoneFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
Phone duplicatePhone = model.getAddressBook().getPersonList()
.get(INDEX_SECOND_PERSON.getZeroBased()).getPhone();
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).withPhone(duplicatePhone)
.build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PHONE);
}

@Test
public void execute_samePhoneAsUser_failure() {
Phone userPhone = model.getUser().getPhone();
Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(personToEdit)
.withPhone(userPhone).build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PHONE);
}

@Test
public void execute_duplicateEmailUnfilteredList_failure() {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Email duplicateEmail = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased()).getEmail();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).withEmail(duplicateEmail)
.build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_EMAIL);
}

@Test
public void execute_duplicateEmailFilteredList_failure() {
Email duplicateEmail = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased()).getEmail();

showPersonAtIndex(model, INDEX_FIRST_PERSON);
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).withEmail(duplicateEmail)
.build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_EMAIL);
}

@Test
public void execute_sameEmailAsUser_failure() {
Email userEmail = model.getUser().getEmail();
Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(personToEdit)
.withEmail(userEmail).build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_EMAIL);
}

@Test
public void equals() {
final EditCommand standardCommand = new EditCommand(INDEX_FIRST_PERSON, DESC_AMY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;
Expand All @@ -18,6 +21,8 @@
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.person.Email;
import seedu.address.model.person.Phone;
import seedu.address.model.user.User;
import seedu.address.model.user.UserData;
import seedu.address.model.user.UserPrefs;
Expand Down Expand Up @@ -68,6 +73,51 @@ public void execute_duplicateUser_failure() {
assertCommandFailure(editUserCommand, model, EditUserCommand.MESSAGE_DUPLICATE_USER);
}

@Test
public void execute_duplicatePhoneUnfilteredList_failure() {
User user = model.getUser();
Phone duplicatePhone = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()).getPhone();
EditUserDescriptor descriptor = new EditUserDescriptorBuilder(user).withPhone(duplicatePhone).build();
EditUserCommand editUserCommand = new EditUserCommand(descriptor);

assertCommandFailure(editUserCommand, model, EditUserCommand.MESSAGE_DUPLICATE_PHONE);
}

@Test
public void execute_duplicatePhoneFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
User user = model.getUser();
Phone duplicatePhone = model.getAddressBook().getPersonList()
.get(INDEX_SECOND_PERSON.getZeroBased()).getPhone();
EditUserDescriptor descriptor = new EditUserDescriptorBuilder(user).withPhone(duplicatePhone).build();
EditUserCommand editUserCommand = new EditUserCommand(descriptor);

assertCommandFailure(editUserCommand, model, EditUserCommand.MESSAGE_DUPLICATE_PHONE);
}

@Test
public void execute_duplicateEmailUnfilteredList_failure() {
User user = model.getUser();
Email duplicateEmail = model.getAddressBook().getPersonList()
.get(INDEX_FIRST_PERSON.getZeroBased()).getEmail();
EditUserDescriptor descriptor = new EditUserDescriptorBuilder(user).withEmail(duplicateEmail).build();
EditUserCommand editUserCommand = new EditUserCommand(descriptor);

assertCommandFailure(editUserCommand, model, EditUserCommand.MESSAGE_DUPLICATE_EMAIL);
}

@Test
public void execute_duplicateEmailFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
User user = model.getUser();
Email duplicateEmail = model.getAddressBook().getPersonList()
.get(INDEX_SECOND_PERSON.getZeroBased()).getEmail();
EditUserDescriptor descriptor = new EditUserDescriptorBuilder(user).withEmail(duplicateEmail).build();
EditUserCommand editUserCommand = new EditUserCommand(descriptor);

assertCommandFailure(editUserCommand, model, EditUserCommand.MESSAGE_DUPLICATE_EMAIL);
}

@Test
public void equals() {
final EditUserCommand standardCommand = new EditUserCommand(DESC_USER_AMY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public EditPersonDescriptorBuilder withPhone(String phone) {
return this;
}

/**
* Sets the {@code Phone} of the {@code EditPersonDescriptor} that we are building.
*/
public EditPersonDescriptorBuilder withPhone(Phone phone) {
descriptor.setPhone(phone);
return this;
}

/**
* Sets the {@code Email} of the {@code EditPersonDescriptor} that we are building.
*/
Expand All @@ -67,6 +75,14 @@ public EditPersonDescriptorBuilder withEmail(String email) {
return this;
}

/**
* Sets the {@code Email} of the {@code EditPersonDescriptor} that we are building.
*/
public EditPersonDescriptorBuilder withEmail(Email email) {
descriptor.setEmail(email);
return this;
}

/**
* Sets the {@code Address} of the {@code EditPersonDescriptor} that we are building.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public EditUserDescriptorBuilder withPhone(String phone) {
return this;
}

/**
* Sets the {@code Phone} of the {@code EditUserDescriptorBuilder} that we are building.
*/
public EditUserDescriptorBuilder withPhone(Phone phone) {
descriptor.setPhone(phone);
return this;
}

/**
* Sets the {@code Email} of the {@code EditUserDescriptorBuilder} that we are building.
*/
Expand All @@ -70,6 +78,14 @@ public EditUserDescriptorBuilder withEmail(String email) {
return this;
}

/**
* Sets the {@code Email} of the {@code EditUserDescriptorBuilder} that we are building.
*/
public EditUserDescriptorBuilder withEmail(Email email) {
descriptor.setEmail(email);
return this;
}

/**
* Sets the {@code Address} of the {@code EditUserDescriptorBuilder} that we are building.
*/
Expand Down
Loading