forked from nus-cs2103-AY1920S1/addressbook-level3
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d652c0
Add `addfeed` command
dvrylc a1fa722
Add `deletefeed` command
dvrylc 7060c22
Merge remote-tracking branch 'upstream/master' into feed-commands
dvrylc 0a949a7
Add missing Javadocs
dvrylc 5238275
Add tests for feed-related commands
dvrylc File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/seedu/address/logic/commands/AddFeedCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
57
src/main/java/seedu/address/logic/commands/DeleteFeedCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
48
src/main/java/seedu/address/logic/parser/AddFeedCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/seedu/address/logic/parser/DeleteFeedCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
46 changes: 46 additions & 0 deletions
46
src/test/java/seedu/address/logic/commands/AddFeedCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.