forked from nusCS2113-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from E0191729/master
1) Added the PPP for E0191729
- Loading branch information
Showing
10 changed files
with
188 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/t13g2/forum/logic/commands/UserDeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.t13g2.forum.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.t13g2.forum.commons.core.EventsCenter; | ||
import com.t13g2.forum.commons.events.model.UserLoginEvent; | ||
import com.t13g2.forum.logic.CommandHistory; | ||
import com.t13g2.forum.logic.commands.exceptions.CommandException; | ||
import com.t13g2.forum.model.Context; | ||
import com.t13g2.forum.model.Model; | ||
import com.t13g2.forum.model.UnitOfWork; | ||
import com.t13g2.forum.model.forum.User; | ||
|
||
/** | ||
* | ||
*/ | ||
public class UserDeleteCommand extends Command { | ||
public static final String COMMAND_WORD = "deleteMe"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Self delete user from the forum book. "; | ||
|
||
public static final String MESSAGE_SUCCESS = "It was nice knowing you! Good bye %1$s :("; | ||
public static final String MESSAGE_FAIL = "No user logged in for Self Delete"; | ||
|
||
|
||
private String userName; | ||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
if (Context.getInstance().getCurrentUser() != null) { | ||
try (UnitOfWork unitOfWork = new UnitOfWork()) { | ||
User userToDelete = Context.getInstance().getCurrentUser(); | ||
userName = userToDelete.getUsername(); | ||
unitOfWork.getUserRepository().deleteUser(userToDelete); | ||
unitOfWork.commit(); | ||
Context.getInstance().setCurrentUser(null); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
EventsCenter.getInstance().post(new UserLoginEvent("", false, false)); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, userName)); | ||
} else { | ||
return new CommandResult(String.format(MESSAGE_FAIL, userName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.