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

Logout Command had been added and working #78

Merged
merged 2 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
27 changes: 10 additions & 17 deletions src/main/java/com/t13g2/forum/logic/commands/AddUserCommand.java
Expand Up @@ -46,26 +46,19 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
try (UnitOfWork unitOfWork = new UnitOfWork()) {
boolean exist = false;
try {
unitOfWork.getUserRepository().getUserByUsername(userToAdd.getUsername());
exist = true;
if (userToAdd == unitOfWork.getUserRepository().getUserByUsername(userToAdd.getUsername())) {
exist = true;
new CommandResult(String.format(MESSAGE_DUPLICATE_PERSON));
}

} catch (EntityDoesNotExistException ex) {
if (!exist) {
unitOfWork.getUserRepository().addUser(userToAdd);
unitOfWork.commit();
}
ex.printStackTrace();
}
if (!exist) {
unitOfWork.getUserRepository().addUser(userToAdd);
unitOfWork.commit();
}
// try {
// if(unitOfWork.getUserRepository().getUserByUsername(this.userToAdd.getUsername()) == null) {
// unitOfWork.getUserRepository().addUser(userToAdd);
// unitOfWork.commit();
// }
// else {
// throw new CommandException(MESSAGE_DUPLICATE_PERSON);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
if (loggedInSuccess) {
return new CommandResult(String.format(MESSAGE_SUCCESS, userName));
} else {
return new CommandResult(MESSAGE_FAIL);
return new CommandResult(String.format(MESSAGE_FAIL, userName));
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/t13g2/forum/logic/commands/LogoutCommand.java
@@ -0,0 +1,41 @@
package com.t13g2.forum.logic.commands;

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.storage.forum.Context;
E0191729 marked this conversation as resolved.
Show resolved Hide resolved
/**
*
*/
public class LogoutCommand extends Command {
public static final String COMMAND_WORD = "logout";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": logout from the forum book. ";


public static final String MESSAGE_SUCCESS = "Good bye : %1$s";
public static final String MESSAGE_FAIL = "No user logged in to Logout";
private String userName;
private Boolean canLogout;

public LogoutCommand() {
if (Context.getInstance().getCurrentUser() != null) {
this.userName = Context.getInstance().getCurrentUser().getUsername();
Context.getInstance().setCurrentUser(null);
canLogout = true;
} else {
canLogout = false;
}
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
if (canLogout) {
return new CommandResult(String.format(MESSAGE_SUCCESS, userName));
} else {
return new CommandResult(String.format(MESSAGE_FAIL, userName));
}
}
}
@@ -0,0 +1,53 @@
package com.t13g2.forum.logic.commands;

import static com.t13g2.forum.logic.parser.CliSyntax.PREFIX_USER_NAME;
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;
import com.t13g2.forum.model.person.Person;

/**
* Deletes a specific user by admin
*/
public class UpdateUserCommand extends Command {
public static final String COMMAND_WORD = "updateUser";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Update a certain user's password, email and/or phone. "
+ "Parameters: "
+ PREFIX_USER_NAME + "USER NAME "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_USER_NAME + "john ";

public static final String MESSAGE_SUCCESS = "%1$s successfully updated.";
public static final String MESSAGE_INVALID_USER = "No user named %1$s found. Please verify and try again!";

private final String userNameToUpdate = "";
private final Person userToUpdate;

/**
* Creates an DeleteUserCommand to delete the specified {@code userName}.
*/
public UpdateUserCommand(Person personToUpdate) {
requireNonNull(personToUpdate);
this.userToUpdate = personToUpdate;
}
@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);
}
User userToDelete = model.doesUserExist(userNameToUpdate);
if (userToDelete == null) {
throw new CommandException(String.format(MESSAGE_INVALID_USER, userNameToUpdate));
} else {

//model.updatePerson(userNameToUpdate);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, userNameToUpdate));
}
}
Expand Up @@ -26,6 +26,7 @@
import com.t13g2.forum.logic.commands.ListCommand;
import com.t13g2.forum.logic.commands.ListModuleCommand;
import com.t13g2.forum.logic.commands.LoginCommand;
import com.t13g2.forum.logic.commands.LogoutCommand;
import com.t13g2.forum.logic.commands.RedoCommand;
import com.t13g2.forum.logic.commands.SelectCommand;
import com.t13g2.forum.logic.commands.SelectModuleCommand;
Expand Down Expand Up @@ -140,6 +141,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddUserCommand.COMMAND_WORD:
return new AddUserCommandParser().parse(arguments);

case LogoutCommand.COMMAND_WORD:
return new LogoutCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down