Skip to content

Commit

Permalink
Merge pull request #200 from arnoldknott/stage
Browse files Browse the repository at this point in the history
Stage: removes server error on no resources and changes to nevbar with semantic design
  • Loading branch information
arnoldknott authored Feb 12, 2025
2 parents bb15cfe + 174f102 commit 914e841
Show file tree
Hide file tree
Showing 28 changed files with 836 additions and 337 deletions.
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

0 comments on commit 914e841

Please sign in to comment.