Skip to content

Commit

Permalink
fix: threaded acc creation and login, error handling offlineUI
Browse files Browse the repository at this point in the history
  • Loading branch information
sprestrelski committed Dec 5, 2023
1 parent 75aede7 commit f402f31
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 71 deletions.
118 changes: 72 additions & 46 deletions app/src/main/java/code/client/Controllers/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.net.URL;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
Expand Down Expand Up @@ -116,14 +117,12 @@ private void handleRecipePostButton(ActionEvent event) throws IOException {
recipeWriter.writeRecipe(postedRecipe);

String recipe = writer.toString();
// Debugging
// System.out.println("Posting: " + recipe);

String response = model.performRecipeRequest("POST", recipe, null);
if (response.contains("Offline")) {

AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
} else if (response.contains("Error")) {

AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
}
}

Expand Down Expand Up @@ -359,7 +358,6 @@ private void handleDetailedViewListeners() {
try {
handleRefreshButton(event);
} catch (URISyntaxException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
Expand Down Expand Up @@ -460,7 +458,6 @@ private void handleRefreshButton(ActionEvent event) throws URISyntaxException, I
new Runnable() {
@Override
public void run() {

String responseText = model.performChatGPTRequest("GET", mealType,
ingredients);
Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
Expand All @@ -476,20 +473,6 @@ public void run() {
});
thread.start();

// AppAlert.show("Loading", "Please wait for the recipe to regenerate.");
// Thread.sleep(2000);
// String responseText = model.performChatGPTRequest("GET", mealType,
// ingredients);
// Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
// chatGPTrecipe.setAccountId(account.getId());
// chatGPTrecipe.setImage(model.performDallERequest("GET",
// chatGPTrecipe.getTitle()));

// // Changes UI to Detailed Recipe Screen
// view.goToDetailedView(chatGPTrecipe, false);
// view.getDetailedView().getRecipeDetailsUI().setEditable(false);
// handleDetailedViewListeners();

} catch (Exception exception) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
exception.printStackTrace();
Expand All @@ -509,17 +492,42 @@ private void handleCreateAcc(ActionEvent event) {
if (username.isEmpty() || password.isEmpty()) {
// Display an error message if username or password is empty
showErrorPane(grid, "Error. Please provide a username and password.");
} else if (isUsernameTaken(username, password)) {
// Display an error message if the username is already taken
showErrorPane(grid, "Error. This username is already taken. Please choose another one.");
} else {
// Continue with account creation logic
System.out.println("Account Created!\nUsername: " + username + "\nPassword: " + password);
model.performAccountRequest("PUT", username, password);
// Show success message
showSuccessPane(grid);
view.goToLoginUI();
view.goToLoading();
Thread thread = new Thread(
new Runnable() {
@Override
public void run() {
if (!isUsernameTaken(username, password)
&& !view.getMainScene().equals(view.getOfflineUI())) {
// Continue with account creation logic
System.out.println(
"Username not taken!\nUsername: " + username + "\nPassword: " + password);
String response = model.performAccountRequest("PUT", username, password);
// Show success message
if (response.contains("Offline")) {
view.goToOfflineUI();
} else {
showSuccessPane(grid);
view.goToLoginUI();
}

} else {
// Display an error message if the username is already taken
view.goToCreateAcc();
Platform.runLater(new Runnable() {
@Override
public void run() {
showErrorPane(grid,
"Error. This username is already taken. Please choose another one.");
}
});
}
}
});
thread.start();
}

}

private void showErrorPane(GridPane grid, String errorMessage) {
Expand Down Expand Up @@ -552,13 +560,12 @@ private void showSuccessPane(GridPane grid) {

private boolean isUsernameTaken(String username, String password) {
// Check if the username is already taken
// temporary logic, no database yet
String response = model.performAccountRequest("GET", username, password);
if (response.contains("Offline")) {
view.goToOfflineUI();
return true;
}
// System.out.println("Response for usernameTaken : " + response);
return (response.equals("Username is taken"));
return (!response.equals("Username is not found"));
}
////////////////////////////////////////

Expand All @@ -571,20 +578,39 @@ private void handleLoginButton(ActionEvent event) {
// Display an error message if username or password is empty
showErrorPane(grid, "Error. Please provide a username and password.");
} else {
boolean loginSuccessful = performLogin(username, password);

if (loginSuccessful) {
showLoginSuccessPane(grid, true); // useless

goToRecipeList();
if (!view.getLoginUI().getRememberLogin()) {
clearCredentials();
} else {
saveCredentials(account);
}
} else {
showLoginSuccessPane(grid, false);
}
view.goToLoading();
Thread thread = new Thread(
new Runnable() {
@Override
public void run() {
boolean loginSuccessful = performLogin(username, password);
if (loginSuccessful) {
Platform.runLater(new Runnable() {
@Override
public void run() {
goToRecipeList();
if (!view.getLoginUI().getRememberLogin()) {
clearCredentials();
} else {
saveCredentials(account);
}
}
});

} else {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (!view.getMainScene().equals(view.getOfflineUI())) {
view.goToLoginUI();
showLoginSuccessPane(grid, false);
}
}
});
}
}
});
thread.start();
}
}

Expand Down
26 changes: 8 additions & 18 deletions app/src/main/java/code/client/Model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import java.nio.file.*;
import java.net.URLEncoder;
import com.mongodb.MongoException;
import com.mongodb.MongoSocketReadException;
import com.mongodb.MongoWriteException;

public class Model {
public String performAccountRequest(String method, String user, String password) {
String response = "Error";
try {
String urlString = AppConfig.SERVER_URL + AppConfig.ACCOUNT_PATH;
urlString += "?=" + user + ":" + password;
Expand All @@ -40,23 +42,18 @@ public String performAccountRequest(String method, String user, String password)
}

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = in.readLine();
response = in.readLine();
in.close();
return response;
} catch (MongoWriteException ex) {
ex.printStackTrace();
return "Duplicate Key Error";
} catch (MongoException ex) {
ex.printStackTrace();
return "Server Offline";
} catch (Exception ex) {
ex.printStackTrace();
return "Error: " + ex.getMessage();
response = "Error: " + ex.getMessage();
}
return response;
}

public String performRecipeRequest(String method, String recipe, String userId) {
// Implement your HTTP request logic here and return the response
String response = "Error";
try {
String urlString = AppConfig.SERVER_URL + AppConfig.RECIPE_PATH;
if (userId != null) {
Expand All @@ -76,23 +73,16 @@ public String performRecipeRequest(String method, String recipe, String userId)
}

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = "";
String line;
while ((line = in.readLine()) != null) {
response += line + "\n";
}
in.close();
return response;
} catch (MongoWriteException ex) {
ex.printStackTrace();
return "Duplicate Key Error";
} catch (MongoException ex) {
ex.printStackTrace();
return "Server Offline";
} catch (Exception ex) {
ex.printStackTrace();
return "Error: " + ex.getMessage();
response = "Error: " + ex.getMessage();
}
return response;
}

public String performChatGPTRequest(String method, String mealType, String ingredients) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/code/client/View/DetailsAppFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public DetailsAppFrame() {
detailedUI.setStyle("-fx-background-color: #F0F8FF;");
setupGrowingUI();

defaultButtonStyle = "-fx-font-style: italic; -fx-background-color: #FFFFFF; -fx-font-weight: bold; -fx-font: 11 arial;";
onStyle = "-fx-font-style: italic; -fx-background-color: #90EE90; -fx-font-weight: bold; -fx-font: 11 arial;";
offStyle = "-fx-font-style: italic; -fx-background-color: #FF7377; -fx-font-weight: bold; -fx-font: 11 arial;";
defaultButtonStyle = "-fx-font: italic 11 arial; -fx-background-color: #FFFFFF; -fx-font-weight: bold;";
onStyle = "-fx-font: italic 11 arial; -fx-background-color: #90EE90; -fx-font-weight: bold;";
offStyle = "-fx-font: italic 11 arial; -fx-background-color: #FF7377; -fx-font-weight: bold;";

backToHomeButton = new Button("Back to List");
backToHomeButton.setStyle(defaultButtonStyle);
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/code/client/View/MealTagStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ public static void styleTags(Recipe recipe, Button mealType) {
switch (recipe.getMealTag().toLowerCase()) {
case "breakfast":
mealType.setStyle(
"-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #FF7276; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
"-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #FF7276; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
mealType.setText("Breakfast");
break;

case "lunch":
mealType.setStyle(
"-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FFFF; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
"-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FFFF; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
mealType.setText("Lunch");
break;

case "dinner":
mealType.setStyle(
"-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FF00; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
"-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FF00; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;");
mealType.setText("Dinner");
break;
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/code/client/View/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URISyntaxException;

import code.server.Recipe;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class View {
Expand All @@ -26,6 +27,10 @@ public View() throws IOException, URISyntaxException {
loadingUI = new LoadingUI();
}

public Parent getMainScene() {
return mainScene.getRoot();
}

public void setScene(Scene scene) {
mainScene = scene;
}
Expand Down Expand Up @@ -59,6 +64,10 @@ public void goToOfflineUI() {
mainScene.setRoot(offlineScreen);
}

public OfflineUI getOfflineUI() {
return offlineScreen;
}

public RecipeListUI getRecipeButtons() {
return home.getRecipeList();
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/code/server/AccountRequestHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package code.server;

import com.mongodb.MongoSocketReadException;
import com.mongodb.MongoTimeoutException;
import com.mongodb.MongoWriteException;
import com.sun.net.httpserver.*;

import java.io.*;
Expand Down Expand Up @@ -30,7 +32,10 @@ public void handle(HttpExchange httpExchange) throws IOException {
} else {
throw new Exception("Not valid request method.");
}
} catch (MongoTimeoutException e) {
} catch (MongoWriteException ex) {
ex.printStackTrace();
response = "Duplicate Key Error";
} catch (MongoSocketReadException | MongoTimeoutException e) {
response = "Server Offline";
} catch (Exception e) {
response = "Error";
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/code/server/RecipeRequestHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package code.server;

import com.mongodb.MongoSocketReadException;
import com.mongodb.MongoWriteException;
import com.sun.net.httpserver.*;

import code.client.Model.RecipeCSVWriter;
Expand Down Expand Up @@ -31,7 +33,14 @@ public void handle(HttpExchange httpExchange) throws IOException {
} else {
throw new Exception("Not valid request method.");
}
} catch (MongoWriteException ex) {
ex.printStackTrace();
response = "Duplicate Key Error";
} catch (MongoSocketReadException ex) {
ex.printStackTrace();
response = "Server Offline";
} catch (Exception e) {
response = "Error";
System.out.println("An erroneous request");
e.printStackTrace();
}
Expand Down

0 comments on commit f402f31

Please sign in to comment.