async def delete_article( id: int, is_superuser: bool = Depends(block_not_super), articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> None: return await articles_db_repo.delete_article(id=id)
async def create_subgroup( new_subgroup: SubgroupCreateModel = Body(..., embed=True), is_superuser: bool = Depends(block_not_super), articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> SubgroupInDBModel: return await articles_db_repo.insert_subgroup(subgroup=new_subgroup)
async def update_article( updated_article: ArticleUpdateModel = Body(..., embed=True), is_superuser: bool = Depends(block_not_super), articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> ArticleInDBModel: return await articles_db_repo.update_article(article=updated_article)
async def login( user: PostUserLoginModel = Body(..., embed=True), user_db_repo: UsersDBRepository = Depends( get_database_repo(UsersDBRepository)), ) -> PublicUserInDB: user = await user_db_repo.authenticate_user(username=user.username, password=user.password) if not user: raise HTTPException( status_code=HTTP_401_UNAUTHORIZED, detail="Authentication was unsuccessful.", headers={"WWW-Authenticate": "Beared"}, ) access_token = AccessToken( access_token=auth_service.create_access_token_for_user(user=user), token_type="bearer") # TODO: Blacklist old token and add new await user_db_repo.update_token(user_id=user.id, token=access_token.access_token) user.jwt = access_token return PublicUserInDB(**user.dict())
async def register_new_user( new_user: PostUserModel = Body(..., embed=True), user_db_repo: UsersDBRepository = Depends( get_database_repo(UsersDBRepository)), ) -> PublicUserInDB: response = await user_db_repo.create_user(new_user=new_user) return response
async def delete_home( id: int, is_superuser: bool = Depends(block_not_super), public_db_repo: PublicDBRepository = Depends(get_database_repo(PublicDBRepository)), cloud_repo: BaseCloudRepository = Depends(get_cloud_repository(BaseCloudRepository)), ) -> None: deleted = await public_db_repo.delete_home(id=id) cloud_repo.delete_keys(list_of_keys=[{"Key": deleted}])
async def create_about( is_superuser: bool = Depends(block_not_super), new_about: AboutCreateModel = Body(..., embed=True), cloud_repo: BaseCloudRepository = Depends(get_cloud_repository(BaseCloudRepository)), public_db_repo: PublicDBRepository = Depends(get_database_repo(PublicDBRepository)), ) -> AboutInDBModel: new_about.image_url = \ cloud_repo.get_sharing_link_from_key(key=new_about.image_key)\ if new_about.image_key else None response = await public_db_repo.insert_about(about=new_about) return response
async def update_home( is_superuser: bool = Depends(block_not_super), updated_home: HomeUpdateModel = Body(..., embed=True), public_db_repo: PublicDBRepository = Depends(get_database_repo(PublicDBRepository)), cloud_repo: BaseCloudRepository = Depends(get_cloud_repository(BaseCloudRepository)), ) -> HomeInDBModel: updated_home.image_url = \ cloud_repo.get_sharing_link_from_key(key=updated_home.image_key)\ if updated_home.image_key else None response = await public_db_repo.update_home(home=updated_home) if response: cloud_repo.delete_keys(list_of_keys=[{"Key": updated_home.old_key}]) return response
async def get_user_from_token( *, token: str, user_repo: UsersDBRepository = Depends(get_database_repo(UsersDBRepository)), ) -> Optional[UserInDB]: try: username = auth_service.get_username_from_token(token=token, secret_key=str(SECRET_KEY)) user = await user_repo.get_user_by_username(username=username) if not user: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=f"User not found for username {username}") # check if token is blocked and return Auth error if blocked true_token = await user_repo.get_token_from_db(user_id=user.id) if true_token != token: raise HTTPException(status_code=401, detail="Token blocked. Another session started. Login required.") except HTTPException as http: raise HTTPException(status_code=401, detail=f"{http.detail}") except Exception as e: raise HTTPException(status_code=400, detail=f"{e.detail}") return user
async def get_article( id: int, articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> ArticleInDBModel: "Get article by ID" return await articles_db_repo.select_article_by_id(id=id)
async def get_articles( group_fk: int, articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> List[ArticlePreviewModel]: "Get all awailable articles for subgroup (names and ID's)" return await articles_db_repo.select_articles_preview(group_fk=group_fk)
async def get_subgroup( articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> List[SubgroupInDBModel]: return await articles_db_repo.select_subgroup()
async def get_subgroup_id( url: str, articles_db_repo: ArticlesDBRepository = Depends(get_database_repo(ArticlesDBRepository)), ) -> int: return await articles_db_repo.select_subgroup_id(url=url)
async def get_home( public_db_repo: PublicDBRepository = Depends(get_database_repo(PublicDBRepository)), ) -> List[HomeInDBModel]: return await public_db_repo.get_home()
async def get_about( public_db_repo: PublicDBRepository = Depends(get_database_repo(PublicDBRepository)), ) -> List[AboutInDBModel]: return await public_db_repo.get_about()