Created
September 20, 2022 14:53
-
-
Save dineshkumarkb/f34a4347428323970904c63e280151f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fastapi_pagination import Page, paginate, add_pagination | |
| from pydantic import BaseModel | |
| from fastapi import FastAPI | |
| from sqlalchemy.exc import SQLAlchemyError | |
| from sqlalchemy.orm import sessionmaker, declarative_base | |
| from datetime import date | |
| app = FastAPI(title=" A simple pagination learning exercise", | |
| debug=True) | |
| add_pagination(app) | |
| class Employee(Base): | |
| __tablename__ = "employee" | |
| empid = Column(Integer, primary_key=True) | |
| empname = Column(String) | |
| dept = Column(Integer) | |
| location = Column(String) | |
| dob = Column(Date) | |
| class EmployeeOut(BaseModel): | |
| empid: int | |
| empname: str | |
| dept: int | |
| location: str | |
| dob: date | |
| class Config: | |
| orm_mode = True | |
| @app.get(path="/api/employees/all", name="Gets all employees", response_model=Page[EmployeeOut]) | |
| async def get_all_employees(): | |
| conn = connect_to_db() | |
| results = conn.query(Employee).all() | |
| return paginate(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment