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

Add feed-related commands #88

Merged
merged 5 commits into from Oct 23, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddFeedCommand.java
@@ -0,0 +1,57 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.feed.Feed;

/**
* Adds a feed to the feed list.
*/
public class AddFeedCommand extends Command {

public static final String COMMAND_WORD = "addfeed";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a feed to the feed list."
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_ADDRESS + "ADDRESS "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "Ladyironchef "
+ PREFIX_ADDRESS + "https://www.ladyironchef.com/feed/";

public static final String MESSAGE_SUCCESS = "New feed added: %1$s";
public static final String MESSAGE_DUPLICATE_FEED = "This feed already exists in the feed list.";

private final Feed toAdd;

/**
* Creates an AddFeedCommand to add the specified {@code Feed}
*/
public AddFeedCommand(Feed feed) {
requireNonNull(feed);
toAdd = feed;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasFeed(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_FEED);
}

model.addFeed(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddFeedCommand // instanceof handles nulls
&& toAdd.equals(((AddFeedCommand) other).toAdd));
}
}
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteFeedCommand.java
@@ -0,0 +1,57 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.feed.Feed;

/**
* Deletes a feed identified using its name from the feed list.
*/
public class DeleteFeedCommand extends Command {

public static final String COMMAND_WORD = "deletefeed";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the feed identified using its name from the feed list.\n"
+ "Parameters: " + PREFIX_NAME + " NAME"
+ "Example: " + COMMAND_WORD + " " + PREFIX_NAME + " Seth Lui";

public static final String MESSAGE_DELETE_FEED_SUCCESS = "Deleted feed: %1$s";
public static final String MESSAGE_MISSING_FEED = "Feed does not exist in feed list.";

private final String name;

public DeleteFeedCommand(String name) {
this.name = name;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

Feed feedToDelete = null;
for (Feed f : model.getFeedList().getFeedList()) {
if (f.getName().equals(name)) {
feedToDelete = f;
break;
}
}

if (feedToDelete == null) {
throw new CommandException(MESSAGE_MISSING_FEED);
}

model.deleteFeed(feedToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_FEED_SUCCESS, feedToDelete));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteFeedCommand // instanceof handles nulls
&& name.equals(((DeleteFeedCommand) other).name)); // state check
}
}
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddFeedCommandParser.java
@@ -0,0 +1,48 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.stream.Stream;

import seedu.address.logic.commands.AddFeedCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.feed.Feed;

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

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

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddFeedCommand.MESSAGE_USAGE));
}

String name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()).fullName;
String address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()).value;
Comment on lines +32 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be a huge hurdle to build a validation check before adding into the list? Personally believe that if the URL is wrong, then the application should not save it into the list in the first place.


Feed feed = new Feed(name, address);

return new AddFeedCommand(feed);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
Expand Up @@ -7,10 +7,12 @@
import java.util.regex.Pattern;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddFeedCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.CloseCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteFeedCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
Expand Down Expand Up @@ -81,6 +83,12 @@ public Command parseCommand(String userInput) throws ParseException {
case ReviewCommand.COMMAND_WORD:
return new ReviewCommandParser().parse(arguments);

case AddFeedCommand.COMMAND_WORD:
return new AddFeedCommandParser().parse(arguments);

case DeleteFeedCommand.COMMAND_WORD:
return new DeleteFeedCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
@@ -0,0 +1,43 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.stream.Stream;

import seedu.address.logic.commands.DeleteFeedCommand;
import seedu.address.logic.parser.exceptions.ParseException;

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

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

if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteFeedCommand.MESSAGE_USAGE));
}

String name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()).fullName;

return new DeleteFeedCommand(name);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
41 changes: 40 additions & 1 deletion src/main/java/seedu/address/ui/FeedPostListPanel.java
@@ -1,9 +1,13 @@
package seedu.address.ui;

import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
Expand Down Expand Up @@ -31,8 +35,43 @@ public FeedPostListPanel(ReadOnlyFeedList feedList) {
feedPostListView.setItems(feedPostList);
feedPostListView.setCellFactory(listView -> new FeedPostListViewCell());

fetchPosts(feedPostList, feedList.getFeedList());

ObservableList<Feed> observableFeedList = feedList.getFeedList();
ListChangeListener<Feed> listener = change -> updatePosts(feedPostList, change);
observableFeedList.addListener(listener);
}

/**
* Detects the changes made in a `ListChangeListener.Change` object and updates the post list accordingly.
* If feeds are added, trigger a thread to fetch its posts. If feeds are removed, remove all its posts from the
* feed list.
* @param feedPostList Observable list of feed posts
* @param change Object representing a change in the feed list
*/
private void updatePosts(ObservableList<FeedPost> feedPostList, ListChangeListener.Change<? extends Feed> change) {
change.next();
List<Feed> added = change.getAddedSubList().stream().filter(Objects::nonNull).collect(Collectors.toList());
List<Feed> removed = change.getRemoved().stream().filter(Objects::nonNull).collect(Collectors.toList());

if (added.size() > 0) {
fetchPosts(feedPostList, added);
}
if (removed.size() > 0) {
for (Feed f : removed) {
feedPostList.removeIf(feedPost -> feedPost.getSource().equals(f.getName()));
}
}
}

/**
* Fires off a thread that fetches posts from a list of feeds and adds them to the post list.
* @param feedPostList Observable list of feed posts.
* @param feedList List of input feeds.
*/
private void fetchPosts(ObservableList<FeedPost> feedPostList, List<Feed> feedList) {
Runnable feedPostFetch = () -> {
for (Feed feed : feedList.getFeedList()) {
for (Feed feed : feedList) {
ObservableList<FeedPost> feedPosts = feed.fetchPosts();
Platform.runLater(() -> {
feedPostList.addAll(feedPosts);
Comment on lines +72 to 77

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the URL provided is not accurate? What will be the response given by the application?

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddFeedCommandTest.java
@@ -0,0 +1,46 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalEateries.getTypicalAddressBook;
import static seedu.address.testutil.TypicalFeeds.getTypicalFeedList;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.feed.Feed;
import seedu.address.testutil.FeedBuilder;

/**
* Contains integration tests (interaction with the Model) for {@code AddFeedCommand}.
*/
public class AddFeedCommandTest {

private Model model;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), getTypicalFeedList(), new UserPrefs());
}

@Test
public void execute_newFeed_success() {
Feed validFeed = new FeedBuilder().build();

Model expectedModel = new ModelManager(model.getAddressBook(), model.getFeedList(), new UserPrefs());
expectedModel.addFeed(validFeed);

assertCommandSuccess(new AddFeedCommand(validFeed), model,
String.format(AddFeedCommand.MESSAGE_SUCCESS, validFeed), expectedModel);
}

@Test
public void execute_duplicateFeed_throwsCommandException() {
Feed feedInFeedList = model.getFeedList().getFeedList().get(0);

assertCommandFailure(new AddFeedCommand(feedInFeedList), model, AddFeedCommand.MESSAGE_DUPLICATE_FEED);
}
}
11 changes: 9 additions & 2 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Expand Up @@ -43,6 +43,13 @@ public class CommandTestUtil {
public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + " " + VALID_TAG_HUSBAND;
public static final String CATEGORY_DESC = " " + PREFIX_CATEGORY + " " + VALID_CATEGORY;

public static final String VALID_NAME_EATBOOK = "Eatbook";
public static final String VALID_NAME_SETHLUI = "Seth Lui";
public static final String VALID_ADDRESS_EATBOOK = "https://eatbook.sg/feed";
public static final String VALID_ADDRESS_SETHLUI = "https://sethlui.com/feed";
public static final String NAME_DESC_EATBOOK = " " + PREFIX_NAME + " " + VALID_NAME_EATBOOK;
public static final String ADDRESS_DESC_EATBOOK = " " + PREFIX_ADDRESS + " " + VALID_ADDRESS_EATBOOK;

public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + " James&"; // '&' not allowed in names
public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS; // empty string not allowed for addresses
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + " hubby*"; // '*' not allowed in tags
Expand Down Expand Up @@ -70,7 +77,7 @@ public class CommandTestUtil {
* - the {@code actualModel} matches {@code expectedModel}
*/
public static void assertCommandSuccess(Command command, Model actualModel, CommandResult expectedCommandResult,
Model expectedModel) {
Model expectedModel) {
try {
CommandResult result = command.execute(actualModel);
assertEquals(expectedCommandResult, result);
Expand All @@ -85,7 +92,7 @@ public static void assertCommandSuccess(Command command, Model actualModel, Comm
* that takes a string {@code expectedMessage}.
*/
public static void assertCommandSuccess(Command command, Model actualModel, String expectedMessage,
Model expectedModel) {
Model expectedModel) {
CommandResult expectedCommandResult = new CommandResult(expectedMessage);
assertCommandSuccess(command, actualModel, expectedCommandResult, expectedModel);
}
Expand Down