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

Team Fix #171

Merged
merged 4 commits into from
Oct 31, 2022
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
Expand Up @@ -15,7 +15,9 @@
import picocli.CommandLine;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.team.Description;
import seedu.address.model.team.Team;
import seedu.address.model.team.TeamName;

/**
* Adds a new team to the address book.
Expand All @@ -41,11 +43,11 @@ public class AddTeamCommand extends Command {
public static final String MESSAGE_TEAM_EXISTS = "There is already an existing team with the same name!";

@CommandLine.Parameters(arity = "1", description = FLAG_TEAM_NAME_DESCRIPTION)
private String teamName;
private TeamName teamName;

@CommandLine.Option(names = {FLAG_DESCRIPTION_STR, FLAG_DESCRIPTION_LONG}, defaultValue =
Team.DEFAULT_DESCRIPTION, description = FLAG_TEAM_DESCRIPTION_DESCRIPTION)
private String description;
Description.NO_DESCRIPTION_STRING, description = FLAG_TEAM_DESCRIPTION_DESCRIPTION)
private Description description;

@CommandLine.Option(names = {FLAG_HELP_STR, FLAG_HELP_STR_LONG}, usageHelp = true,
description = FLAG_HELP_DESCRIPTION)
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/seedu/address/logic/commands/DeleteTeamCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import static seedu.address.logic.parser.CliSyntax.FLAG_TEAM_NAME_DESCRIPTION;

import java.util.List;
import java.util.stream.Collectors;

import picocli.CommandLine;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.team.Team;
import seedu.address.model.team.TeamName;

/**
* Deletes a team from the addressbook.
Expand All @@ -30,7 +32,7 @@ public class DeleteTeamCommand extends Command {
public static final String MESSAGE_TEAM_NOT_EXISTS = "This team you are trying to delete does not exist!";

@CommandLine.Parameters(arity = "1", description = FLAG_TEAM_NAME_DESCRIPTION)
private String targetTeamName;
private TeamName targetTeamName;

@CommandLine.Option(names = {FLAG_HELP_STR, FLAG_HELP_STR_LONG}, usageHelp = true,
description = FLAG_HELP_DESCRIPTION)
Expand All @@ -52,22 +54,24 @@ public CommandResult execute(Model model) throws CommandException {
Team currentTeam = model.getTeam();
Team targetTeam = new Team(targetTeamName);

int teamIndex = teamList.indexOf(targetTeam);
if (teamIndex == -1) {
List<Team> filteredListWithTargetTeam = teamList.stream()
.filter(targetTeam::isSameTeam).collect(Collectors.toList());

if (filteredListWithTargetTeam.size() == 0) {
throw new CommandException(MESSAGE_TEAM_NOT_EXISTS);
}

if (teamList.size() == 1) {
throw new CommandException(MESSAGE_AT_LEAST_ONE_TEAM);
}
assert filteredListWithTargetTeam.size() == 1;
Team targetTeamInTeamList = filteredListWithTargetTeam.get(0);

Team deletedTeam = teamList.get(teamIndex);

model.deleteTeam(deletedTeam);
model.deleteTeam(targetTeamInTeamList);
if (currentTeam.equals(targetTeam)) {
model.setTeam(model.getTeamList().get(0));
}
return new CommandResult(String.format(MESSAGE_DELETE_TEAM_SUCCESS, deletedTeam));
return new CommandResult(String.format(MESSAGE_DELETE_TEAM_SUCCESS, targetTeamInTeamList));
}

@Override
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/seedu/address/logic/commands/EditTeamCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.team.Description;
import seedu.address.model.team.Link;
import seedu.address.model.team.Task;
import seedu.address.model.team.Team;
import seedu.address.model.team.TeamName;

/**
* Edits the currently set team.
Expand Down Expand Up @@ -72,8 +74,8 @@ public EditTeamCommand() {
private static Team createEditedTeam(Team teamToEdit, EditTeamDescriptor editTeamDescriptor) {
assert editTeamDescriptor != null;

String updatedName = editTeamDescriptor.getName().orElse(teamToEdit.getTeamName());
String updatedDescription = editTeamDescriptor.getDescription().orElse(teamToEdit.getDescription());
TeamName updatedName = editTeamDescriptor.getName().orElse(teamToEdit.getTeamName());
Description updatedDescription = editTeamDescriptor.getDescription().orElse(teamToEdit.getDescription());
List<Person> members = teamToEdit.getTeamMembers();
List<Task> tasks = teamToEdit.getTaskList();
List<Link> links = teamToEdit.getLinkList();
Expand Down Expand Up @@ -131,21 +133,21 @@ public boolean equals(Object other) {

private static class Arguments {
@CommandLine.Option(names = {FLAG_NAME_STR, FLAG_NAME_STR_LONG}, description = FLAG_TEAM_NAME_DESCRIPTION)
private String name;
private TeamName name;

@CommandLine.Option(names = {FLAG_DESCRIPTION_STR, FLAG_DESCRIPTION_LONG},
description = FLAG_TEAM_DESCRIPTION_DESCRIPTION)
private String description;
private Description description;
}

/**
* Stores the details to edit the team with. Each non-empty field value will replace the
* corresponding field value of the team.
*/
public static class EditTeamDescriptor {
private String name;
private TeamName name;

private String description;
private Description description;

public EditTeamDescriptor() {
}
Expand All @@ -165,19 +167,19 @@ public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, description);
}

public Optional<String> getName() {
public Optional<TeamName> getName() {
return Optional.ofNullable(name);
}

public void setName(String name) {
public void setName(TeamName name) {
this.name = name;
}

public Optional<String> getDescription() {
public Optional<Description> getDescription() {
return Optional.ofNullable(description);
}

public void setDescription(String description) {
public void setDescription(Description description) {
this.description = description;
}

Expand Down
19 changes: 12 additions & 7 deletions src/main/java/seedu/address/logic/commands/SetTeamCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS;

import java.util.List;
import java.util.stream.Collectors;

import picocli.CommandLine;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.team.Team;
import seedu.address.model.team.TeamName;

/**
* Sets the current team to an existing team.
Expand All @@ -34,7 +36,7 @@ public class SetTeamCommand extends Command {
public static final String MESSAGE_TEAM_NOT_EXISTS = "This team you are trying to set does not exist!";

@CommandLine.Parameters(arity = "1", description = FLAG_TEAM_NAME_DESCRIPTION)
private String targetTeamName;
private TeamName targetTeamName;

@CommandLine.Option(names = {FLAG_HELP_STR, FLAG_HELP_STR_LONG}, usageHelp = true,
description = FLAG_HELP_DESCRIPTION)
Expand All @@ -55,20 +57,23 @@ public CommandResult execute(Model model) throws CommandException {
List<Team> teamList = model.getTeamList();
Team currentTeam = model.getTeam();
Team targetTeam = new Team(targetTeamName);
int teamIndex = teamList.indexOf(targetTeam);
List<Team> filteredListWithTargetTeam = teamList.stream()
.filter(targetTeam::isSameTeam).collect(Collectors.toList());

if (teamIndex == -1) {
if (filteredListWithTargetTeam.size() == 0) {
throw new CommandException(MESSAGE_TEAM_NOT_EXISTS);
}
assert filteredListWithTargetTeam.size() == 1;
Team targetTeamInTeamList = filteredListWithTargetTeam.get(0);

if (currentTeam.equals(targetTeam)) {
if (currentTeam.equals(targetTeamInTeamList)) {
throw new CommandException(MESSAGE_TEAM_ALREADY_SET);
}
Team existingTeam = teamList.get(teamIndex);
model.setTeam(existingTeam);

model.setTeam(targetTeamInTeamList);
model.updateFilteredMembersList(PREDICATE_SHOW_ALL_PERSONS);
model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS);
return new CommandResult(String.format(MESSAGE_SET_TEAM_SUCCESS, targetTeam));
return new CommandResult(String.format(MESSAGE_SET_TEAM_SUCCESS, targetTeamInTeamList));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.team.Description;
import seedu.address.model.team.Task;
import seedu.address.model.team.TeamName;
import seedu.address.model.team.Url;

/**
Expand All @@ -29,6 +31,8 @@ public class AddressBookParser {
.registerConverter(Index.class, new IndexConverter())
.registerConverter(Url.class, new UrlConverter())
.registerConverter(Task.class, new TaskConverter())
.registerConverter(TeamName.class, new TeamNameConverter())
.registerConverter(Description.class, new DescriptionConverter())
.registerConverter(Order.class, new OrderConverter());
/**
* Parses user input into command for execution.
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/logic/parser/DescriptionConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.address.logic.parser;

import picocli.CommandLine;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.team.Description;

/**
* Converter from {@code String} to {@code Description}
*/
public class DescriptionConverter implements CommandLine.ITypeConverter<Description> {
@Override
public Description convert(String value) throws Exception {
try {
return ParserUtil.parseDescription(value);
} catch (ParseException e) {
throw new CommandLine.TypeConversionException(e.getMessage());
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.team.Description;
import seedu.address.model.team.TeamName;
import seedu.address.model.team.Url;

/**
Expand Down Expand Up @@ -160,6 +162,36 @@ public static LocalDateTime parseLocalDateTime(String datetime) throws ParseExce
}
}

/**
* Parses a {@code String name} into a {@code TeamName}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code teamName} is invalid.
*/
public static TeamName parseTeamName(String teamName) throws ParseException {
requireNonNull(teamName);
String trimmedName = teamName.trim();
if (!TeamName.isValidTeamName(trimmedName)) {
throw new ParseException(TeamName.MESSAGE_CONSTRAINTS);
}
return new TeamName(trimmedName);
}

/**
* Parses a {@code String name} into a {@code Description}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code description} is invalid.
*/
public static Description parseDescription(String description) throws ParseException {
requireNonNull(description);
String trimmedName = description.trim();
if (!Description.isValidTeamDescription(trimmedName)) {
throw new ParseException(Description.MESSAGE_CONSTRAINTS);
}
return new Description(trimmedName);
}

/**
* Parses {@code String order} into a {@code Order}
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/logic/parser/TeamNameConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.address.logic.parser;

import picocli.CommandLine;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.team.TeamName;

/**
* Converter from {@code String} to {@code TeamName}
*/
public class TeamNameConverter implements CommandLine.ITypeConverter<TeamName> {
@Override
public TeamName convert(String value) throws Exception {
try {
return ParserUtil.parseTeamName(value);
} catch (ParseException e) {
throw new CommandLine.TypeConversionException(e.getMessage());
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/seedu/address/model/team/Description.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.address.model.team;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Team's description in the addressbook
* Guarantees: immutable; is valid as declared in {@link #isValidTeamDescription(String)}
*/
public class Description {

public static final String MESSAGE_CONSTRAINTS =
"Team description should only contain alphanumeric characters and spaces, and it should not be blank";

public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";
public static final String NO_DESCRIPTION_STRING = "No description added";
public static final Description NO_DESCRIPTION = new Description(NO_DESCRIPTION_STRING);
public static final Description DEFAULT_DESCRIPTION = new Description("A default team created just for you");
public final String description;

/**
* Constructs a {@code Description}
*
* @param description A valid description.
*/
public Description(String description) {
requireNonNull(description);
checkArgument(isValidTeamDescription(description), MESSAGE_CONSTRAINTS);
this.description = description;
}

/**
* Returns true if a given string is a valid description.
*/
public static boolean isValidTeamDescription(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return description;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Description // instanceof handles nulls
&& description.equals(((Description) other).description)); // state check
}

@Override
public int hashCode() {
return description.hashCode();
}

}
Loading