Add api func
This commit is contained in:
74
main.py
Normal file
74
main.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from fastapi import FastAPI, HTTPException, Depends
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models import Auditory, Oboruds, Zametki, Base
|
||||
from schemas import AuditoryCreate, Auditory, OborudsCreate, Oboruds, ZametkiCreate, Zametki # Импортируем Pydantic-модели
|
||||
|
||||
DATABASE_URL = "sqlite+aiosqlite:///./test.db" # Используем асинхронный SQLite
|
||||
|
||||
engine = create_async_engine(DATABASE_URL, echo=True)
|
||||
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
# Dependency to get the database session
|
||||
async def get_db():
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
|
||||
# CRUD для Auditory
|
||||
@app.post("/auditory/", response_model=Auditory)
|
||||
async def create_auditory(auditory: AuditoryCreate, db: AsyncSession = Depends(get_db)):
|
||||
db_auditory = Auditory(**auditory.model_dump())
|
||||
db.add(db_auditory)
|
||||
await db.commit()
|
||||
await db.refresh(db_auditory)
|
||||
return db_auditory
|
||||
|
||||
@app.get("/auditory/{auditory_id}", response_model=Auditory)
|
||||
async def read_auditory(auditory_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Auditory).filter(Auditory.id == auditory_id))
|
||||
auditory = result.scalars().first()
|
||||
if auditory is None:
|
||||
raise HTTPException(status_code=404, detail="Auditory not found")
|
||||
return auditory
|
||||
|
||||
# CRUD для Oboruds
|
||||
@app.post("/oboruds/", response_model=Oboruds)
|
||||
async def create_oboruds(oboruds: OborudsCreate, db: AsyncSession = Depends(get_db)):
|
||||
db_oboruds = Oboruds(**oboruds.model_dump())
|
||||
db.add(db_oboruds)
|
||||
await db.commit()
|
||||
await db.refresh(db_oboruds)
|
||||
return db_oboruds
|
||||
|
||||
@app.get("/oboruds/{oboruds_id}", response_model=Oboruds)
|
||||
async def read_oboruds(oboruds_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Oboruds).filter(Oboruds.id == oboruds_id))
|
||||
oboruds = result.scalars().first()
|
||||
if oboruds is None:
|
||||
raise HTTPException(status_code=404, detail="Oboruds not found")
|
||||
return oboruds
|
||||
|
||||
# CRUD для Zametki
|
||||
@app.post("/zametki/", response_model=Zametki)
|
||||
async def create_zametki(zametki: ZametkiCreate, db: AsyncSession = Depends(get_db)):
|
||||
db_zametki = Zametki(**zametki.model_dump())
|
||||
db.add(db_zametki)
|
||||
await db.commit()
|
||||
await db.refresh(db_zametki)
|
||||
return db_zametki
|
||||
|
||||
@app.get("/zametki/{zametki_id}", response_model=Zametki)
|
||||
async def read_zametki(zametki_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Zametki).filter(Zametki.id == zametki_id))
|
||||
zametki = result.scalars().first()
|
||||
if zametki is None:
|
||||
raise HTTPException(status_code=404, detail="Zametki not found")
|
||||
return zametki
|
||||
36
models.py
Normal file
36
models.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import relationship
|
||||
from databases import Database
|
||||
import datetime
|
||||
|
||||
DATABASE_URL = "sqlite:///./test.db" # Замени на свою базу данных
|
||||
|
||||
database = Database(DATABASE_URL)
|
||||
Base = declarative_base()
|
||||
|
||||
class Auditory(Base):
|
||||
__tablename__ = "auditory"
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
audnazvanie = Column(String)
|
||||
oboruds = relationship('Oboruds', back_populates='auditory')
|
||||
|
||||
class Oboruds(Base):
|
||||
__tablename__ = "oboruds"
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
invNumber = Column(Integer)
|
||||
nazvanie = Column(String(500))
|
||||
raspologenie = Column(String(200))
|
||||
numberved = Column(String(100))
|
||||
numberppasu = Column(String(100))
|
||||
kolichestvo = Column(Integer)
|
||||
balancenumber = Column(Integer)
|
||||
aud_id = Column(Integer, ForeignKey('auditory.id'))
|
||||
auditory = relationship('Auditory', back_populates='oboruds')
|
||||
|
||||
class Zametki(Base):
|
||||
__tablename__ = "zametki"
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
txtzam = Column(String(10000))
|
||||
created_date = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
rmdt = Column(DateTime)
|
||||
50
schemas.py
Normal file
50
schemas.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
# Pydantic модель для Auditory
|
||||
class AuditoryBase(BaseModel):
|
||||
audnazvanie: str
|
||||
|
||||
class AuditoryCreate(AuditoryBase):
|
||||
pass
|
||||
|
||||
class Auditory(AuditoryBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True # Ранее называлось `orm_mode = True`
|
||||
|
||||
# Pydantic модель для Oboruds
|
||||
class OborudsBase(BaseModel):
|
||||
invNumber: int
|
||||
nazvanie: str
|
||||
raspologenie: str
|
||||
numberved: str
|
||||
numberppasu: str
|
||||
kolichestvo: int
|
||||
balancenumber: int
|
||||
aud_id: int
|
||||
|
||||
class OborudsCreate(OborudsBase):
|
||||
pass
|
||||
|
||||
class Oboruds(OborudsBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# Pydantic модель для Zametki
|
||||
class ZametkiBase(BaseModel):
|
||||
txtzam: str
|
||||
rmdt: datetime | None = None
|
||||
|
||||
class ZametkiCreate(ZametkiBase):
|
||||
pass
|
||||
|
||||
class Zametki(ZametkiBase):
|
||||
id: int
|
||||
created_date: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Reference in New Issue
Block a user