interstellar_ai/app/backend/database.ts

153 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-09-25 09:10:14 +02:00
import axios from "axios";
// Construct the base API URL based on the environment
const apiURL = new URL("http://localhost:5000/interstellar_ai/db");
2024-10-07 09:17:31 +02:00
if (typeof window !== 'undefined') {
apiURL.hostname = window.location.hostname; // Set hostname for browsers
2024-10-07 09:17:31 +02:00
} else {
apiURL.hostname = "localhost"; // Default to localhost for non-browser environments
2024-10-07 09:17:31 +02:00
}
// Function to send data to the database and return a success status
2024-10-07 08:57:34 +02:00
export const sendToDatabase = async (data: object): Promise<boolean> => {
2024-09-30 15:26:31 +02:00
try {
const response = await axios.post(apiURL.href, data);
2024-09-30 15:26:31 +02:00
const status = response.data.status;
const success = response.data.response;
postMessage({ status, success }); // Send status back to the main thread
return success; // Return success status
2024-09-30 15:26:31 +02:00
} catch (error) {
postMessage({ status: 500, success: false }); // Handle errors
console.log(error);
return false; // Return false on error
2024-09-30 15:26:31 +02:00
}
};
// Function to send data and get a string response
2024-10-07 08:57:34 +02:00
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
2024-10-01 15:34:43 +02:00
try {
const response = await axios.post(apiURL.href, data);
2024-10-01 15:34:43 +02:00
const status = response.data.status;
const success = response.data.response;
postMessage({ status, success });
return success; // Return response string
2024-10-01 15:34:43 +02:00
} catch (error) {
postMessage({ status: 500, success: false });
console.log(error);
return "false"; // Return "false" on error
2024-10-01 15:34:43 +02:00
}
};
// Functions for each action
export const createAccount = async (username: string, email: string, password: string) => {
const data = {
action: "create_account",
2024-09-30 15:26:31 +02:00
username: username,
email: email,
password: password,
};
return await sendToDatabase(data); // Send account creation request
};
export const changePassword = async (usernameOrEmail: string, password: string, newPassword: string) => {
const data = {
action: "change_password",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
new_password: newPassword,
};
return await sendToDatabase(data); // Send password change request
};
2024-10-07 16:41:31 +02:00
export const getSettings = async (usernameOrEmail: string, password: string) => {
const data = {
2024-10-02 14:14:20 +02:00
action: "get_settings",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
return await sendToDatabaseAndGetString(data); // Get user settings
2024-10-01 15:34:43 +02:00
};
2024-10-07 16:41:31 +02:00
export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => {
2024-10-01 15:34:43 +02:00
const data = {
2024-10-07 16:41:31 +02:00
action: "change_settings",
2024-10-01 15:34:43 +02:00
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
2024-10-07 16:41:31 +02:00
data: newData,
2024-10-01 15:34:43 +02:00
};
return await sendToDatabase(data); // Send settings change request
2024-10-01 15:34:43 +02:00
};
2024-10-07 16:41:31 +02:00
export const getHistory = async (usernameOrEmail: string, password: string) => {
2024-10-01 15:34:43 +02:00
const data = {
2024-10-07 16:41:31 +02:00
action: "get_history",
2024-10-01 15:34:43 +02:00
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
return await sendToDatabaseAndGetString(data); // Get user history
};
2024-10-07 16:41:31 +02:00
export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => {
const data = {
2024-10-07 16:41:31 +02:00
action: "change_history",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
data: newData,
};
return await sendToDatabase(data); // Send history change request
};
2024-10-07 16:41:31 +02:00
export const getEmail = async (usernameOrEmail: string, password: string): Promise<string> => {
const data = {
action: "get_email",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
return await sendToDatabaseAndGetString(data); // Get user email
2024-10-07 16:41:31 +02:00
};
export const getName = async (usernameOrEmail: string, password: string): Promise<string> => {
const data = {
action: "get_name",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
return await sendToDatabaseAndGetString(data); // Get user name
2024-10-07 16:41:31 +02:00
};
export const checkCredentials = async (usernameOrEmail: string, password: string) => {
const data = {
action: "check_credentials",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
const sendBack = await sendToDatabase(data); // Check user credentials
2024-10-01 15:34:43 +02:00
if (sendBack) {
2024-10-07 11:16:51 +02:00
if (typeof localStorage !== 'undefined') {
// Store user data in localStorage if credentials are valid
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password));
localStorage.setItem("accountName", await getName(usernameOrEmail, password));
localStorage.setItem("accountPassword", password);
2024-10-07 11:16:51 +02:00
}
2024-10-01 15:34:43 +02:00
}
return sendBack; // Return success status
};
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
const data = {
action: "delete_account",
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
password,
};
return await sendToDatabase(data); // Send account deletion request
};