-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (106 loc) · 3.79 KB
/
main.py
File metadata and controls
118 lines (106 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from fastapi import Depends,FastAPI
from fastapi.middleware.cors import CORSMiddleware
from models import Product
from database import session, engine
import database_models
from sqlalchemy.orm import Session
app=FastAPI()
#To resolve CORS for connection with backend and frontend
app.add_middleware(
CORSMiddleware,
#add the address of frontend
allow_origins=["http://localhost:3000"],
#allow all the methods in the app to access
allow_methods=["*"]
)
#sqlalchemy to access the database and add the data to
#database
database_models.Base.metadata.create_all(bind=engine)
#we are trying to access the homepage
@app.get("/")
def greet():
return "Welcome to My app"
products=[
#passing the value to models class product
Product(id=1,name="Phone",description="budget phone",price=699.99,quantity=10),
Product(id=2,name="Laptop",description="budget laptop",price=999.99,quantity=30),
Product(id=3,name="Pen",description="A blue ink pen",price=1.99,quantity=100),
Product(id=4,name="Table",description="A wooden table",price=199.99,quantity=20),
]
#getting globally the session to re use
#everytime anyone wants to use db then they have to inject this into
#object
def get_db():
db=session()
#close the connection
try:
yield db
finally:
db.close()
#function to store data in the tables created
def init_db():
db=session()
#check if table is empty then execute else don't
count=db.query(database_models.Product).count
if count==0:
for product in products:
#we get product from db_model of sql alchemy then
#convert our product into sqlalchemy product were we convert pydantic using model dump then unpack model dump
#for key value pair
db.add(database_models.Product(**product.model_dump()))
db.commit()
init_db()
#getting all the product
@app.get("/products")
#Dependencies Injection
def get_all_products(db:Session=Depends(get_db)):
#database connection with injection from fastapi
db_products=db.query(database_models.Product).all()
return db_products
return products
#get product through the ID
@app.get("/products/{id}")
def get_product_by_id(id:int,db:Session=Depends(get_db)):
#find product in database model were first product whose id matches
db_product= db.query(database_models.Product).filter(database_models.Product.id==id).first()
#if there is db-product then return db_product
if db_product:
return db_product
#else
return "product not found"
#posting the product to the products list
@app.post("/products")
def add_product(product:Product,db:Session=Depends(get_db)):
#adding data to the model were user enter the value
db.add(database_models.Product(**product.model_dump()))
#commit the changes to database
db.commit()
return product
#update product
@app.put("/products/{id}")
def update_product(id: int,product:Product,db:Session=Depends(get_db)):
#check for product
db_product=db.query(database_models.Product).filter(database_models.Product.id==id).first()
#if product exist then
if db_product:
#change the properties of the product
db_product.name=product.name
db_product.price=product.price
db_product.description=product.description
db_product.quantity=product.quantity
#commit the changes
db.commit()
return "Product Updated"
else:
return "No product found"
#delete product
@app.delete("/products")
def delete_product(id:int,db:Session=Depends(get_db)):
#get the product
db_product=db.query(database_models.Product).filter(database_models.Product.id==id).first()
if db_product:
db.delete(db_product)
db.commit()
return "Product Deleted"
else:
return "No Product Found"