some comments and fixes

This commit is contained in:
2025-05-27 12:43:50 +03:00
parent dd0be3e257
commit 6055467e97
3 changed files with 69 additions and 8 deletions

View File

@@ -2,15 +2,17 @@ from sqlmodel import create_engine, SQLModel, Session
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
# Get database configuration
load_dotenv() load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL") DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL: if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is not set") raise ValueError("DATABASE_URL environment variable is not set")
# Create database engine
engine = create_engine(DATABASE_URL, echo=True) engine = create_engine(DATABASE_URL, echo=True)
SQLModel.metadata.create_all(engine) SQLModel.metadata.create_all(engine)
# Get database session
def get_session(): def get_session():
with Session(engine) as session: with Session(engine) as session:
yield session yield session

View File

@@ -1,19 +1,23 @@
from fastapi import FastAPI from fastapi import FastAPI, HTTPException
from sqlmodel import SQLModel, Session, select from sqlmodel import SQLModel, Session, select
from typing import List
from .database import engine from .database import engine
from .models import Author, Book from .models import Author, Book
app = FastAPI() app = FastAPI()
# Initialize the database
@app.on_event("startup") @app.on_event("startup")
def on_startup(): def on_startup():
SQLModel.metadata.create_all(engine) SQLModel.metadata.create_all(engine)
# Root endpoint
@app.get("/") @app.get("/")
async def read_root(): async def hello_world():
return {"message": "Hello, FastAPI with SQLModel and PostgreSQL!"} return {"message": "Hello world!"}
@app.post("/authors/") # Create an author
@app.post("/authors/", response_model=Author)
def create_author(author: Author): def create_author(author: Author):
with Session(engine) as session: with Session(engine) as session:
session.add(author) session.add(author)
@@ -21,13 +25,39 @@ def create_author(author: Author):
session.refresh(author) session.refresh(author)
return author return author
@app.get("/authors/") # Read authors
@app.get("/authors/", response_model=List[Author])
def read_authors(): def read_authors():
with Session(engine) as session: with Session(engine) as session:
authors = session.exec(select(Author)).all() authors = session.exec(select(Author)).all()
return authors return authors
@app.post("/books/") # Update an author
@app.put("/authors/{author_id}", response_model=Author)
def update_author(author_id: int, author: Author):
with Session(engine) as session:
db_author = session.get(Author, author_id)
if not db_author:
raise HTTPException(status_code=404, detail="Author not found")
db_author.name = author.name
session.add(db_author)
session.commit()
session.refresh(db_author)
return db_author
# Delete an author
@app.delete("/authors/{author_id}")
def delete_author(author_id: int):
with Session(engine) as session:
db_author = session.get(Author, author_id)
if not db_author:
raise HTTPException(status_code=404, detail="Author not found")
session.delete(db_author)
session.commit()
return {"message": "Author deleted"}
# Create a book
@app.post("/books/", response_model=Book)
def create_book(book: Book): def create_book(book: Book):
with Session(engine) as session: with Session(engine) as session:
session.add(book) session.add(book)
@@ -35,8 +65,34 @@ def create_book(book: Book):
session.refresh(book) session.refresh(book)
return book return book
@app.get("/books/") # Read books
@app.get("/books/", response_model=List[Book])
def read_books(): def read_books():
with Session(engine) as session: with Session(engine) as session:
books = session.exec(select(Book)).all() books = session.exec(select(Book)).all()
return books return books
# Update a book
@app.put("/books/{book_id}", response_model=Book)
def update_book(book_id: int, book: Book):
with Session(engine) as session:
db_book = session.get(Book, book_id)
if not db_book:
raise HTTPException(status_code=404, detail="Book not found")
db_book.title = book.title
db_book.authors = book.authors
session.add(db_book)
session.commit()
session.refresh(db_book)
return db_book
# Delete a book
@app.delete("/books/{book_id}")
def delete_book(book_id: int):
with Session(engine) as session:
db_book = session.get(Book, book_id)
if not db_book:
raise HTTPException(status_code=404, detail="Book not found")
session.delete(db_book)
session.commit()
return {"message": "Book deleted"}

View File

@@ -1,16 +1,19 @@
from typing import Optional, List from typing import Optional, List
from sqlmodel import SQLModel, Field, Relationship from sqlmodel import SQLModel, Field, Relationship
# Relationship model
class AuthorBookLink(SQLModel, table=True): class AuthorBookLink(SQLModel, table=True):
author_id: int | None = Field(default=None, foreign_key="author.id", primary_key=True) author_id: int | None = Field(default=None, foreign_key="author.id", primary_key=True)
book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True) book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True)
# Author model
class Author(SQLModel, table=True): class Author(SQLModel, table=True):
id: Optional[int] = Field(primary_key=True, index=True) id: Optional[int] = Field(primary_key=True, index=True)
name: str name: str
books: List["Book"] = Relationship(back_populates="authors", link_model=AuthorBookLink) books: List["Book"] = Relationship(back_populates="authors", link_model=AuthorBookLink)
# Book model
class Book(SQLModel, table=True): class Book(SQLModel, table=True):
id: Optional[int] = Field(primary_key=True, index=True) id: Optional[int] = Field(primary_key=True, index=True)
title: str title: str