more routes

This commit is contained in:
grey-cat-1908 2024-08-16 11:39:50 +00:00
parent c0f79726ec
commit d891d9de86
3 changed files with 68 additions and 5 deletions

View file

@ -1,8 +1,8 @@
from enum import Enum from enum import Enum
from uuid import UUID from uuid import UUID
from typing import TypeAlias from typing import TypeAlias, Annotated
from pydantic import field_validator, field_serializer from pydantic import field_validator, field_serializer, Field
from models import BaseModel, form from models import BaseModel, form
@ -79,7 +79,7 @@ class AnswerData(BaseModel):
class Answer(BaseModel): class Answer(BaseModel):
id: int id: int
form: form.Form form: Annotated[form.Form, Field(exclude=True)]
data: AnswerData data: AnswerData
@field_validator("data") @field_validator("data")

View file

@ -1,9 +1,10 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from sqlalchemy import select from sqlalchemy import select, and_
from pydantic import ValidationError from pydantic import ValidationError
import database import database
from models import AnswerData, Answer from models import AnswerData, Answer
from .utils import User
router = APIRouter(prefix="/answer") router = APIRouter(prefix="/answer")
@ -26,3 +27,42 @@ async def create_answer(form_id: int, answer_data: AnswerData):
raise HTTPException(400, e.errors()[0].get("msg")) raise HTTPException(400, e.errors()[0].get("msg"))
return answer_model return answer_model
@router.get("/get")
async def get_answers(user: User, form_id: int):
async with database.sessions.begin() as session:
return {
"answers": [
Answer.model_validate(item)
for item in await session.scalars(
select(database.Answer)
.where(
and_(
database.Answer.form_id == form_id,
database.Form.owner_id == user.id,
)
)
.join(database.Answer.form)
)
]
}
@router.delete("/delete")
async def delete_answer(user: User, id: int):
async with database.sessions.begin() as session:
stmt = (
select(database.Answer)
.where(database.Answer.id == id)
.join(database.Answer.form)
)
db_request = await session.execute(stmt)
answer = db_request.scalar_one_or_none()
if answer is None:
raise HTTPException(404, "Answer not found")
if answer.form.owner_id != user.id:
raise HTTPException(403, "Forbidden")
await session.delete(answer)

View file

@ -1,5 +1,5 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from sqlalchemy import select from sqlalchemy import select, delete
import database import database
from models import FormData, Form from models import FormData, Form
@ -22,6 +22,29 @@ async def create_form(user: User, form_data: FormData) -> Form:
return Form.model_validate(form) return Form.model_validate(form)
@router.put("/edit")
async def edit_form(user: User, id: int, form_data: FormData) -> Form:
async with database.sessions.begin() as session:
stmt = select(database.Form).where(database.Form.id == id)
db_request = await session.execute(stmt)
form = db_request.scalar_one_or_none()
if form is None:
raise HTTPException(404, "Form not found")
if form.owner_id != user.id:
raise HTTPException(403, "Forbidden")
form.name = form_data.name
form.data = form_data.model_dump()
await session.flush()
stmt = delete(database.Answer).where(database.Answer.form_id == id)
await session.execute(stmt)
return Form.model_validate(form)
@router.delete("/delete") @router.delete("/delete")
async def delete_form(user: User, id: int): async def delete_form(user: User, id: int):
async with database.sessions.begin() as session: async with database.sessions.begin() as session: