MIH Profile Links

This commit is contained in:
2026-05-27 15:36:56 +02:00
parent 33d07b1617
commit 052f937027
30 changed files with 2240 additions and 499 deletions
+128
View File
@@ -0,0 +1,128 @@
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel
from typing import List
#from ..mih_database import dbConnection
import mih_database
import mih_database.mihDbConnections
from mih_database.mihDbObjects import ProfileLink
from sqlalchemy import select, insert, delete, CursorResult
from sqlalchemy.orm import Session
#SuperToken Auth from front end
from supertokens_python.recipe.session.framework.fastapi import verify_session
from supertokens_python.recipe.session import SessionContainer
from fastapi import Depends
router = APIRouter()
class ProfileLinkResponse(BaseModel):
idprofile_links: int
app_id: str
business_id: str
site_name: str
custom_name: str
destination: str
order: int
class Config:
from_attributes = True
class profileLinkInsertRequest(BaseModel):
app_id: str
business_id: str
site_name: str
custom_name: str
destination: str
order:int
class profileLinkDeletRequest(BaseModel):
idprofile_links: int
class profileLinkUpdateRequest(BaseModel):
idprofile_links: int
site_name: str
custom_name: str
destination: str
order:int
def get_db():
dbEngine = mih_database.mihDbConnections.dbAllConnect()
with Session(dbEngine) as session:
yield session
@router.get("/profile-links/user/{app_id}", response_model=List[ProfileLinkResponse], tags=["Profile Links"])
async def getUserProfileLinks(
app_id: str,
dbSession: Session = Depends(get_db),
# session: SessionContainer = Depends(verify_session())
):
queryStatement = select(ProfileLink).where(ProfileLink.app_id == app_id).order_by(ProfileLink.order)
queryResults = dbSession.execute(queryStatement).scalars().all()
return queryResults
@router.get("/profile-links/business/{business_id}", response_model=List[ProfileLinkResponse], tags=["Profile Links"])
async def getBusinessProfileLinks(
business_id: str,
dbSession: Session = Depends(get_db),
# session: SessionContainer = Depends(verify_session())
):
queryStatement = select(ProfileLink).where(ProfileLink.business_id == business_id).order_by(ProfileLink.order)
queryResults = dbSession.execute(queryStatement).scalars().all()
return queryResults
@router.post("/profile-links/insert/", status_code=201, tags = ["Profile Links"])
async def addNewProfileLink(
insertItem: profileLinkInsertRequest,
dbSession: Session = Depends(get_db),
session: SessionContainer = Depends(verify_session())
):
queryStatement = insert(ProfileLink).values(
app_id = insertItem.app_id,
business_id = insertItem.business_id,
site_name = insertItem.site_name,
custom_name = insertItem.custom_name,
destination = insertItem.destination,
order = insertItem.order
)
dbSession.execute(queryStatement)
dbSession.commit()
return {"message": "Successfully Created Record"}
@router.delete("/profile-links/delete/", tags=["Profile Links"])
async def deleteProfileLink(
deleteItem: profileLinkDeletRequest,
dbSession: Session = Depends(get_db),
session: SessionContainer = Depends(verify_session())
):
queryStatement = select(ProfileLink).where(ProfileLink.idprofile_links == deleteItem.idprofile_links)
profileLink = dbSession.execute(queryStatement).scalar_one_or_none()
if not profileLink:
raise HTTPException(status_code=404, detail="Record not found")
dbSession.delete(profileLink)
dbSession.execute(queryStatement)
dbSession.commit()
return {"message": "Successfully Deleted Record"}
@router.put("/profile-links/update/", tags=["Profile Links"])
async def updateProfileLink(
updateItem: profileLinkUpdateRequest,
dbSession: Session = Depends(get_db),
session: SessionContainer = Depends(verify_session())
):
queryStatement = select(ProfileLink).where(ProfileLink.idprofile_links == updateItem.idprofile_links)
profileLink = dbSession.execute(queryStatement).scalar_one_or_none()
if not profileLink:
raise HTTPException(status_code=404, detail="Link not found")
profileLink.site_name = updateItem.site_name
profileLink.custom_name = updateItem.custom_name
profileLink.destination = updateItem.destination
profileLink.order = updateItem.order
dbSession.commit()
return {"message": "Successfully Updated Record"}
+28
View File
@@ -136,6 +136,34 @@ async def read_all_users(username: str, session: SessionContainer = Depends(veri
db.close()
return {"available": available}
# Get List of all files
@router.get("/user/username/{username}", tags=["MIH Users"])
async def read_users_by_username(username: str,
# session: SessionContainer = Depends(verify_session()),
):
db = mih_database.dbConnection.dbAppDataConnect()
cursor = db.cursor()
# query = "SELECT * FROM users where username = %s"
query = "SELECT * FROM users WHERE LOWER(username) = LOWER(%s)"
cursor.execute(query, (username,))
items = [
{
"idUser": item[0],
"email": item[1],
"fname": item[2],
"lname": item[3],
"type": item[4],
"app_id": item[5],
"username": item[6],
"pro_pic_path": item[7],
"purpose": item[8],
}
for item in cursor.fetchall()
]
cursor.close()
db.close()
return items[0]
# Get List of all files
@router.get("/user/{app_id}", tags=["MIH Users"])
async def read_users_by_app_id(app_id: str, session: SessionContainer = Depends(verify_session())):