Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-F12-2#57 from bennyLCK/branch-b-S…
Browse files Browse the repository at this point in the history
…ortArticlesByDate

Add support for parsing sort article command
  • Loading branch information
Ko-Khan authored Mar 27, 2024
2 parents d401f41 + cdd93c6 commit ba23fa9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package seedu.address.logic.commands.articlecommands;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.SortArticleCommandParser;
import seedu.address.model.Model;

/**
* Sorts all articles in the article book by an attribute of articles.
*/
public class SortArticleCommand extends ArticleCommand {

public static final String COMMAND_WORD = "sort";

public static final String COMMAND_PREFIX = "-a";

public static final String MESSAGE_SUCCESS = "sorted all articles by date";

public static final String MESSAGE_USAGE = COMMAND_WORD + " " + COMMAND_PREFIX
+ ": sorts articles according to publication date and displays the sorted article list.\n"
+ "Parameters: D/ (corresponds to prefix for article's publication date attribute)\n"
+ "Example: " + COMMAND_WORD + " " + COMMAND_PREFIX + " D/";

private final String prefix;

/**
* @param prefix referring to an attribute of articles to sort by
*/
public SortArticleCommand(String prefix) {
requireNonNull(prefix);
this.prefix = prefix;
}

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

if (!SortArticleCommandParser.isAllowedPrefix(prefix)) {
throw new CommandException(Messages.MESSAGE_INVALID_SORTING_PREFIX);
}

// model.sortArticleBook(prefix);

return new CommandResult(MESSAGE_SUCCESS);
}
}
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.SortArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -66,6 +67,9 @@ public static Command parseCommand(String userInput) throws ParseException {
case ListArticleCommand.COMMAND_WORD:
return new ListArticleCommand();

case SortArticleCommand.COMMAND_WORD:
return new SortArticleCommandParser().parse(arguments);

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,43 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE;

import java.util.ArrayList;

import seedu.address.logic.commands.articlecommands.SortArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

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

private static final ArrayList<String> AllowedPrefixes = new ArrayList<>() {
{
add(PREFIX_PUBLICATION_DATE.getPrefix());
}
};

/**
* Checks if the given prefix is allowed in choosing an attribute for sorting.
*/
public static boolean isAllowedPrefix(String prefix) {
return AllowedPrefixes.contains(prefix);
}

/**
* Parses the given {@code String} of arguments in the context of the SortArticleCommand
* and returns an SortArticleCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public SortArticleCommand parse(String args) throws ParseException {
String prefix = args.trim();
if (prefix.isEmpty() || !PREFIX_PUBLICATION_DATE.getPrefix().equals(prefix)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortArticleCommand.MESSAGE_USAGE));
}

return new SortArticleCommand(prefix);
}
}

0 comments on commit ba23fa9

Please sign in to comment.