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

Branch kk filter by status #66

Merged
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
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/commands/SetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package seedu.address.logic.commands;

/**
* Sets predicates that filter article book
*/
public class SetCommand {

Check warning on line 6 in src/main/java/seedu/address/logic/commands/SetCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/SetCommand.java#L6

Added line #L6 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not "FilterCommand"

Copy link
Collaborator Author

@Ko-Khan Ko-Khan Mar 28, 2024

Choose a reason for hiding this comment

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

I wanted to emphasize that using the command sets the filter for all future searches.

public static final String COMMAND_WORD = "set";
}
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,44 @@
package seedu.address.logic.commands.articlecommands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ARTICLES;

import java.util.function.Predicate;

import seedu.address.logic.commands.CommandResult;
import seedu.address.model.Model;
import seedu.address.model.article.Article;
import seedu.address.model.article.ArticleMatchesStatusPredicate;
import seedu.address.model.article.exceptions.InvalidStatusException;

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

public static final String COMMAND_PREFIX = "-a";

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

/**
* Constructs a SetArticleCommand object.
* @param status The status to be filtered by.
*/
public SetArticleCommand(String status) {

Check warning on line 29 in src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java#L29

Added line #L29 was not covered by tests
try {
finalPredicate = new ArticleMatchesStatusPredicate(status);
} catch (InvalidStatusException e) {
finalPredicate = PREDICATE_SHOW_ALL_ARTICLES;
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java#L31-L35

Added lines #L31 - L35 were not covered by tests

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.getFilter().updateFilter(finalPredicate);
model.updateFilteredArticleList(finalPredicate);
return new CommandResult(MESSAGE_SUCCESS);

Check warning on line 42 in src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java#L39-L42

Added lines #L39 - L42 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.logic.commands.articlecommands.EditArticleCommand;
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 All @@ -36,6 +37,7 @@
* @return the command based on the user input
* @throws ParseException if the user input does not conform the expected format
*/
@SuppressWarnings("checkstyle:Regexp")
public static Command parseCommand(String userInput) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
Expand Down Expand Up @@ -70,6 +72,9 @@
case SortArticleCommand.COMMAND_WORD:
return new SortArticleCommandParser().parse(arguments);

case SetArticleCommand.COMMAND_WORD:
return new SetArticleCommandParser().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);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

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.parser.exceptions.ParseException;

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

Check warning on line 13 in src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java#L13

Added line #L13 was not covered by tests

@Override
public SetArticleCommand parse(String userInput) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(userInput, PREFIX_STATUS);

Check warning on line 17 in src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java#L17

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

Check warning on line 19 in src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java#L19

Added line #L19 was not covered by tests
}
String status = argMultimap.getValue(PREFIX_STATUS).get();
return new SetArticleCommand(status);

Check warning on line 22 in src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/SetArticleCommandParser.java#L21-L22

Added lines #L21 - L22 were not covered by tests
}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests
}
}
23 changes: 23 additions & 0 deletions src/main/java/seedu/address/model/ArticleFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.address.model;

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

import java.util.function.Predicate;

import seedu.address.model.article.Article;

/**
* Use to filter through ArticleBook
*/
public class ArticleFilter implements Filter {
private Predicate<Article> finalPredicate;
public ArticleFilter() {
finalPredicate = PREDICATE_SHOW_ALL_ARTICLES;
}
public void updateFilter(Predicate<Article> newPredicate) {
finalPredicate = newPredicate;
}

Check warning on line 19 in src/main/java/seedu/address/model/ArticleFilter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ArticleFilter.java#L18-L19

Added lines #L18 - L19 were not covered by tests
public Predicate<Article> getFinalPredicate() {
return finalPredicate;

Check warning on line 21 in src/main/java/seedu/address/model/ArticleFilter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ArticleFilter.java#L21

Added line #L21 was not covered by tests
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seedu.address.model;

/**
* Used to sieve through lists.
*/
public interface Filter {
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,5 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredArticleList(Predicate<Article> predicate);
ArticleFilter getFilter();
}
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;
private final FilteredList<Article> filteredArticles;
private final ArticleFilter filter;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -39,6 +40,7 @@
this.userPrefs = new UserPrefs(userPrefs);
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
filteredArticles = new FilteredList<>(this.articleBook.getArticleList());
filter = new ArticleFilter();
}

public ModelManager() {
Expand Down Expand Up @@ -79,7 +81,6 @@
requireNonNull(addressBookFilePath);
userPrefs.setAddressBookFilePath(addressBookFilePath);
}

//=========== AddressBook ================================================================================

@Override
Expand Down Expand Up @@ -193,6 +194,7 @@
@Override
public void updateFilteredArticleList(Predicate<Article> predicate) {
requireNonNull(predicate);
predicate = predicate.and(filter.getFinalPredicate());

Check warning on line 197 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L197

Added line #L197 was not covered by tests
filteredArticles.setPredicate(predicate);
}

Expand All @@ -215,4 +217,7 @@
&& filteredArticles.equals(otherModelManager.filteredArticles);
}

public ArticleFilter getFilter() {
return filter;

Check warning on line 221 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L221

Added line #L221 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.model.article;

import java.util.function.Predicate;

import seedu.address.model.article.exceptions.InvalidStatusException;

/**
* Ensures that an {@code Article}'s {@code Status} matches the given status
*/
public class ArticleMatchesStatusPredicate implements Predicate<Article> {
private Article.Status status;

/**
* Constructs an ArticleMatchesStatusPredicate
* @param s The string representation of the status
* @throws InvalidStatusException thrown when status entered is invalid.
*/
public ArticleMatchesStatusPredicate(String s) throws InvalidStatusException {

Check warning on line 18 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L18

Added line #L18 was not covered by tests
if (s.equals("PUBLISHED")) {
status = Article.Status.PUBLISHED;

Check warning on line 20 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L20

Added line #L20 was not covered by tests
} else if (s.equals("DRAFT")) {
status = Article.Status.DRAFT;

Check warning on line 22 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L22

Added line #L22 was not covered by tests
} else if (s.equals("ARCHIVED")) {
status = Article.Status.ARCHIVED;

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

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L24

Added line #L24 was not covered by tests
} else {
throw new InvalidStatusException();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L28 was not covered by tests

@Override
public boolean test(Article article) {
return this.status.equals(article.getStatus());

Check warning on line 32 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L32

Added line #L32 was not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 38 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L38

Added line #L38 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof TitleContainsKeywordsPredicate)) {
return false;

Check warning on line 43 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L43

Added line #L43 was not covered by tests
}

ArticleMatchesStatusPredicate otherArticleMatchesStatusPredicate = (ArticleMatchesStatusPredicate) other;
return this.status.equals(otherArticleMatchesStatusPredicate.getStatus());

Check warning on line 47 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L46-L47

Added lines #L46 - L47 were not covered by tests
}
public Article.Status getStatus() {
return this.status;

Check warning on line 50 in src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/ArticleMatchesStatusPredicate.java#L50

Added line #L50 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
* Tests that an {@code Article}'s {@code Title} matches any of the keywords given.
*/
public class TitleContainsKeywordsPredicate implements Predicate<Article> {
private final List<String> keywords;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package seedu.address.model.article.exceptions;

/**
* Signals that status entered is not a valid one.
*/
public class InvalidStatusException extends RuntimeException {
public InvalidStatusException() {
super("The status is invalid");
}

Check warning on line 9 in src/main/java/seedu/address/model/article/exceptions/InvalidStatusException.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/article/exceptions/InvalidStatusException.java#L8-L9

Added lines #L8 - L9 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.ArticleFilter;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyArticleBook;
Expand Down Expand Up @@ -209,6 +210,10 @@ public void setArticle(Article target, Article editedArticle) {
public void sortArticleBook(String prefix) {
throw new AssertionError("This method should not be called.");
}
@Override
public ArticleFilter getFilter() {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
Loading