Skip to content

Commit

Permalink
Merge pull request #87 from skpai27/deleteCommand
Browse files Browse the repository at this point in the history
deleteReview command
  • Loading branch information
skpai27 authored Mar 26, 2019
2 parents 5e5f911 + f3d09d7 commit bed6f4d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Edit selected fields in a specified entry. +
Format: `editReview INDEX [re/ENTRY] [rr/RATING]`

****
* Edits the review at the specified `INDEX`. The index refers to the index number shown in the displayed reviews list. The index *must be a positive integer* 1, 2, 3, ...
* Edits the review at the specified `INDEX`. The index refers to the index number of the reviews of the selected Restaurant. The index *must be a positive integer* 1, 2, 3, ...
* At least one of the optional fields must be provided.
* Existing values will be updated to the input values.
****
Expand All @@ -98,15 +98,15 @@ Deletes the review from the Food Diary. +
Format: `deleteReview INDEX`

****
* Deletes the review at the specified `INDEX`.
* Deletes the review at the specified `INDEX` of the selected Restaurant.
* The index refers to the index number shown in the displayed reviews list.
* The index *must be a positive integer* 1, 2, 3, ...
****

Examples:

* `delete 2` +
Deletes the 2nd review in the Food Diary.
* `deleteReview 2` +
Deletes the 2nd review of the selected Restaurant in the Food Diary.

=== Add a picture of the food to a review : `addImage`

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_RESTAURANT_DISPLAYED_INDEX = "The restaurant index provided is invalid";
public static final String MESSAGE_INVALID_REVIEW_INDEX = "The review index provided is invalid";
public static final String MESSAGE_NO_REVIEWS = "There are no reviews for this restaurant.";
public static final String MESSAGE_RESTAURANTS_LISTED_OVERVIEW = "%1$d restaurants listed!";
public static final String MESSAGE_NO_RESTAURANT_SELECTED = "No restaurant selected";
}
101 changes: 101 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteReviewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package seedu.address.logic.commands;

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

import java.util.ArrayList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.restaurant.Restaurant;
import seedu.address.model.review.Review;

/**
* Edits the details of an existing review tagged to a restaurant in the Food Diary.
*/
public class DeleteReviewCommand extends Command {
public static final String COMMAND_WORD = "deleteReview";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the review identified "
+ "by the index number used in the displayed review list "
+ "of the selected restaurant first, i.e. user needs to first "
+ "select a restaurant from the list of restaurants.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1 ";

public static final String MESSAGE_DELETE_REVIEW_SUCCESS = "Deleted review number %1$s for restaurant: %2$s";

private final Index index;

/**
* @param index of the restaurant in the filtered restaurant list to edit
*/
public DeleteReviewCommand(Index index) {
requireNonNull(index);
this.index = index;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
Restaurant selectedRestaurant = model.getSelectedRestaurant();

if (selectedRestaurant == null) {
throw new CommandException(Messages.MESSAGE_NO_RESTAURANT_SELECTED);
}

if (selectedRestaurant.getReviews().size() == 0) {
throw new CommandException(Messages.MESSAGE_NO_REVIEWS);
}
if (selectedRestaurant.getReviews().size() < index.getOneBased()) {
throw new CommandException(Messages.MESSAGE_INVALID_REVIEW_INDEX);
}

Restaurant restaurantWithDeletedReview = createRestaurantWithDeletedReview(selectedRestaurant, index);

model.setRestaurant(selectedRestaurant, restaurantWithDeletedReview);
model.updateFilteredRestaurantList(PREDICATE_SHOW_ALL_RESTAURANTS);
model.commitFoodDiary();
return new CommandResult(String.format(MESSAGE_DELETE_REVIEW_SUCCESS, index.getOneBased(),
restaurantWithDeletedReview.getName()));
}

/**
* Creates and returns a {@code Restaurant} with the details of {@code restaurantToEdit}
* edited with {@code editRestaurantDescriptor}.
*/
private static Restaurant createRestaurantWithDeletedReview(Restaurant restaurantSelected, Index index) {
assert restaurantSelected != null;

List<Review> editedReviews = new ArrayList<>();
editedReviews.addAll(restaurantSelected.getReviews());
editedReviews.remove(index.getZeroBased());

return new Restaurant(restaurantSelected.getName(), restaurantSelected.getPhone(),
restaurantSelected.getEmail(),
restaurantSelected.getAddress(), restaurantSelected.getTags(),
restaurantSelected.getWeblink(), restaurantSelected.getOpeningHours(),
restaurantSelected.getCategories(), editedReviews);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

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

// state check
DeleteReviewCommand e = (DeleteReviewCommand) other;
return index.equals(e.index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class EditReviewCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_RESTAURANT = "This review already exists for this restaurant.";


private final Index index;
private final EditReviewDescriptor editReviewDescriptor;

Expand All @@ -64,6 +65,12 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
if (selectedRestaurant == null) {
throw new CommandException(Messages.MESSAGE_NO_RESTAURANT_SELECTED);
}
if (selectedRestaurant.getReviews().size() == 0) {
throw new CommandException(Messages.MESSAGE_NO_REVIEWS);
}
if (selectedRestaurant.getReviews().size() > index.getOneBased()) {
throw new CommandException(Messages.MESSAGE_INVALID_REVIEW_INDEX);
}

Restaurant restaurantWithEditedReview = createRestaurantWithEditedReview(selectedRestaurant,
editReviewDescriptor, index);
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.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteReviewCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteReviewCommandParser implements Parser<DeleteReviewCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns an DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteReviewCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteReviewCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteReviewCommand.MESSAGE_USAGE), pe);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/FoodDiaryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteReviewCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditReviewCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -77,6 +78,9 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);

case DeleteReviewCommand.COMMAND_WORD:
return new DeleteReviewCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand Down

0 comments on commit bed6f4d

Please sign in to comment.