Skip to content

Commit

Permalink
Merge pull request #58 from MadLamprey/branch-addLCommand
Browse files Browse the repository at this point in the history
Add LinkedIn and Github Command functionalities
  • Loading branch information
MadLamprey committed Oct 17, 2023
2 parents b1cea57 + 146ece3 commit 2557610
Show file tree
Hide file tree
Showing 27 changed files with 690 additions and 75 deletions.
51 changes: 40 additions & 11 deletions src/main/java/seedu/address/logic/commands/AddGCommand.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
package seedu.address.logic.commands;

import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Github;
import seedu.address.model.person.Person;

/**
* Adds GitHub account to candidates existing details.
*/
public class AddGCommand extends Command {
public static final String COMMAND_WORD = "addL";
public static final String COMMAND_WORD = "addG";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds LinkedIn to details of a candidate. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds Github to details of a candidate. "
+ "Parameters: " + "[" + COMMAND_WORD + " <USERID> <USERNAME>]...\n"
+ "Example: " + COMMAND_WORD + " 2 alexyeoh";

public static final String MESSAGE_SUCCESS = "LinkedIn account added for: %1$s";
public static final String MESSAGE_SUCCESS = "Github account added for: %1$s";

private final int targetIndex;
private final Index index;

private final String username;
private final Github username;

/**
* Creates an AddG Command to add Github.
* @param targetIndex
* @param index
* @param username
*/
public AddGCommand(int targetIndex, String username) {
this.targetIndex = targetIndex;
public AddGCommand(Index index, Github username) {
this.index = index;
this.username = username;
}

@Override
public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_SUCCESS, "PLACEHOLDER"));
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
personToEdit.setGithub(username);
model.setPerson(personToEdit, personToEdit);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(personToEdit));
}

/**
* Generates a command execution success message based on whether
* the username is added to or removed from
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = !username.value.isEmpty() ? MESSAGE_SUCCESS : "";
return String.format(message, personToEdit.getName());
}

@Override
Expand All @@ -47,12 +76,12 @@ public boolean equals(Object other) {
}

AddGCommand otherAddGCommand = (AddGCommand) other;
return this.targetIndex == otherAddGCommand.targetIndex && this.username.equals(otherAddGCommand.username);
return this.index.equals(otherAddGCommand.index) && this.username.equals(otherAddGCommand.username);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("targetIndex", targetIndex).add("username", username).toString();
return new ToStringBuilder(this).add("index", index).add("username", username).toString();
}

}
46 changes: 37 additions & 9 deletions src/main/java/seedu/address/logic/commands/AddLCommand.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
package seedu.address.logic.commands;

import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.LinkedIn;
import seedu.address.model.person.Person;

/**
* Adds LinkedIn account to candidate's existing details.
*/
public class AddLCommand extends Command {
public static final String COMMAND_WORD = "addL";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds LinkedIn to details of a candidate. "
+ "Parameters: " + "[" + COMMAND_WORD + " <USERID> <USERNAME>]...\n"
+ "Example: " + COMMAND_WORD + " 2 alexyeoh";

public static final String MESSAGE_SUCCESS = "LinkedIn account added for: %1$s";

private final int targetIndex;
private final Index index;

private final String username;
private final LinkedIn username;

/**
* Creates an AddL command to add LinkedIn.
* @param targetIndex
* @param index
* @param username
*/
public AddLCommand(int targetIndex, String username) {
this.targetIndex = targetIndex;
public AddLCommand(Index index, LinkedIn username) {
this.index = index;
this.username = username;
}

@Override
public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_SUCCESS, "PLACEHOLDER"));
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
personToEdit.setLinkedIn(username);
model.setPerson(personToEdit, personToEdit);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(personToEdit));
}

/**
* Generates a command execution success message based on whether
* the username is added to or removed from
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = !username.value.isEmpty() ? MESSAGE_SUCCESS : "";
return String.format(message, personToEdit.getName());
}

@Override
Expand All @@ -47,12 +75,12 @@ public boolean equals(Object other) {
}

AddLCommand otherAddLCommand = (AddLCommand) other;
return this.targetIndex == otherAddLCommand.targetIndex && this.username.equals(otherAddLCommand.username);
return this.index.equals(otherAddLCommand.index) && this.username.equals(otherAddLCommand.username);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("targetIndex", targetIndex).add("username", username).toString();
return new ToStringBuilder(this).add("index", index).add("username", username).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Remark updatedRemark = personToEdit.getRemark();
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedRemark, updatedTags);
}

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/seedu/address/logic/commands/GithubCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.address.logic.commands;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Redirects user to the Github account of a specific candidates.
*/
public class GithubCommand extends Command {

public static final String COMMAND_WORD = "github";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Redirects to user's Github account. "
+ "Parameters: " + "[" + COMMAND_WORD + " <USERID>]...\n"
+ "Example: " + COMMAND_WORD + " 2";
public static final String MESSAGE_SUCCESS = "Redirecting to Github ...";

private final Index index;

public GithubCommand(Index index) {
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
String username = personToEdit.getGithub().value;
String githubUrl = "https://github.com/" + username;
try {
Desktop.getDesktop().browse(new URI(githubUrl));
} catch (IOException | URISyntaxException e) {
System.out.println(e.getMessage());
}

return new CommandResult(MESSAGE_SUCCESS);
}
}
51 changes: 51 additions & 0 deletions src/main/java/seedu/address/logic/commands/LinkedInCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.address.logic.commands;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Redirects the user to the LinkedIn account of a specific candidate.
*/
public class LinkedInCommand extends Command {

public static final String COMMAND_WORD = "linkedin";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Redirects to user's LinkedIn account. "
+ "Parameters: " + "[" + COMMAND_WORD + " <USERID>]...\n"
+ "Example: " + COMMAND_WORD + " 2";
public static final String MESSAGE_SUCCESS = "Redirecting to LinkedIn ...";

private final Index index;

public LinkedInCommand(Index index) {
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
String username = personToEdit.getLinkedIn().value;
String linkedInUrl = "https://www.linkedin.com/in/" + username;
try {
Desktop.getDesktop().browse(new URI(linkedInUrl));
} catch (IOException | URISyntaxException e) {
System.out.println(e.getMessage());
}

return new CommandResult(MESSAGE_SUCCESS);
}
}
39 changes: 39 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddGCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddGCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Github;

/**
* Parses input arguments and creates a new AddLCommand object
*/
public class AddGCommandParser implements Parser<AddGCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddLCommand
* and returns an AddLCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddGCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultiMap = ArgumentTokenizer.tokenize(args, PREFIX_USERNAME);

Index index;

try {
index = ParserUtil.parseIndex(argMultiMap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddGCommand.MESSAGE_USAGE), pe);
}

index = ParserUtil.parseIndex(argMultiMap.getPreamble());
String username = argMultiMap.getValue(PREFIX_USERNAME).orElse("");

return new AddGCommand(index, new Github(username));
}
}
38 changes: 38 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddLCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddLCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.LinkedIn;

/**
* Parses input arguments and creates a new AddLCommand object
*/
public class AddLCommandParser implements Parser<AddLCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddLCommand
* and returns an AddLCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddLCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultiMap = ArgumentTokenizer.tokenize(args, PREFIX_USERNAME);
Index index;

try {
index = ParserUtil.parseIndex(argMultiMap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddLCommand.MESSAGE_USAGE), pe);
}

index = ParserUtil.parseIndex(argMultiMap.getPreamble());
String username = argMultiMap.getValue(PREFIX_USERNAME).orElse("");

return new AddLCommand(index, new LinkedIn(username));
}
}
Loading

0 comments on commit 2557610

Please sign in to comment.