84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
from fastapi import FastAPI, HTTPException, APIRouter, Request, Header, Depends
|
||
from fastapi.responses import JSONResponse
|
||
from fastapi.templating import Jinja2Templates
|
||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||
from typing import Dict
|
||
from pydantic import BaseModel
|
||
from sqlalchemy.orm import Session
|
||
import json
|
||
from model.database import get_async_session, Client
|
||
from utils.clients import upsert_client, del_jobs, add_jobs, get_applied_jobs
|
||
from typing import Union
|
||
|
||
import asyncio
|
||
|
||
router = APIRouter()
|
||
templates = Jinja2Templates(directory="templates")
|
||
API_KEY = "4545454"
|
||
|
||
# Пример данных
|
||
clients = {
|
||
1: {"username": "John Doe", "email": "john@example.com", "phone": "+123456781"},
|
||
44: {"username": "Jane Smith", "email": "jane@example.com", "phone": "+987654321"}
|
||
}
|
||
@router.get("/get_client/{client_id}", include_in_schema=False)
|
||
async def get_client_modal(client_id: int):
|
||
client = clients.get(client_id) # Используй clients вместо clients_db
|
||
if not client:
|
||
raise HTTPException(status_code=404, detail="Client not found")
|
||
return JSONResponse(content=client)
|
||
|
||
class JsonData(BaseModel):
|
||
json_data: Union[dict, str]
|
||
|
||
|
||
@router.post("/client/")
|
||
async def client(data: JsonData, x_api_key: str = Header(...), db: Session = Depends(get_async_session)):
|
||
if x_api_key != "4545454":
|
||
raise HTTPException(status_code=403, detail="Invalid API Key")
|
||
|
||
# Если json_data строка — декодируем
|
||
if isinstance(data.json_data, str):
|
||
try:
|
||
data.json_data = json.loads(data.json_data)
|
||
first_name = data.json_data['first_name']
|
||
# print(first_name)
|
||
last_name = data.json_data['last_name']
|
||
email_addr = data.json_data['email_addr']
|
||
user_id = data.json_data['user_id']
|
||
phone_num = data.json_data['phone_num']
|
||
|
||
print(first_name)
|
||
|
||
|
||
# Создаем отдельные сессии для работы с БД
|
||
async_session_maker = async_sessionmaker(bind=db.bind, expire_on_commit=False)
|
||
async with async_session_maker() as db1, async_session_maker() as db2:
|
||
client_task = upsert_client(db1, user_id, first_name, last_name, email_addr, phone_num, str(data.json_data))
|
||
del_task = del_jobs(db2, user_id)
|
||
|
||
|
||
client, _ = await asyncio.gather(client_task, del_task)
|
||
|
||
# ## TODO Получить данные для фильтра!
|
||
ads = await add_jobs(db, user_id)
|
||
get_jobs = await get_applied_jobs(db, user_id)
|
||
|
||
|
||
|
||
|
||
|
||
except json.JSONDecodeError:
|
||
raise HTTPException(status_code=400, detail="Invalid JSON format")
|
||
|
||
print("Полученные данные:", data.json_data)
|
||
return {"message": "JSON получен", "data": get_jobs}
|
||
|
||
|
||
|
||
|
||
# data = await upsert_client(db, first_name, last_name, email_addr, parsed_data)
|
||
# client = await upsert_client(db, user_id, first_name, last_name, email_addr, phone_num, str(data.json_data))
|
||
|
||
# resp = await idss(db, user_id)
|
||
# resp2 = await idss(db, user_id, first_name) |