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

Filter articles by tag #85

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@
import seedu.address.model.Model;
import seedu.address.model.article.Article;
import seedu.address.model.article.ArticleMatchesStatusPredicate;
import seedu.address.model.article.ArticleMatchesTagPredicate;
import seedu.address.model.article.ArticleMatchesTimePeriodPredicate;
import seedu.address.model.article.exceptions.InvalidStatusException;
import seedu.address.model.tag.Tag;

/**
* Use to set filter for Articles
*/
public class SetArticleCommand extends ArticleCommand {
public static final String COMMAND_WORD = "set";
public class FilterArticleCommand extends ArticleCommand {
public static final String COMMAND_WORD = "filter";

public static final String COMMAND_PREFIX = "-a";

public static final String MESSAGE_SUCCESS = "Filter Updated";
public static final String MESSAGE_SUCCESS = "filter online";
private Predicate<Article> finalPredicate;

/**
* Constructs a SetArticleCommand object.
* Constructs a FilterArticleCommand object.
* @param status The status to be filtered by.
*/
public SetArticleCommand(String status, String start, String end) {
public FilterArticleCommand(String status, String tagName, String start, String end) throws ParseException {

Check warning on line 35 in src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java#L35

Added line #L35 was not covered by tests
try {
finalPredicate = new ArticleMatchesStatusPredicate(status);
} catch (InvalidStatusException e) {
Expand All @@ -42,7 +44,14 @@
Predicate<Article> timePredicate = new ArticleMatchesTimePeriodPredicate(startDate, endDate);
finalPredicate = finalPredicate.and(timePredicate);
} catch (ParseException e) {
finalPredicate = finalPredicate;
if (!start.equals("") && end.equals("")) {
throw e;

Check warning on line 48 in src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java#L48

Added line #L48 was not covered by tests
}
}

Check warning on line 50 in src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java#L50

Added line #L50 was not covered by tests
if (!tagName.trim().equals("")) {
Tag tag = new Tag(tagName);
Predicate<Article> tagPredicate = new ArticleMatchesTagPredicate(tag);
finalPredicate = finalPredicate.and(tagPredicate);

Check warning on line 54 in src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/FilterArticleCommand.java#L52-L54

Added lines #L52 - L54 were not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import seedu.address.logic.commands.articlecommands.AddArticleCommand;
import seedu.address.logic.commands.articlecommands.DeleteArticleCommand;
import seedu.address.logic.commands.articlecommands.EditArticleCommand;
import seedu.address.logic.commands.articlecommands.FilterArticleCommand;
import seedu.address.logic.commands.articlecommands.FindArticleCommand;
import seedu.address.logic.commands.articlecommands.ListArticleCommand;
import seedu.address.logic.commands.articlecommands.SetArticleCommand;
import seedu.address.logic.commands.articlecommands.SortArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -72,8 +72,8 @@
case SortArticleCommand.COMMAND_WORD:
return new SortArticleCommandParser().parse(arguments);

case SetArticleCommand.COMMAND_WORD:
return new SetArticleCommandParser().parse(arguments);
case FilterArticleCommand.COMMAND_WORD:
return new FilterArticleCommandParser().parse(arguments);

Check warning on line 76 in src/main/java/seedu/address/logic/parser/ArticleBookParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ArticleBookParser.java#L76

Added line #L76 was not covered by tests

default:
logger.finer("This user input caused a ParseException: " + userInput);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package seedu.address.logic.parser;

import static seedu.address.logic.parser.CliSyntax.PREFIX_ARTICLETAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_END;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;

import java.util.stream.Stream;

import seedu.address.logic.commands.articlecommands.SetArticleCommand;
import seedu.address.logic.commands.articlecommands.FilterArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a SetArticleCommand object
* Parses input arguments and creates a FilterArticleCommand object
*/
public class SetArticleCommandParser implements Parser<SetArticleCommand> {
public class FilterArticleCommandParser implements Parser<FilterArticleCommand> {

Check warning on line 16 in src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java#L16

Added line #L16 was not covered by tests

@Override
public SetArticleCommand parse(String userInput) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(userInput, PREFIX_STATUS, PREFIX_START, PREFIX_END);
if (!arePrefixesPresent(argMultimap, PREFIX_STATUS, PREFIX_START, PREFIX_END)) {
throw new ParseException("The set command does not follow the correct format");
public FilterArticleCommand parse(String userInput) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(userInput, PREFIX_STATUS,

Check warning on line 20 in src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java#L20

Added line #L20 was not covered by tests
PREFIX_ARTICLETAG, PREFIX_START, PREFIX_END);
if (!arePrefixesPresent(argMultimap, PREFIX_STATUS, PREFIX_ARTICLETAG, PREFIX_START, PREFIX_END)) {
throw new ParseException("The filter command does not follow the correct format");

Check warning on line 23 in src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java#L23

Added line #L23 was not covered by tests
}
String status = argMultimap.getValue(PREFIX_STATUS).get();
String tagName = argMultimap.getValue(PREFIX_ARTICLETAG).get();

Check warning on line 26 in src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java#L26

Added line #L26 was not covered by tests
String start = argMultimap.getValue(PREFIX_START).get();
String end = argMultimap.getValue(PREFIX_END).get();
return new SetArticleCommand(status, start, end);
return new FilterArticleCommand(status, tagName, start, end);

Check warning on line 29 in src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FilterArticleCommandParser.java#L29

Added line #L29 was not covered by tests
}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.article;


import java.util.function.Predicate;

import seedu.address.model.article.exceptions.InvalidStatusException;
Expand Down Expand Up @@ -29,6 +30,8 @@ public ArticleMatchesStatusPredicate(String s) throws InvalidStatusException {

@Override
public boolean test(Article article) {
//Check that article is not null.
assert (article instanceof Article);
return this.status.equals(article.getStatus());
}

Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

LGTM

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.model.article;

import static java.util.Objects.requireNonNull;

import java.util.Set;
import java.util.function.Predicate;

import seedu.address.model.tag.Tag;

/**
* Checks if article has matching tag
*/
public class ArticleMatchesTagPredicate implements Predicate<Article> {
private Tag tag;
public ArticleMatchesTagPredicate(Tag tag) {
this.tag = tag;
}

Check warning on line 17 in src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java#L15-L17

Added lines #L15 - L17 were not covered by tests

@Override
public boolean test(Article article) {
requireNonNull(article);
Set<Tag> others = article.getTags();
boolean isMatch = false;
requireNonNull(tag);

Check warning on line 24 in src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java#L21-L24

Added lines #L21 - L24 were not covered by tests
for (Tag other : others) {
requireNonNull(other);

Check warning on line 26 in src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java#L26

Added line #L26 was not covered by tests
if (other.equals(tag)) {
isMatch = true;

Check warning on line 28 in src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java#L28

Added line #L28 was not covered by tests
}
}
return isMatch;

Check warning on line 31 in src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesTagPredicate.java#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}
Loading