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

Remove Entities from outer layers using DTOs #26

Merged
merged 3 commits into from
Dec 2, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/Accounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public Account(String username, String password, List<Deck> decks){
}
}

/**
* Create a new Account with the given username, password, and list of decks.
* @param username Username of the account
* @param password Password of the account
* @param decks Starting decks of the account
*/
public Account(String username, String password, List<Deck> decks, Map<Deck, List<StudySession>> decksToSessions) {
this.username = username;
this.password = password;
this.decks = decks;
this.decksToSessions = decksToSessions;
}

/**
* Create a new Account with the given username and password, and an empty list of decks.
* @param username Username of the account
Expand Down Expand Up @@ -57,6 +70,13 @@ public void setUsername(String username) {
this.username = username;
}

/**
* @return This account's password
*/
public String getPassword() {
return password;
}

/**
* @return This account's deck-to-sessions mapping
*/
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/Accounts/AccountDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Accounts;

import Decks.DeckDTO;
import Sessions.StudySessionDTO;

import java.util.List;
import java.util.Map;

public class AccountDTO {

private final String username;
private final String password;
private final List<DeckDTO> decks;
private final Map<DeckDTO, List<StudySessionDTO>> decksToSessions;

public AccountDTO(String username, String password, List<DeckDTO> decks, Map<DeckDTO,
List<StudySessionDTO>> decksToSessions) {
this.username = username;
this.password = password;
this.decks = decks;
this.decksToSessions = decksToSessions;
}

/**
* @return this account's username
*/
public String getUsername() {
return username;
}

/**
* @return this account's password
*/
public String getPassword() {
return password;
}

/**
* @return this account's decks
*/
public List<DeckDTO> getDecks() {
return decks;
}

/**
* @return a mapping from a deck to its sessions owned by this account
*/
public Map<DeckDTO, List<StudySessionDTO>> getDecksToSessions() {
return decksToSessions;
}
}
191 changes: 154 additions & 37 deletions src/main/java/Accounts/AccountInteractor.java
Original file line number Diff line number Diff line change
@@ -1,94 +1,160 @@
package Accounts;

import Decks.Deck;
import Decks.DeckDTO;
import Decks.DeckInteractor;
import Flashcards.Flashcard;
import Flashcards.FlashcardData;
import Sessions.SessionInteractor;
import Sessions.StudySession;
import Sessions.StudySessionDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountInteractor {

private static Account currentAccount;

private AccountInteractor() {}

/**
* Create and return a new user account.
* @param username This account's identifying username.
* @param password This account's password.
* @return a new Account.
* Attempt to log in to the given account. Return whether the login was successful.
* @param accountDTO The target account
* @param attemptedPassword The password used to attempt a login
* @return whether login was successful
*/
public static Account createAccount(String username, String password){
return new Account(username, password);
public static boolean login(AccountDTO accountDTO, String attemptedPassword) {
Account account = convertDTOToAccount(accountDTO);
if (account.isCorrectPassword(attemptedPassword)) {
currentAccount = account;
return true;
} else {
return false;
}
}

/**
* Change this account's username to the given username.
* @param account The target account
* @param newUsername The new username of the account
* Provide relevant interactors with the ability to use the given deck.
*/
public static void changeUsername(Account account, String newUsername) {
account.setUsername(newUsername);
public static void selectDeck(DeckDTO selectedDeckDTO) {
Deck currentDeck = findDeckInCurrentAccountFromDTO(selectedDeckDTO);
DeckInteractor.setCurrentDeck(currentDeck);
}

/**
* Provide relevant interactors with the ability to use the given StudySession.
*/
public static void selectSession(DeckDTO deckDTO, StudySessionDTO sessionDTO) {
Deck currentDeck = findDeckInCurrentAccountFromDTO(deckDTO);
StudySession currentSession = findSessionInCurrentAccountFromDTO(currentDeck, sessionDTO);
SessionInteractor.setCurrentSession(currentSession);
}

/**
* @return the current account
*/
public static AccountDTO getCurrentAccount() {
return convertAccountToDTO(currentAccount);
}

/**
* Return whether the attempted password matches the account's true password.
* @param account The targeted account for login
* @param accountDTO The targeted account for login
* @param attemptedPassword The attempted password
* @return whether the attempted password matches the account's true password.
*/
public static boolean isCorrectPassword(Account account, String attemptedPassword) {
public static boolean isCorrectPassword(AccountDTO accountDTO, String attemptedPassword) {
Account account = convertDTOToAccount(accountDTO);
return account.isCorrectPassword(attemptedPassword);
}

/**
* Add a new deck to this account.
* @param account The target account
* @param deck The deck to be added
* Create and return an AccountDTO.
* @param username This account's identifying username.
* @param password This account's password.
* @return a new AccountDTO.
*/
public static AccountDTO createAccount(String username, String password) {
Account account = new Account(username, password);
return convertAccountToDTO(account);
}

/**
* Change the current account's username to the given username.
* @param newUsername The new username of the account
*/
public static void changeCurrentAccountUsername(String newUsername) {
currentAccount.setUsername(newUsername);
}

/**
* Add a new deck to the account.
* @param deckDTO The deck to be added
*/
public static void addDeckToAccount(Account account, Deck deck) {
account.addDeck(deck);
public static void addDeckToCurrentAccount(DeckDTO deckDTO) {
Deck deck = DeckInteractor.convertDTOToDeck(deckDTO);
currentAccount.addDeck(deck);
}

/**
* Delete an existing deck from this account.
* @param account The target account
* @param deck The deck to be deleted
* @param deckDTO The deck to be deleted
*/
public static void deleteDeckFromAccount(Account account, Deck deck) {
account.deleteDeck(deck);
public static void deleteDeckFromCurrentAccount(DeckDTO deckDTO) {
Deck deck = findDeckInCurrentAccountFromDTO(deckDTO);
currentAccount.getDecks().remove(deck);
}

/**
* Add a new StudySession on a deck to this account.
* @param account The target account
* @param deck The deck to which the StudySession is based on
* @param session The added StudySession
* @param deckDTO The deck to which the StudySession is based on
* @param sessionDTO The added StudySession
*/
public static void addSessionToAccount(Account account, Deck deck, StudySession session) {
account.addSession(deck, session);
public static void addSessionToCurrentAccount(DeckDTO deckDTO, StudySessionDTO sessionDTO) {
Deck deck = findDeckInCurrentAccountFromDTO(deckDTO);
currentAccount.addSession(deck, SessionInteractor.convertDTOToSession(sessionDTO));
}

/**
* Delete an existing StudySession on a deck from this account.
* @param account The target account
* @param deck The deck to which the StudySession is based on
* @param session The StudySession to be deleted
* Delete an existing StudySession on a deck from the current account.
* @param deckDTO The deck to which the StudySession is based on
* @param sessionDTO The StudySession to be deleted
*/
public static void deleteSessionFromAccount(Account account, Deck deck, StudySession session) {
account.deleteSession(deck, session);
public static void deleteSessionFromCurrentAccount(DeckDTO deckDTO, StudySessionDTO sessionDTO) {
Deck deck = findDeckInCurrentAccountFromDTO(deckDTO);
StudySession session = findSessionInCurrentAccountFromDTO(deck, sessionDTO);
currentAccount.deleteSession(deck, session);
}

/**
* Get the StudySessions linked to the given deck.
* @param deckDTO the deck whose sessions will be fetched
* @return a List of StudySessionDTOs
*/
public static List<StudySessionDTO> getSessionsOfDeck(DeckDTO deckDTO) {
Deck deck = findDeckInCurrentAccountFromDTO(deckDTO);
List<StudySession> sessions = currentAccount.getDecksToSessions().get(deck);
List<StudySessionDTO> sessionDTOs = new ArrayList<>();
for (StudySession s : sessions) {
sessionDTOs.add(SessionInteractor.convertSessionToDTO(s));
}
return sessionDTOs;
}

/**
* Adds or deletes flashcards in the flashcard-to-data mapping of each StudySession to match the current state of
* the specified deck. Should be called when a card is added or deleted from a deck.
* @param account The target account
* @param deck The deck which has had cards added or deleted
* @param deckDTO The deck which has had cards added or deleted
*/
public static void updateSessionsOfDeck(Account account, Deck deck) {
public static void updateSessionsOfDeckInCurrentAccount(DeckDTO deckDTO) {
Deck deck = findDeckInCurrentAccountFromDTO(deckDTO);

List<Flashcard> flashcardList = deck.getFlashcards();

List<StudySession> listOfSessions = account.getDecksToSessions().get(deck);
List<StudySession> listOfSessions = currentAccount.getDecksToSessions().get(deck);

for (StudySession session : listOfSessions) {
// First, check for updates needed from adding a card:
Expand All @@ -115,4 +181,55 @@ public static void updateSessionsOfDeck(Account account, Deck deck) {
}
}

public static Account convertDTOToAccount(AccountDTO accountDTO) {
String username = accountDTO.getUsername();
String password = accountDTO.getPassword();
List<Deck> decks = new ArrayList<>();
Map<Deck, List<StudySession>> decksToSessions = new HashMap<>();
for (DeckDTO deckDTO : accountDTO.getDecks()) {
Deck deck = DeckInteractor.convertDTOToDeck(deckDTO);
decks.add(deck);
List<StudySession> sessions = new ArrayList<>();
for (StudySessionDTO sessionDTO : accountDTO.getDecksToSessions().get(deckDTO)) {
StudySession session = SessionInteractor.convertDTOToSession(sessionDTO);
sessions.add(session);
}
decksToSessions.put(deck, sessions);
}
return new Account(username, password, decks, decksToSessions);
}

public static AccountDTO convertAccountToDTO(Account account) {
String username = account.getUsername();
String password = account.getPassword();
List<DeckDTO> decksDTO = new ArrayList<>();
Map<DeckDTO, List<StudySessionDTO>> decksToSessionsDTO = new HashMap<>();
for (Deck deck : account.getDecks()) {
DeckDTO deckDTO = DeckInteractor.convertDeckToDTO(deck);
decksDTO.add(deckDTO);
List<StudySessionDTO> sessionsDTO = new ArrayList<>();
for (StudySession session : account.getDecksToSessions().get(deck)) {
StudySessionDTO sessionDTO = SessionInteractor.convertSessionToDTO(session);
sessionsDTO.add(sessionDTO);
}
decksToSessionsDTO.put(deckDTO, sessionsDTO);
}
return new AccountDTO(username, password, decksDTO, decksToSessionsDTO);
}

private static Deck findDeckInCurrentAccountFromDTO(DeckDTO deckDTO) {
return currentAccount.getDecks().stream()
.filter(d -> d.getName().equals(deckDTO.getName()))
.findAny()
.orElse(null);
}

private static StudySession findSessionInCurrentAccountFromDTO(Deck deck, StudySessionDTO sessionDTO) {
StudySession selectedSession = SessionInteractor.convertDTOToSession(sessionDTO);
return currentAccount.getDecksToSessions().get(deck).stream()
.filter(session -> session.getClass().equals(selectedSession.getClass()))
.findAny()
.orElse(null);
}

}
Loading