From bc52111190d2bdd6d8282c23d679de38f20bac53 Mon Sep 17 00:00:00 2001 From: difuz1x Date: Tue, 12 May 2026 01:53:13 +0300 Subject: [PATCH] Solytion --- crud.py | 36 +++++++++++++++++++++++++++++++++++ database.py | 14 ++++++++++++++ library.db | Bin 0 -> 28672 bytes main.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ models.py | 27 ++++++++++++++++++++++++++ schemas.py | 37 +++++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 crud.py create mode 100644 database.py create mode 100644 library.db create mode 100644 main.py create mode 100644 models.py create mode 100644 schemas.py diff --git a/crud.py b/crud.py new file mode 100644 index 00000000..4e1d5e6c --- /dev/null +++ b/crud.py @@ -0,0 +1,36 @@ +from sqlalchemy.orm import Session +import models +import schemas + + +def get_author(db: Session, author_id: int): + return (db.query(models.Author). + filter(models.Author.id == author_id).first()) + + +def get_authors(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.Author).offset(skip).limit(limit).all() + + +def create_author(db: Session, author: schemas.AuthorCreate): + db_author = models.Author(name=author.name, bio=author.bio) + db.add(db_author) + db.commit() + db.refresh(db_author) + return db_author + + +def get_books(db: Session, skip: int = 0, limit: int = 100, + author_id: int = None): + query = db.query(models.Book) + if author_id: + query = query.filter(models.Book.author_id == author_id) + return query.offset(skip).limit(limit).all() + + +def create_book(db: Session, book: schemas.BookCreate, author_id: int): + db_book = models.Book(**book.model_dump(), author_id=author_id) + db.add(db_book) + db.commit() + db.refresh(db_book) + return db_book diff --git a/database.py b/database.py new file mode 100644 index 00000000..fee1560d --- /dev/null +++ b/database.py @@ -0,0 +1,14 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy.ext.declarative import declarative_base + + +SQL_ALCHEMY_DATABASE_URL = "sqlite:///./library.db" + +engine = create_engine( + SQL_ALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() diff --git a/library.db b/library.db new file mode 100644 index 0000000000000000000000000000000000000000..b7cabfae7e20af485a16eec90d662e6de29fa4b0 GIT binary patch literal 28672 zcmeI(!H&{E7{Kw)!j_bTWfKn#;WCK_sTwc7fGpcxaoOT_LB$Ixg4(#y4V1XX(`KK+ zH}N5S3Ln9vr#(0=gBIexK=YrJPW$!iSLQcdCMoANaP%<)}^CF9Ar9~E`gtXvw^o9e^8pL?S6U7gSnKmY**5I_I{1Q0*~ zft(8*n5KQ-7K3m$o?L}j!%-A{S`5Q++Lv0s+w8fr?H#&rWag^udNR$Bjd1Lo?3%V? zi^*nGHF7lQhM%Ozb%$*+ZH$X;$Wl;q*Zoxm0t6pJ#E!bY+dlS^SmWwE39er6iiMU&KxvPr=C zDoS>}F0N103DCK^+u4`|9h8kbry+ephI|x8*%{b2Ej!8DTvUGP(WuoH@jRS;SnbPdT%!8hP#X;a1Q0*~0R#|0009ILKmY**awT9D zYF2jre`{24b2UH)g8%{uAbLx literal 0 HcmV?d00001 diff --git a/main.py b/main.py new file mode 100644 index 00000000..d3f62ee2 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +from fastapi import FastAPI, Depends, HTTPException +from sqlalchemy.orm import Session +from typing import List + + +import crud +import models +import schemas +from database import SessionLocal, engine + + +models.Base.metadata.create_all(bind=engine) + + +app = FastAPI(title="Library") + + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +@app.post("/authors/", response_model=schemas.Author) +def create_author(author: schemas.AuthorCreate, db: Session = Depends(get_db)): + return crud.create_author(db=db, author=author) + + +@app.get("/authors/", response_model=List[schemas.Author]) +def read_authors(skip: int = 0, limit: int = 100, + db: Session = Depends(get_db)): + return crud.get_authors(db=db, skip=skip, limit=limit) + + +@app.get("/authors/{author_id}", response_model=schemas.Author) +def get_author(author_id: int, db: Session = Depends(get_db)): + db_author = crud.get_author(db=db, author_id=author_id) + if db_author is None: + raise HTTPException(status_code=404, detail="Author not found") + return db_author + + +@app.post("/books/", response_model=schemas.Book) +def create_book_for_author(author: int, book: schemas.BookCreate, + db: Session = Depends(get_db)): + return crud.create_book(db=db, book=book, author_id=author) + + +@app.get("/books/", response_model=List[schemas.Book]) +def read_books(skip: int = 0, limit: int = 10, author_id: int = None, + db: Session = Depends(get_db)): + return crud.get_books(db, skip=skip, limit=limit, author_id=author_id) diff --git a/models.py b/models.py new file mode 100644 index 00000000..c3455003 --- /dev/null +++ b/models.py @@ -0,0 +1,27 @@ +from sqlalchemy import Column, Integer, String, Date, ForeignKey +from sqlalchemy.orm import relationship + + +from database import Base + + +class Author(Base): + __tablename__ = "authors" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String, unique=True, index=True) + bio = Column(String) + + books = relationship("Book", back_populates="author") + + +class Book(Base): + __tablename__ = "books" + + id = Column(Integer, primary_key=True, index=True) + title = Column(String, index=True) + summary = Column(String) + publication_date = Column(Date) + author_id = Column(Integer, ForeignKey("authors.id")) + + author = relationship("Author", back_populates="books") diff --git a/schemas.py b/schemas.py new file mode 100644 index 00000000..1511d9a2 --- /dev/null +++ b/schemas.py @@ -0,0 +1,37 @@ +from pydantic import BaseModel, ConfigDict +from typing import List, Optional +from datetime import date + + +class BookBase(BaseModel): + title: str + summary: str + publication_date: date + + +class BookCreate(BookBase): + pass + + +class Book(BookBase): + id: int + author_id: int + + model_config = ConfigDict(from_attributes=True) + + +class AuthorBase(BaseModel): + name: str + bio: Optional[str] = None + + +class AuthorCreate(AuthorBase): + pass + + +class Author(AuthorBase): + id: int + + books: List[Book] = [] + + model_config = ConfigDict(from_attributes=True)