lerafoxqueen/LeraFoxQueen/router.py

101 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, FastAPI, Depends, Body, Form, Request, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
from starlette.datastructures import FormData
from json import JSONDecodeError, dumps, loads
import uuid
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)
response.set_cookie(key="fakesession", value=str(uuid.uuid4()))
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
@router.post("/message")
async def message_data(
theme_name: str = Form(...),
sender_name: str = Form(...),
sender_sex: str = Form(...),
sender_age: int = Form(...),
telegram_name: str = Form(...),
interests_in_school_university: str = Form(...),
interests_in_sports: str = Form(...),
interests_in_work: str = Form(...),
interests_in_your_free_time: str = Form(...),
pets_plants_fish: str = Form(...),
relationships_with_relatives_and_friends: str = Form(...),
past_relationships: str = Form(...),
life_principles_values: str = Form(...),
goals_in_life: str = Form(...),
message_text: str = Form(...),
):
return {
"theme_name": theme_name,
"sender_name": sender_name,
"sender_sex": sender_sex,
"sender_age": sender_age,
"telegram_name": telegram_name,
"interests_in_school_university": interests_in_school_university,
"interests_in_sports": interests_in_sports,
"interests_in_work": interests_in_work,
"interests_in_your_free_time": interests_in_your_free_time,
"pets_plants_fish": pets_plants_fish,
"relationships_with_relatives_and_friends": relationships_with_relatives_and_friends,
"past_relationships": past_relationships,
"life_principles_values": life_principles_values,
"goals_in_life": goals_in_life,
"message_text": message_text,
}