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

added deleteModuleCommand and parser for that command, updated tests … #77

Merged
merged 8 commits into from
Oct 24, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions docs/DeveloperGuide.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= AddressBook Level 4 - Developer Guide
= Forum Book - Developer Guide
:site-section: DeveloperGuide
:toc:
:toc-title:
Expand All @@ -12,9 +12,9 @@ ifdef::env-github[]
:note-caption: :information_source:
:warning-caption: :warning:
endif::[]
:repoURL: https://github.com/se-edu/addressbook-level4/tree/master
:repoURL: https://github.com/CS2113-AY1819S1-T13-2/main/

By: `Team SE-EDU`      Since: `Jun 2016`      Licence: `MIT`
By: `Team CS2113 T13-2`      Since: `Aug 2018`      Licence: `MIT`

== Setting up

Expand Down Expand Up @@ -411,6 +411,11 @@ The admin management is facilitated by various commands. It extends `ForumBook`

* `SetAdminCommand`: Sets a user as admin or reverts an admin to user.

* `CreateModuleCommand`: Creates a module by admin.

* `AdminUpdatePasswordCommand`: Updates a certain user's password by admin.

* `DeleteUserCommand`: Deletes a certain user by admin.

Given below is an example usage scenario and how the admin management behaves at each step.

Expand All @@ -427,10 +432,26 @@ Step 3. The admin executes `block uName/john` to block john from creating new th

image::SDBlockUserFromCreating.png[width="800"]

Step 4. The admin executes `setAdmin uName/john set/true` to set a certain user as admin or to revert an admin to user. The `setAdmin` calls the `User.setAdmin(true)` and then `UnitOfWork.commit()`, cuasing another modified forum book state to be saved into `ForumBookStorage`.
Step 4. The admin executes `setAdmin uName/john set/true` to set a certain user as admin or to revert an admin to user. The `setAdmin` calls the `User.setAdmin(true)` and then `UnitOfWork.commit()`, causing another modified forum book state to be saved into `ForumBookStorage`.

image::SDSetAdmin.png[width="800"]

Step 5. The admin executes `createModule mCode/CS2113 mTitle/Software Engineering and OOP` to create the specific module. The `createModule` calls the `UnitOfWork.getModuleRepository().addModule(module)` and then `UnitOfWork.commit()`, causing another modified forum book state to be save into `ForumBookStorage`.

image::SDCreateModule.png[width="800"]

Step 6. The admin executes `deleteModule mCode/CS2113` to delete specific module. The `deleteModule` calls the `UnitOfWork.getModuleRepository().removeModule(module);` and then `UnitOfWork.commit()`, causing another modified forum book state to be save into `ForumBookStorage`.

image::SDDeleteModule.png[width="800"]

Step 7. The admin executes `updatePass uName/john uPass/098` to update the user john's password. The `updatePass` calls the `UnitOfWork.getUserRepository().updateUser(user)` and then `UnitOfWork.commit()`, causing another modified forum book state to be save into `ForumBookStorage`.

image::SDUpdatePass.png[width="800"]

Step 8. The admin executes `deleteUser uName/john` to delete the user john. The `deleteUser` calls the ` UnitOfWork.getUserRepository().deleteUser(userToDelete)` and then `UnitOfWork.commit()`, causing another modified forum book state to be save into `ForumBookStorage`.

image::SDDeleteUser.png[width="800"]

[NOTE]
If the syntax of a command is wrong, the program will prompt the user to try again and show a suggested command format.

Expand Down
72 changes: 71 additions & 1 deletion docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,83 @@ Examples:

=== Sorting a forumThread by most like : `sortlike`

Sort the threads under a specific module by date. +
Sort the threads under a specific module by most like. +
Format: `sortlike m/MODULE_NAME`

Examples:

* `sortlike m/CS2113`

=== Announcing new command : `announce`

Announce the new announcement created by admin. +
Format: `announce aTitle/ANNOUNCEMENT_TITLE aContent/ANNOUNCEMENT_CONTENT`

Example:

* `announce aTitle/Urgent! aContent/System maintenance from 4pm to 5pm.`

=== Checking for the latest announcement : `checkAnnounce`

Check for the latest announcement. +
Format: `checkAnnounce`

=== Blocking a user from creating thread or comment : `block`

Block a specific user that match the given argument by admin. +
Format: `block uName/USER_NAME`

Example:

* `block uName/john`

=== Setting a user as admin or revert an admin to user : `setAdmin`

Set a user as an admin or revert an admin to user. +
Format: `setAdmin uName/USER_NAME set/SET_OR_REVERT`

Examples:

* `setAdmin uName/john set/true`

* `setAdmin uName/john set/false`

=== Creating a new module : `createModule`

Create a new module by admin. +
Format: `createModule mCode/MODULE_CODE mTitle/MODULE_TITLE`

Example:

* `createModule mCode/CS2113 mTitle/Software Engineering and OOP`

=== Deleting a module : `deleteModule`

Delete an existing module by admin. +
Format: `deleteModule mCode/MODULE_CODE`

Example:

* `deleteModule mCode/CS2113`

=== Updating password : `updatePass`

Update the given user's password by admin. +
Format: `updatePass uName/USER_NAME uPass/USER_PASSWORD`

Example:

* `updatePass uName/john uPass/098`

=== Delete a user : `deleteUser`

Delete a given user by admin. +
Format: `deleteUser uName/USER_NAME`

Example:

* `deleteUser uName/john`

=== Exiting the program : `exit`

Exits the program. +
Expand Down
Binary file modified docs/diagrams/HighLevelSequenceDiagrams.pptx
Binary file not shown.
Binary file added docs/images/SDCreateModule.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/SDDeleteModule.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/SDDeleteUser.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/SDSetAdmin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/SDUpdatePass.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class CreateModuleCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Creates a module. "
+ "Parameters: "
+ PREFIX_MODULE_TITLE + "MODULE TITLE "
+ PREFIX_MODULE_CODE + "MODULE CODE "
+ PREFIX_MODULE_TITLE + "MODULE TITLE "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_MODULE_TITLE + "Software Engineering and OOP "
+ PREFIX_MODULE_CODE + "CS2113";
+ PREFIX_MODULE_CODE + "CS2113 "
+ PREFIX_MODULE_TITLE + "Software Engineering and OOP";

public static final String MESSAGE_SUCCESS = "%1$s now is added to module";
public static final String MESSAGE_DUPLICATE_MODULE = "%1$s has been added previously";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.t13g2.forum.logic.commands;

import static com.t13g2.forum.logic.parser.CliSyntax.PREFIX_MODULE_CODE;
import static java.util.Objects.requireNonNull;

import com.t13g2.forum.logic.CommandHistory;
import com.t13g2.forum.logic.commands.exceptions.CommandException;
import com.t13g2.forum.model.Model;
import com.t13g2.forum.model.forum.User;

//@@xllx1

/**
* Deletes a module to the forum book by admin
*/
public class DeleteModuleCommand extends Command {
public static final String COMMAND_WORD = "deleteModule";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes a module. "
+ "Parameters: "
+ PREFIX_MODULE_CODE + "MODULE CODE "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_MODULE_CODE + "CS2113";

public static final String MESSAGE_SUCCESS = "%1$s is now deleted.";
public static final String MESSAGE_INVALID_MODULE = "There is no module matching %1$s.";

private final String moduleCodeToDelete;

/**
* Creates an DeleteModuleCommand to create the specified {@code module}.
*/
public DeleteModuleCommand(String moduleCode) {
requireNonNull(moduleCode);
moduleCodeToDelete = moduleCode;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
// if user has not login or is not admin, then throw exception
if (!model.checkIsLogin()) {
throw new CommandException(User.MESSAGE_NOT_LOGIN);
}
if (!model.checkIsAdmin()) {
throw new CommandException(User.MESSAGE_NOT_ADMIN);
}
if (model.deleteModule(moduleCodeToDelete)) {
return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCodeToDelete));
} else {
throw new CommandException(String.format(MESSAGE_INVALID_MODULE,
moduleCodeToDelete));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LoginCommand extends Command {


public static final String MESSAGE_SUCCESS = "WELCOME : %1$s";
public static final String MESSAGE_FAIL = "No user named %1$s found or password is wrong";
public static final String MESSAGE_FAIL = "No user named %s found or password is wrong";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
private final String userName;
private final String userPassword;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.t13g2.forum.logic.commands.exceptions;

import com.t13g2.forum.logic.commands.Command;

/**
* Represents an error which occurs during execution of a {@link Command}.
*/
public class InvalidModuleException extends Exception {
public InvalidModuleException(String message) {
super(message);
}

/**
* Constructs a new {@code CommandException} with the specified detail {@code message} and {@code cause}.
*/
public InvalidModuleException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.t13g2.forum.logic.commands.CreateModuleCommand;
import com.t13g2.forum.logic.commands.CreateThreadCommand;
import com.t13g2.forum.logic.commands.DeleteCommand;
import com.t13g2.forum.logic.commands.DeleteModuleCommand;
import com.t13g2.forum.logic.commands.DeleteThreadCommand;
import com.t13g2.forum.logic.commands.EditCommand;
import com.t13g2.forum.logic.commands.ExitCommand;
Expand Down Expand Up @@ -117,6 +118,9 @@ public Command parseCommand(String userInput) throws ParseException {
case CreateModuleCommand.COMMAND_WORD:
return new CreateModuleCommandParser().parse(arguments);

case DeleteModuleCommand.COMMAND_WORD:
return new DeleteModuleCommandParser().parse(arguments);

case CreateThreadCommand.COMMAND_WORD:
return new CreateThreadCommandParser().parse(arguments);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/t13g2/forum/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_COMMENT = new Prefix("c/");
public static final Prefix PREFIX_INDEX = new Prefix("i/");
public static final Prefix PREFIX_MODULE = new Prefix("m/");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.t13g2.forum.logic.parser;

import static com.t13g2.forum.logic.parser.CliSyntax.PREFIX_MODULE_CODE;
import static com.t13g2.forum.logic.parser.CliSyntax.PREFIX_MODULE_TITLE;

import java.util.stream.Stream;

import com.t13g2.forum.commons.core.Messages;
import com.t13g2.forum.logic.commands.DeleteModuleCommand;
import com.t13g2.forum.logic.parser.exceptions.ParseException;

//@@xllx1
/**
* Parses input arguments and deletes a module from forum book.
*/
public class DeleteModuleCommandParser implements Parser<DeleteModuleCommand> {
/**
* Parses the given {@code String} of arguments in the context of the DeleteModuleCommand
* and returns an DeleteModuleCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteModuleCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_MODULE_CODE);

if (!arePrefixesPresent(argMultimap, PREFIX_MODULE_CODE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT,
DeleteModuleCommand.MESSAGE_USAGE));
}

String moduleCode = argMultimap.getValue(PREFIX_MODULE_TITLE).get();

return new DeleteModuleCommand(moduleCode);
}

/**
* 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());
}
}
34 changes: 31 additions & 3 deletions src/main/java/com/t13g2/forum/model/ForumBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import com.t13g2.forum.logic.commands.exceptions.DuplicateModuleException;
import com.t13g2.forum.logic.commands.exceptions.InvalidModuleException;
import com.t13g2.forum.model.forum.Announcement;
import com.t13g2.forum.model.forum.Module;
import com.t13g2.forum.model.forum.User;
Expand Down Expand Up @@ -227,15 +228,42 @@ public void setAdmin(User user) {
* creates a module.
*/
public boolean createModule(Module module) {
boolean exist = false;
try (UnitOfWork unitOfWork = new UnitOfWork()) {
if (unitOfWork.getModuleRepository().getModuleByCode(module.getModuleCode()) == null) {
unitOfWork.getModuleRepository().getModuleByCode(module.getModuleCode());
exist = true;
throw new DuplicateModuleException("");
} catch (DuplicateModuleException e) {
return false;
} catch (Exception e) {
e.printStackTrace();
}

if (!exist) {
try (UnitOfWork unitOfWork = new UnitOfWork()) {
module.setCreatedByUserId(Context.getInstance().getCurrentUser().getId());
unitOfWork.getModuleRepository().addModule(module);
unitOfWork.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}

/**
* deletes a module.
*/
public boolean deleteModule(String moduleCode) {
try (UnitOfWork unitOfWork = new UnitOfWork()) {
Module moduleToDelete = unitOfWork.getModuleRepository().getModuleByCode(moduleCode);
if (moduleToDelete != null) {
unitOfWork.getModuleRepository().removeModule(moduleToDelete);
unitOfWork.commit();
} else {
throw new DuplicateModuleException("");
throw new InvalidModuleException("");
}
} catch (DuplicateModuleException e) {
} catch (InvalidModuleException e) {
return false;
} catch (Exception e) {
e.printStackTrace();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/t13g2/forum/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,20 @@ public interface Model {
boolean blockUser(User user);

/**
* set or revert the user as admin.
* sets or revert the user as admin.
*/
void setAdmin(User user);

/**
* create module by admin.
* creates module by admin.
*/
boolean createModule(Module module);

/**
* deletes module by admin.
*/
boolean deleteModule(String moduleCode);

/**
* admin updates password.
*/
Expand Down