Skip to content

Commit

Permalink
Merge pull request #93 from cshao02/shaoheng/update-edit-telegram
Browse files Browse the repository at this point in the history
Update edit telegram username
  • Loading branch information
brianchoon authored Oct 17, 2024
2 parents 9ece7ff + e0bf42a commit 8a666b7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public static String format(Person person) {
}

private static void appendTelegramUsernameToMsg(Person person, StringBuilder builder) {
builder.append("; Telegram username: ");
if (person.getTelegramUsername().toString() != null) {
builder.append("; Telegram username: ").append(person.getTelegramUsername());
builder.append(person.getTelegramUsername());
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
TelegramUsername updatedTeleUsername = editPersonDescriptor.getTelegramUsername()
.orElse(personToEdit.getTelegramUsername());

//Role[] updatedRoles = (editPersonDescriptor.getRoles().orElse(personToEdit.getRoles())).toArray(new Role[0]);


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

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public AddCommand parse(String args) throws ParseException {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
TelegramUsername telegramUsername = ParserUtil
.parseTele(argMultimap.getValue(PREFIX_TELEGRAM).orElse(null)); // null to represent input absent
.parseTeleOnAdd(argMultimap.getValue(PREFIX_TELEGRAM).orElse(null)); // null to represent input absent

//Set <Role> roleList = ParserUtil.parseRoles(argMultimap.getAllValues(PREFIX_ROLE));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public EditCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_TELEGRAM, PREFIX_ROLE);


Index index;

try {
Expand All @@ -66,11 +65,10 @@ public EditCommand parse(String args) throws ParseException {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
if (argMultimap.getValue(PREFIX_TELEGRAM).isPresent()) {
editPersonDescriptor.setTelegramUsername(ParserUtil.parseTele(argMultimap.getValue(PREFIX_TELEGRAM)
.orElse(null)));
editPersonDescriptor.setTelegramUsername(ParserUtil.parseTeleOnEdit(argMultimap.getValue(PREFIX_TELEGRAM)
.get()));
}


parseRolesForEdit(argMultimap.getAllValues(PREFIX_ROLE)).ifPresent(editPersonDescriptor::setRoles);
if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public static Address parseAddress(String address) throws ParseException {
}

/**
* Parses the given {@code String telegramUsername} and returns a {@code TelegramUsername} object.
* Parses the given {@code String telegramUsername} on add command and returns a {@code TelegramUsername} object.
* Leading and trailing spaces will be trimmed.
*
* @param telegramUsername The username string to be parsed. Can be {@code null}.
* @return A {@code TelegramUsername} object with the trimmed username.
* @throws ParseException if the provided {@code telegramUsername} is invalid according to the
* {@code TelegramUsername#isValidUsername(String)} method.
*/
public static TelegramUsername parseTele(String telegramUsername) throws ParseException {
public static TelegramUsername parseTeleOnAdd(String telegramUsername) throws ParseException {
if (telegramUsername == null) {
return new TelegramUsername(telegramUsername);
}
Expand All @@ -103,6 +103,27 @@ public static TelegramUsername parseTele(String telegramUsername) throws ParseEx
return new TelegramUsername(trimmedTelegramUsername);
}

/**
* Parses the given {@code String telegramUsername} on edit command and returns a {@code TelegramUsername} object.
* Leading and trailing spaces will be trimmed. Difference between this parse and the one for add command is that
* this parse method allows removal of telegram username when user input empty input.
*
* @param telegramUsername The username string to be parsed
* @return A {@code TelegramUsername} object with the trimmed username.
* @throws ParseException if the provided {@code telegramUsername} is invalid according to the
* {@code TelegramUsername#isValidUsername(String)} method.
*/
public static TelegramUsername parseTeleOnEdit(String telegramUsername) throws ParseException {
if (telegramUsername.isEmpty()) {
return new TelegramUsername(null);
}
String trimmedTelegramUsername = telegramUsername.trim();
if (!TelegramUsername.isValidUsername(trimmedTelegramUsername)) {
throw new ParseException(TelegramUsername.MESSAGE_CONSTRAINTS);
}
return new TelegramUsername(trimmedTelegramUsername);
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.TelegramUsername;
import seedu.address.model.role.Role;
import seedu.address.model.role.RoleHandler;

Expand All @@ -26,12 +27,14 @@ public class ParserUtilTest {
private static final String INVALID_PHONE = "+651234";
private static final String INVALID_ADDRESS = " ";
private static final String INVALID_EMAIL = "example.com";
private static final String INVALID_TELE = "a";
private static final String INVALID_ROLE = "owner";

private static final String VALID_NAME = "Rachel Walker";
private static final String VALID_PHONE = "123456";
private static final String VALID_ADDRESS = "123 Main Street #0505";
private static final String VALID_EMAIL = "[email protected]";
private static final String VALID_TELE = "rachel";

private static final String VALID_ROLE_1 = "vendor";
private static final String VALID_ROLE_2 = "sponsor";
Expand Down Expand Up @@ -150,6 +153,54 @@ public void parseEmail_validValueWithWhitespace_returnsTrimmedEmail() throws Exc
assertEquals(expectedEmail, ParserUtil.parseEmail(emailWithWhitespace));
}

@Test
public void parseTeleOnAdd_nullTelegramUsername() throws ParseException {
TelegramUsername expectedTelegramUsername = new TelegramUsername(null);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnAdd(null));
}

@Test
public void parseTeleOnAdd_invalidTelegramUsername_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseTeleOnAdd(INVALID_TELE));
}

@Test
public void parseTeleOnAdd_validValueWithWhitespace_returnsTrimmedTele() throws ParseException {
String teleWithWhitespace = WHITESPACE + VALID_TELE + WHITESPACE;
TelegramUsername expectedTelegramUsername = new TelegramUsername(VALID_TELE);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnAdd(teleWithWhitespace));
}

@Test
public void parseTeleOnAdd_validValueWithoutWhitespace_returnsTele() throws Exception {
TelegramUsername expectedTelegramUsername = new TelegramUsername(VALID_TELE);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnAdd(VALID_TELE));
}

@Test
public void parseTeleOnEdit_emptyTelegramUsername_removesTele() throws ParseException {
TelegramUsername expectedTelegramUsername = new TelegramUsername(null);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnEdit(""));
}

@Test
public void parseTeleOnEdit_invalidTelegramUsername_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseTeleOnEdit(INVALID_TELE));
}

@Test
public void parseTeleOnEdit_validValueWithWhitespace_returnsTrimmedTele() throws ParseException {
String teleWithWhitespace = WHITESPACE + VALID_TELE + WHITESPACE;
TelegramUsername expectedTelegramUsername = new TelegramUsername(VALID_TELE);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnEdit(teleWithWhitespace));
}

@Test
public void parseTeleOnEdit_validValueWithoutWhitespace_returnsTele() throws Exception {
TelegramUsername expectedTelegramUsername = new TelegramUsername(VALID_TELE);
assertEquals(expectedTelegramUsername, ParserUtil.parseTeleOnEdit(VALID_TELE));
}

@Test
public void parseRole_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseRole((String) null));
Expand Down

0 comments on commit 8a666b7

Please sign in to comment.