# from json import JSONDecodeError, dumps, loads from fastapi import ( APIRouter, Body, Cookie, Depends, FastAPI, Form, Header, HTTPException, Request, ) from fastapi.responses import HTMLResponse, JSONResponse from starlette.datastructures import FormData from .repository import DatingRepository from .schemas import SDating, SDatingAdd, SDatingId # async def get_body(request: Request): # content_type = request.headers.get('Content-Type') # if content_type is None: # raise HTTPException(status_code=400, detail='No Content-Type provided!') # elif content_type == 'application/json': # try: # return await request.json() # except JSONDecodeError: # raise HTTPException(status_code=400, detail='Invalid JSON data') # elif (content_type == 'application/x-www-form-urlencoded' or # content_type.startswith('multipart/form-data')): # try: # return await request.form() # except Exception: # raise HTTPException(status_code=400, detail='Invalid Form data') # else: # raise HTTPException(status_code=400, detail='Content-Type not supported!') router = APIRouter( prefix="/datings", tags=["Знакомства"], ) @router.post( "/add", description="Добавляет знакомство в базу данных, а еще ....", summary="Добавляет знакомство в базу данных", response_description="Вот такой ответ придет", ) # async def add_dating(dating: SDatingAdd = Body()) -> SDatingId: # new_dating_id = await DatingRepository.add_dating(dating) # return {"id": new_dating_id} # type: ignore async def add_dating(dating: SDatingAdd = Body()) -> JSONResponse: new_dating_id = await DatingRepository.add_dating(dating) content = {"id": new_dating_id} # type: ignore response = JSONResponse(content=content) return response @router.get("/get") async def get_datings() -> list[SDating]: datings = await DatingRepository.get_datings() return datings # async def get_datings() -> JSONResponse: # datings = await DatingRepository.get_datings() # content = dumps(datings, default=lambda o: o.__dict__) # type: ignore # response = JSONResponse(content=content) # response.set_cookie(key="fakesession", value="fake-cookie-session-value") # return response