Skip to content

Commit

Permalink
Merge pull request #131 from shardhrv/store-uuid-in-eventmanager
Browse files Browse the repository at this point in the history
Update editCommand and deleteCommand logic
  • Loading branch information
cshao02 authored Nov 3, 2024
2 parents 7e01f32 + 58de4b4 commit 3953283
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public CommandResult execute(Model model, EventManager eventManager) throws Comm

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
eventManager.removeAllPersonsInEvents(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
Expand Down Expand Up @@ -98,8 +99,11 @@ public CommandResult execute(Model model, EventManager eventManager) throws Comm
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

// edit for model
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
// edit person for eventManager
eventManager.editAllPersonsInEvents(personToEdit, editedPerson);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

Expand All @@ -118,9 +122,10 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
.orElse(personToEdit.getTelegramUsername());

Set<Role> updatedRoles = editPersonDescriptor.getRoles().orElse(personToEdit.getRoles());
UUID uuid = personToEdit.getUniqueId();

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress,
updatedTeleUsername, updatedRoles);
updatedTeleUsername, updatedRoles, uuid);

}

Expand Down
56 changes: 55 additions & 1 deletion src/main/java/seedu/address/model/event/Event.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model.event;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -203,7 +204,7 @@ public static boolean isValidEvent(String event) {
}

/**
* Removes a person from the event with the specified role.
* Removes a person from the event with the specified role that is known.
* @param person Person to be removed.
* @param role Role of the person.
* @throws IllegalArgumentException If the person does not have the specified role.
Expand Down Expand Up @@ -235,6 +236,59 @@ public void removePerson(Person person, String role) {
roleSet.remove(person);

}

/**
* Removes person from all roles if they are present in that role
* @param personToRemove Person that will be removed
*/
public void removePerson(Person personToRemove) {
requireNonNull(personToRemove);
// check each set and see it contains the person to remove
if (attendees.contains(personToRemove)) {
attendees.remove(personToRemove);
}

if (vendors.contains(personToRemove)) {
vendors.remove(personToRemove);
}

if (sponsors.contains(personToRemove)) {
sponsors.remove(personToRemove);
}

if (volunteers.contains(personToRemove)) {
volunteers.remove(personToRemove);
}
}

/**
* Edits person in all roles if they are present in that role
* @param personToEdit Person that will be removed
* @param editedPerson Person that will replace personToEdit
*/
public void editPerson(Person personToEdit, Person editedPerson) {
requireAllNonNull(personToEdit, editedPerson);
// check each set and see it contains the person to edit
if (attendees.contains(personToEdit)) {
attendees.remove(personToEdit);
attendees.add(editedPerson);
}

if (vendors.contains(personToEdit)) {
vendors.remove(personToEdit);
vendors.add(editedPerson);
}

if (sponsors.contains(personToEdit)) {
sponsors.remove(personToEdit);
sponsors.add(editedPerson);
}

if (volunteers.contains(personToEdit)) {
volunteers.remove(personToEdit);
volunteers.add(editedPerson);
}
}
@Override
public boolean equals(Object other) { // equality of 2 events defined only by the event name
return other == this // short circuit if same object
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/model/event/EventManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package seedu.address.model.event;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.List;

import javafx.collections.ObservableList;
import seedu.address.model.person.Person;

/**
* Wraps all {@code Event} and abstracts away
Expand Down Expand Up @@ -94,6 +96,26 @@ public void resetData(ReadOnlyEventManager newData) {

setEvents(newData.getEventList());
}

/**
* Edits all the people that match personToEdit in all events
* @param personToEdit person that will be edited
* @param editedPerson new person that will replace personToEdit
*/
public void editAllPersonsInEvents(Person personToEdit, Person editedPerson) {
requireAllNonNull(personToEdit, editedPerson);
eventList.forEach(event -> event.editPerson(personToEdit, editedPerson));
}

/**
* Edits all the people that match personToEdit in all events
* @param personToRemove person that will be edited
*/
public void removeAllPersonsInEvents(Person personToRemove) {
requireNonNull(personToRemove);
eventList.forEach(event -> event.removePerson(personToRemove));
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/seedu/address/model/event/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,62 @@ public void getRole_validRole_true() {
} catch (Exception e) {
assert false;
}
}

@Test
public void removePerson_fromMultipleRoles_success() {
Event event = new Event("Event1");
Person person = new PersonBuilder().withRoles("attendee", "vendor", "sponsor", "volunteer").build();

try {
// Adding person to all roles
event.addPerson(person, "attendee");
event.addPerson(person, "vendor");
event.addPerson(person, "sponsor");
event.addPerson(person, "volunteer");

// Removing person from all roles
event.removePerson(person);

// Assert that person has been removed from all roles
assertFalse(event.getAttendees().contains(person));
assertFalse(event.getVendors().contains(person));
assertFalse(event.getSponsors().contains(person));
assertFalse(event.getVolunteers().contains(person));
} catch (Exception e) {
assert false;
}
}

@Test
public void editPerson_inMultipleRoles_success() {
Event event = new Event("Event1");
Person personToEdit = new PersonBuilder().withRoles("attendee", "vendor", "sponsor", "volunteer").build();
Person editedPerson = new PersonBuilder().withRoles("attendee", "vendor", "sponsor", "volunteer")
.withName("Edited Name").build();

try {
// Adding person to all roles
event.addPerson(personToEdit, "attendee");
event.addPerson(personToEdit, "vendor");
event.addPerson(personToEdit, "sponsor");
event.addPerson(personToEdit, "volunteer");

// Editing person
event.editPerson(personToEdit, editedPerson);

// Assert that editedPerson is present and personToEdit is removed
assertTrue(event.getAttendees().contains(editedPerson));
assertTrue(event.getVendors().contains(editedPerson));
assertTrue(event.getSponsors().contains(editedPerson));
assertTrue(event.getVolunteers().contains(editedPerson));

assertFalse(event.getAttendees().contains(personToEdit));
assertFalse(event.getVendors().contains(personToEdit));
assertFalse(event.getSponsors().contains(personToEdit));
assertFalse(event.getVolunteers().contains(personToEdit));
} catch (Exception e) {
assert false;
}
}
}

0 comments on commit 3953283

Please sign in to comment.