Skip to content

Commit b9df4d0

Browse files
product, category create get query model routes created
1 parent b073056 commit b9df4d0

File tree

12 files changed

+205
-27
lines changed

12 files changed

+205
-27
lines changed

BaseApp/BlogApp/models.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

BaseApp/BlogApp/query.py

Whitespace-only changes.

BaseApp/BlogApp/router.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

BaseApp/BlogApp/schemas.py

Whitespace-only changes.
File renamed without changes.

BaseApp/ProductApp/models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from sqlalchemy import Boolean, Column, Integer, String, ForeignKey
2+
from sqlalchemy.orm import relationship
3+
# Import Base variable of database from database
4+
from BaseApp.database import Base
5+
6+
7+
class Product(Base):
8+
__tablename__ = "products"
9+
id = Column(Integer, primary_key=True)
10+
name = Column(String(50), nullable=True)
11+
detail = Column(String(100), nullable=False)
12+
category_id = Column(Integer, ForeignKey('category.id'), nullable=False)
13+
is_delete = Column(Boolean, default=False, nullable=False)
14+
created_on = Column(String(100), nullable=True)
15+
updated_on = Column(String(100), nullable=True)
16+
17+
def __repr__(self):
18+
return 'Product name : %r' % self.name
19+
20+
21+
class Category(Base):
22+
__tablename__ = "category"
23+
id = Column(Integer, primary_key=True)
24+
name = Column(String(50), nullable=True)
25+
is_delete = Column(Boolean, default=False, nullable=False)
26+
created_on = Column(String(100), nullable=True)
27+
updated_on = Column(String(100), nullable=True)
28+
product = relationship("Product", backref="category", lazy=True)
29+
30+
def __repr__(self):
31+
return 'Category name : %r' % self.name

BaseApp/ProductApp/query.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from BaseApp.ProductApp.models import Product, Category
2+
import datetime
3+
4+
5+
def getCategory(id, db):
6+
return db.query(Category).filter_by(id=id, is_delete=False).first()
7+
8+
9+
def getAllCategory(db):
10+
return db.query(Category).all()
11+
12+
13+
def addCategory(data, db):
14+
dateTime = str(datetime.datetime.timestamp(datetime.datetime.now()))
15+
category = Category(
16+
name=data.name,
17+
created_on=dateTime,
18+
updated_on=dateTime
19+
)
20+
db.add(category)
21+
db.commit()
22+
return category
23+
24+
25+
def getProduct(id, db):
26+
return db.query(Product).filter_by(id=id, is_delete=False).first()
27+
28+
29+
def getAllProducts(db):
30+
return db.query(Product).all()
31+
32+
33+
def addProduct(data, db):
34+
dateTime = str(datetime.datetime.timestamp(datetime.datetime.now()))
35+
category = getCategory(data.category, db)
36+
product = Product(
37+
name=data.name,
38+
detail=data.detail,
39+
category_id=category.id,
40+
created_on=dateTime,
41+
updated_on=dateTime
42+
)
43+
db.add(product)
44+
db.commit()
45+
return product

BaseApp/ProductApp/router.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
from fastapi import Depends, APIRouter
3+
from sqlalchemy.orm import Session
4+
# get_db method from dependencies
5+
from BaseApp.UserApp.schemas import UserResponse
6+
from BaseApp.dependencies import get_db, get_current_user
7+
from BaseApp.ProductApp.schemas import ProductResponse, CategoryResponse, ProductCreate, CategoryCreate
8+
from BaseApp.ProductApp.query import addProduct, addCategory, getAllProducts, getAllCategory
9+
10+
# Create Router Instance of FastApi to create api's
11+
api = APIRouter(tags=['Products'])
12+
13+
14+
# Tag name for this app is blog || use tags=['blog']
15+
16+
# API's created here
17+
@api.get("/products", response_model=List[ProductResponse], status_code=200)
18+
async def allProducts(db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_user)):
19+
data = getAllProducts(db)
20+
return data
21+
22+
23+
@api.post("/product-create", response_model=ProductResponse, status_code=201)
24+
async def createProduct(data: ProductCreate, db: Session = Depends(get_db),
25+
current_user: UserResponse = Depends(get_current_user)):
26+
data = addProduct(data, db)
27+
return data
28+
29+
30+
@api.get("/categories", response_model=List[CategoryResponse], status_code=200)
31+
async def allCategory(db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_user)):
32+
data = getAllCategory(db)
33+
return data
34+
35+
36+
@api.post("/create-category", response_model=CategoryResponse, status_code=201)
37+
async def createCategory(data: CategoryCreate, db: Session = Depends(get_db),
38+
current_user: UserResponse = Depends(get_current_user)):
39+
data = addCategory(data, db)
40+
return data

BaseApp/ProductApp/schemas.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
3+
from pydantic import BaseModel
4+
5+
6+
class CategoryResponse(BaseModel):
7+
id: int
8+
name: str
9+
created_on: str
10+
11+
# Required for ORM based response data to enable ORM mode
12+
class Config:
13+
orm_mode = True
14+
15+
16+
class ProductResponse(BaseModel):
17+
id: int
18+
name: str
19+
detail: str
20+
category_id: int
21+
created_on: str
22+
23+
# Required for ORM based response data to enable ORM mode
24+
class Config:
25+
orm_mode = True
26+
27+
28+
class ProductCreate(BaseModel):
29+
name: str
30+
detail: str
31+
category: int
32+
33+
34+
class CategoryCreate(BaseModel):
35+
name: str

BaseApp/manager.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from BaseApp.database import engine
55
# Import Apps module
66
from BaseApp import UserApp
7-
from BaseApp import BlogApp
7+
from BaseApp import ProductApp
88
# Import models and router from App
99
from BaseApp.UserApp import models, router
10-
from BaseApp.BlogApp import models, router
10+
from BaseApp.ProductApp import models, router
1111

12+
# No need to add or bind to database engine to our models here alembic do this
1213
# Binding Apps models to database engine
1314
# UserApp.models.Base.metadata.create_all(bind=engine)
14-
# BlogApp.models.Base.metadata.create_all(bind=engine)
15+
# ProductApp.models.Base.metadata.create_all(bind=engine)
1516

1617
# FastApi Instance
1718
app = FastAPI(
@@ -22,8 +23,6 @@
2223

2324
# Include App to FastApi Instance
2425
app.include_router(UserApp.router.api, prefix="/auth")
25-
app.include_router(BlogApp.router.api, prefix="/blog")
26+
app.include_router(ProductApp.router.api, prefix="/product")
27+
2628

27-
# Run FastApi to main
28-
# if __name__ == "__main__":
29-
# uvicorn.run(app, host="0.0.0.0", port=8000)

db-migration/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
# This helps alembic auto generation
2323
from BaseApp.UserApp.models import *
24+
from BaseApp.ProductApp.models import *
2425
from BaseApp.database import Base
2526

2627
target_metadata = Base.metadata
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""product and category model
2+
3+
Revision ID: 9a397463dcbf
4+
Revises: 12db141fd663
5+
Create Date: 2021-06-19 17:36:33.007047
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '9a397463dcbf'
14+
down_revision = '12db141fd663'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('category',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('name', sa.String(length=50), nullable=True),
24+
sa.Column('is_delete', sa.Boolean(), nullable=False),
25+
sa.Column('created_on', sa.String(length=100), nullable=True),
26+
sa.Column('updated_on', sa.String(length=100), nullable=True),
27+
sa.PrimaryKeyConstraint('id')
28+
)
29+
op.create_table('products',
30+
sa.Column('id', sa.Integer(), nullable=False),
31+
sa.Column('name', sa.String(length=50), nullable=True),
32+
sa.Column('detail', sa.String(length=100), nullable=False),
33+
sa.Column('category_id', sa.Integer(), nullable=False),
34+
sa.Column('is_delete', sa.Boolean(), nullable=False),
35+
sa.Column('created_on', sa.String(length=100), nullable=True),
36+
sa.Column('updated_on', sa.String(length=100), nullable=True),
37+
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
38+
sa.PrimaryKeyConstraint('id')
39+
)
40+
# ### end Alembic commands ###
41+
42+
43+
def downgrade():
44+
# ### commands auto generated by Alembic - please adjust! ###
45+
op.drop_table('products')
46+
op.drop_table('category')
47+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)