Commit f30f2b11 by rakesh.pv

fast api crud pi using alchamy sqlite

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (simple_crud_fastapi)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/simple_crud_fastapi.iml" filepath="$PROJECT_DIR$/.idea/simple_crud_fastapi.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Create sqlite engine instance
engine = create_engine("sqlite:///todo.db")
# Create declarative base meta instance
Base = declarative_base()
# Create session local class for session maker
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
from fastapi import FastAPI, Body
import schemas
from schemas import Item
app = FastAPI()
sample_db = {
1: {"task 1": " learn"},
2: {"task 2 ": "improve"},
3: {" task 3 ": " rework"}
}
@app.get("/")
def index():
return sample_db
# path paramaters api
# get method
@app.get("/{id}")
def get_by_id(id: int):
return sample_db[id]
# post method
# Option # 2
@app.post("/")
def addItem(item: schemas.Item):
newId = len(sample_db.keys()) + 1
sample_db[newId] = {"task": item.task}
return sample_db
# update
# put
@app.put("/{id}")
def updateItem(id: int, item: schemas.Item):
sample_db[id]['task'] = item.task
return sample_db
# delete
@app.delete("/{id}")
def deleteItem(id: int):
del sample_db[id]
return sample_db
from sqlalchemy import Column, Integer, String
from database import Base
class Item(Base):
__tablename__ = 'items'
id = Column(Integer, primary_key=True)
task = Column(String(256))
from pydantic import BaseModel
class Item(BaseModel):
task: str
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment