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

Dev: prevents server error if no resources are loaded from backend, updates navbar to semantic design #199

Merged
merged 19 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7d0b5e7
chore: removes the migration of user_profile and user_account for exi…
arnoldknott Feb 5, 2025
9b68aa3
chore: fizes linting
arnoldknott Feb 5, 2025
dbd4020
chore:fixes showing error page, when no resources are found
arnoldknott Feb 10, 2025
cc41479
chore: removes the error catching from the api class, leave to each s…
arnoldknott Feb 10, 2025
82e9c72
chore: fixes fomrating and linting
arnoldknott Feb 10, 2025
aecefdc
chore: adds layout for reworked nav bar
arnoldknott Feb 10, 2025
c066324
chore: fixes formating and linting
arnoldknott Feb 10, 2025
17cea7d
chore: adds theming menu to user dropdown in layout
arnoldknott Feb 10, 2025
6bad570
chore: fixes formating and linting
arnoldknott Feb 10, 2025
a1dd82b
chore: adds logInOut button to new layout
arnoldknott Feb 11, 2025
440aee5
chore: fixes formating and linting
arnoldknott Feb 11, 2025
871b021
chore: adds center element to nvabar and cleans up many items
arnoldknott Feb 12, 2025
a8feb01
chore: renames protected url into dashboard
arnoldknott Feb 12, 2025
3b585ca
chore: adds formating to title
arnoldknott Feb 12, 2025
a8af604
chore: adds shadow to new login and logout button template
arnoldknott Feb 12, 2025
c4bdac9
chore: updates navbar to new layout, including responsitivity to mate…
arnoldknott Feb 12, 2025
25b8801
chore: fixes responsibility and placement inside navbar
arnoldknott Feb 12, 2025
9c21f70
chore: fixes logo on landing page and formating and linting
arnoldknott Feb 12, 2025
576a0cc
chore: fixes linting
arnoldknott Feb 12, 2025
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
93 changes: 46 additions & 47 deletions backendAPI/src/core/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from sqlmodel.ext.asyncio.session import AsyncSession

from core.config import config
from sqlmodel import select
from models.identity import User, UserAccount, UserProfile
from models.access import IdentifierTypeLink, IdentityType

# from sqlmodel import SQLmodel # noqa: F401

Expand All @@ -24,51 +21,53 @@ async def get_async_session() -> AsyncSession:
# Run extraordinary migrations:
# Comment when propagated all the way into production!
async def run_migrations():
session = await get_async_session()
# async with get_async_session() as session:
response = await session.exec(select(User))
users = response.unique().fetchall()
for user in users:
# # The settings in user account and user profile cannot be changed, before the user is created.
try:
query = select(UserAccount, UserProfile).where(
UserAccount.user_id == user.id, UserProfile.user_id == user.id
)
response = await session.exec(query)
existing_account_and_profile = response.unique().fetchall()
print("=== existing_account_and_profile ===")
print(existing_account_and_profile)
if not existing_account_and_profile:
print("== no account or profile found - trying to generate it ===")
user_account = UserAccount(user_id=user.id)
user_profile = UserProfile(user_id=user.id)
account_type_link = IdentifierTypeLink(
id=user_account.id, type=IdentityType.user_account
)
profile_type_link = IdentifierTypeLink(
id=user_profile.id, type=IdentityType.user_profile
)
user_account = UserAccount.model_validate(user_account)
user_profile = UserProfile.model_validate(user_profile)
user.user_account_id = user_account.id
user.user_profile_id = user_profile.id
pass
# Did not work in production!
# session = await get_async_session()
# # async with get_async_session() as session:
# response = await session.exec(select(User))
# users = response.unique().fetchall()
# for user in users:
# # # The settings in user account and user profile cannot be changed, before the user is created.
# try:
# query = select(UserAccount, UserProfile).where(
# UserAccount.user_id == user.id, UserProfile.user_id == user.id
# )
# response = await session.exec(query)
# existing_account_and_profile = response.unique().fetchall()
# print("=== existing_account_and_profile ===")
# print(existing_account_and_profile)
# if not existing_account_and_profile:
# print("== no account or profile found - trying to generate it ===")
# user_account = UserAccount(user_id=user.id)
# user_profile = UserProfile(user_id=user.id)
# account_type_link = IdentifierTypeLink(
# id=user_account.id, type=IdentityType.user_account
# )
# profile_type_link = IdentifierTypeLink(
# id=user_profile.id, type=IdentityType.user_profile
# )
# user_account = UserAccount.model_validate(user_account)
# user_profile = UserProfile.model_validate(user_profile)
# user.user_account_id = user_account.id
# user.user_profile_id = user_profile.id

session.add(user_account)
session.add(user_profile)
session.add(account_type_link)
session.add(profile_type_link)
session.add(user)
await session.commit()
await session.refresh(user_account)
await session.refresh(user_profile)
await session.refresh(account_type_link)
await session.refresh(profile_type_link)
await session.refresh(user)
except Exception as error:
print("Error in run_migrations:")
print(error)
await session.rollback()
await session.close()
# session.add(user_account)
# session.add(user_profile)
# session.add(account_type_link)
# session.add(profile_type_link)
# session.add(user)
# await session.commit()
# await session.refresh(user_account)
# await session.refresh(user_profile)
# await session.refresh(account_type_link)
# await session.refresh(profile_type_link)
# await session.refresh(user)
# except Exception as error:
# print("Error in run_migrations:")
# print(error)
# await session.rollback()
# await session.close()


# engine = create_engine(config.POSTGRES_URL.unicode_string())
Expand Down
157 changes: 79 additions & 78 deletions frontend_svelte/src/lib/server/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ class BaseAPI {
});
}

private errorHandler(error: unknown): Response {
console.error(error);
if (error instanceof Response) {
return error;
}
return new Response(`Error accessing ${this.apiBaseURL}`, { status: 500 });
}
// private errorHandler(error: unknown): Response {
// console.log('=== src - lib - server - apis - errorHandler ===');
// console.error(error);
// if (error instanceof Response) {
// return error;
// }
// return new Response(`Error accessing ${this.apiBaseURL}`, { status: 500 });
// }

async post(
session_id: string,
Expand All @@ -56,33 +57,33 @@ class BaseAPI {
options: RequestInit = {},
headers: HeadersInit = {}
): Promise<Response> {
try {
const accessToken = await this.oauthProvider.getAccessToken(session_id, scopes);
// options.body = JSON.stringify(body);
if (body instanceof FormData) {
options.body = JSON.stringify(Object.fromEntries(body));
} else if (typeof body === 'string') {
options.body = body;
} else {
console.error('Invalid body type or not yet implemented: ' + typeof body);
throw new Error('Invalid body type');
}
// options.body = body;
options.method = 'POST';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// const response = await fetch(`${this.apiBaseURL}${path}`, {
// method: 'POST',
// headers: {
// Authorization: `Bearer ${accessToken}`,
// 'Content-Type': "application/json"
// },
// body: JSON.stringify(body)
// });
// return response
} catch (error) {
return this.errorHandler(error);
// try {
const accessToken = await this.oauthProvider.getAccessToken(session_id, scopes);
// options.body = JSON.stringify(body);
if (body instanceof FormData) {
options.body = JSON.stringify(Object.fromEntries(body));
} else if (typeof body === 'string') {
options.body = body;
} else {
console.error('Invalid body type or not yet implemented: ' + typeof body);
throw new Error('Invalid body type');
}
// options.body = body;
options.method = 'POST';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// const response = await fetch(`${this.apiBaseURL}${path}`, {
// method: 'POST',
// headers: {
// Authorization: `Bearer ${accessToken}`,
// 'Content-Type': "application/json"
// },
// body: JSON.stringify(body)
// });
// return response
// } catch (error) {
// return this.errorHandler(error);
// }
}

async get(
Expand All @@ -92,20 +93,20 @@ class BaseAPI {
options: RequestInit,
headers: HeadersInit
): Promise<Response> {
try {
const accessToken = await this.oauthProvider.getAccessToken(sessionId, scopes);
options.method = 'GET';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// const response = await fetch(`${this.apiBaseURL}${path}`, {
// headers: {
// Authorization: `Bearer ${accessToken}`
// }
// });
// return response;
} catch (error) {
return this.errorHandler(error);
}
// try {
const accessToken = await this.oauthProvider.getAccessToken(sessionId, scopes);
options.method = 'GET';
const request = this.constructRequest(path, accessToken, options, headers);
const response = await fetch(request);
// if (response.status !== 200) {
// console.error('=== src - lib - server - apis - get - response.status !== 200 ===');
// console.error(response.status)
// error(response.status, {message: 'Error accessing ' + this.apiBaseURL + path });
// }
return response;
// } catch (error) {
// return this.errorHandler(error);
// }
}

async put(
Expand All @@ -116,22 +117,22 @@ class BaseAPI {
options: RequestInit = {},
headers: HeadersInit = {}
): Promise<Response> {
try {
const accessToken = await this.oauthProvider.getAccessToken(session_id, scopes);
if (body instanceof FormData) {
options.body = JSON.stringify(Object.fromEntries(body));
} else if (typeof body === 'string') {
options.body = body;
} else {
console.error('Invalid body type or not yet implemented: ' + typeof body);
throw new Error('Invalid body type');
}
options.method = 'PUT';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
} catch (error) {
return this.errorHandler(error);
// try {
const accessToken = await this.oauthProvider.getAccessToken(session_id, scopes);
if (body instanceof FormData) {
options.body = JSON.stringify(Object.fromEntries(body));
} else if (typeof body === 'string') {
options.body = body;
} else {
console.error('Invalid body type or not yet implemented: ' + typeof body);
throw new Error('Invalid body type');
}
options.method = 'PUT';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// } catch (error) {
// return this.errorHandler(error);
// }
}

async delete(
Expand All @@ -141,22 +142,22 @@ class BaseAPI {
options: RequestInit,
headers: HeadersInit
): Promise<Response> {
try {
const accessToken = await this.oauthProvider.getAccessToken(sessionId, scopes);
options.method = 'DELETE';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// const response = await fetch(`${this.apiBaseURL}${path}`, {
// headers: {
// Authorization: `Bearer ${accessToken}`
// }
// });
// return response;
} catch (error) {
console.error('=== src - lib - server - apis - delete - error ===');
console.error(error);
return this.errorHandler(error);
}
// try {
const accessToken = await this.oauthProvider.getAccessToken(sessionId, scopes);
options.method = 'DELETE';
const request = this.constructRequest(path, accessToken, options, headers);
return await fetch(request);
// const response = await fetch(`${this.apiBaseURL}${path}`, {
// headers: {
// Authorization: `Bearer ${accessToken}`
// }
// });
// return response;
// } catch (error) {
// console.error('=== src - lib - server - apis - delete - error ===');
// console.error(error);
// return this.errorHandler(error);
// }
}
}

Expand Down
Loading