feat: added endpoints: auth, pofile, account, keyring
This commit is contained in:
145
api/api/endpoints/account.py
Normal file
145
api/api/endpoints/account.py
Normal file
@@ -0,0 +1,145 @@
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
Body,
|
||||
Depends,
|
||||
Form,
|
||||
HTTPException,
|
||||
Request,
|
||||
Response,
|
||||
status,
|
||||
)
|
||||
|
||||
|
||||
from fastapi_jwt_auth import AuthJWT
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||
|
||||
from api.db.connection.session import get_connection_dep
|
||||
|
||||
from api.db.logic.account import get_user_id, put_user_id, post_add_user,get_user_login
|
||||
|
||||
from api.schemas.account.account import Role,Status
|
||||
from api.schemas.endpoints.account import UserUpdate
|
||||
|
||||
from api.services.access_token_validadtion import AccessTokenValidadtion
|
||||
from api.services.user_role_validation import db_user_role_validation
|
||||
from api.services.update_data_validation import put_user_data_validator
|
||||
|
||||
api_router = APIRouter(
|
||||
prefix="/account",
|
||||
tags=["User accountModel"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@api_router.get("/{user_id}")
|
||||
async def get_account(user_id: int,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends()):
|
||||
|
||||
current_user = AccessTokenValidadtion(Authorize)
|
||||
|
||||
authorize_user = await db_user_role_validation(connection, current_user)
|
||||
|
||||
user = await get_user_id(connection, user_id)
|
||||
|
||||
if user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Account not found")
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@api_router.post("/{user_id}")
|
||||
async def post_account(
|
||||
user_id: int,
|
||||
user: UserUpdate,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends()
|
||||
):
|
||||
current_user = AccessTokenValidadtion(Authorize)
|
||||
|
||||
authorize_user = await db_user_role_validation(connection, current_user)
|
||||
|
||||
user_validation = await get_user_id(connection, user_id)
|
||||
|
||||
if user_validation is None:
|
||||
|
||||
user_new = await post_add_user(connection,user,user_id,authorize_user.id)
|
||||
return user_new
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="An account with this information already exists.")
|
||||
|
||||
|
||||
|
||||
|
||||
@api_router.put("/{user_id}")
|
||||
async def put_account(
|
||||
user_id: int,
|
||||
user_update: UserUpdate,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends()
|
||||
):
|
||||
current_user = AccessTokenValidadtion(Authorize)
|
||||
|
||||
authorize_user = await db_user_role_validation(connection, current_user)
|
||||
|
||||
|
||||
user = await get_user_id(connection, user_id)
|
||||
if user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Account not found")
|
||||
|
||||
|
||||
update_values = put_user_data_validator(user_update,user)
|
||||
|
||||
if update_values is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="The provided data already exists in the database")
|
||||
|
||||
await put_user_id(connection, update_values, user)
|
||||
|
||||
|
||||
user = await get_user_id(connection, user_id)
|
||||
|
||||
return user
|
||||
|
||||
@api_router.delete("/{user_id}")
|
||||
async def delete_account(
|
||||
user_id: int,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends()
|
||||
):
|
||||
current_user = AccessTokenValidadtion(Authorize)
|
||||
|
||||
authorize_user = await db_user_role_validation(connection, current_user)
|
||||
|
||||
|
||||
user = await get_user_id(connection, user_id)
|
||||
if user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Account not found")
|
||||
|
||||
|
||||
user_update = UserUpdate(status=Status.DELETED.value)
|
||||
|
||||
update_values = put_user_id_validator(user_update,user)
|
||||
|
||||
if update_values is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="The provided data already exists in the database")
|
||||
|
||||
await put_user_id(connection, update_values, user)
|
||||
|
||||
|
||||
user = await get_user_id(connection, user_id)
|
||||
|
||||
return user
|
Reference in New Issue
Block a user