Commit 1df23fb4 authored by Libcat's avatar Libcat 👷
Browse files

修改商品分类适配花束自选功能

parent 18d86321
......@@ -28,14 +28,16 @@ class Response(Model):
msg: str
class ProductType(Model):
class Category(Model):
id: int
name: str
flexi: bool
# products: List[Product]
class ProductTypeCreate(Model):
class CategoryCreate(Model):
name: str
flexi: Optional[bool] = False
class ProductBase(Model):
......@@ -44,7 +46,7 @@ class ProductBase(Model):
name: str
price: Decimal
description: str
# product_type: ProductType
# product_type: Category
image: Optional[str]
class ProductGet(Model):
......@@ -56,7 +58,7 @@ class ProductGet(Model):
detail: str
inventory: int
sales: int
product_type: ProductType
category: Category
created_time: datetime
image: Optional[str]
......@@ -68,7 +70,7 @@ class ProductCreate(Model):
detail: str
inventory: int
price: Decimal
product_type: int
category: int
image: Optional[str]
......
......@@ -6,34 +6,51 @@ import apischemas as api
from database import *
router = APIRouter(tags=["产品类型CRUD"], prefix="/product_types")
router = APIRouter(tags=["产品类型CRUD"], prefix="/category")
@router.get('/',
response_model=List[api.ProductType],
response_model=List[api.Category],
description="获取产品类型列表")
async def get_multi():
return list(ProductType.select())
async def get_multi(flexi: bool = None):
print(flexi)
q = Category.select()
if flexi!=None:
q=q.where(Category.flexi==flexi)
return list(q)
@router.get('/{id}',
response_model=api.ProductType,
response_model=api.Category,
description="获取产品类型")
async def get_one(id:int):
return ProductType.get_or_none(ProductType.id==id)
return Category.get_or_none(Category.id==id)
@router.post('/',
response_model=api.ProductType,
response_model=api.Category,
description="创建产品类型")
async def create_one(item: api.ProductTypeCreate):
dbitem = ProductType.create(**item.dict())
async def create_one(item: api.CategoryCreate):
dbitem = Category.create(**item.dict())
return dbitem
@router.put('/',
response_model=api.Category,
description="修改产品类型")
async def create_one(item: api.Category):
dbitem: Category = Category.get_or_none(Category.id == item.id)
if dbitem:
dbitem.name = item.name
dbitem.flexi = item.flexi
dbitem.save()
return Response("修改成功", 200)
else:
return Response("该项目不存在", 404)
@router.delete('/{id}', description="删除产品类型")
async def delete_one(id: int):
dbitem: Product = Product.get_or_none(Product.id == id)
dbitem: Category = Category.get_or_none(Category.id == id)
if dbitem:
dbitem.delete_instance(recursive=True)
return Response("删除成功", 200)
......
......@@ -29,7 +29,7 @@ async def get_one(id: int):
@router.post('/', response_model=api.ProductGet, description="创建产品")
async def create_one(name: str = Form(...),
description: Optional[str] = Form(""),
product_type: int = Form(...),
category: int = Form(...),
price: Optional[Decimal] = Form(0),
inventory: Optional[int] = Form(0),
sales: Optional[int] = Form(0),
......@@ -48,7 +48,7 @@ async def create_one(name: str = Form(...),
f.write(content)
dbitem = Product.create(name=name,
description=description,
product_type=product_type,
category=category,
price=price,
inventory=inventory,
sales=sales,
......@@ -61,7 +61,7 @@ async def create_one(name: str = Form(...),
async def update_one(id: int,
name: Optional[str] = Form(None),
description: Optional[str] = Form(None),
product_type: Optional[int] = Form(None),
category: Optional[int] = Form(None),
price: Optional[Decimal] = Form(None),
inventory: Optional[int] = Form(None),
sales: Optional[int] = Form(None),
......@@ -77,8 +77,8 @@ async def update_one(id: int,
dbitem.name = name
if description:
dbitem.description = description
if product_type:
dbitem.product_type = product_type
if category:
dbitem.category = category
if price:
dbitem.price = price
if inventory:
......
......@@ -13,5 +13,4 @@
from .models import *
from .database import *
from .crud import *
from . import config
\ No newline at end of file
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : crud.py
@Time : 2021/05/14 12:10:35
@Author : Runze Tang
@Version : 1.0
@Contact : mf1933084@smail.nju.edu.cn
@License : (C)Copyright 2020, Runze Tang
@Desc : 帮助对数据库模型的增删改查
'''
from typing import Callable
from functools import wraps
from peewee import fn
from .models import *
def as_list(Func: Callable) -> list:
@wraps(Func)
def f():
return list(Func())
return f
......@@ -14,7 +14,7 @@ from playhouse.pool import PooledMySQLDatabase
from .models import *
from . import config
__all__=('init_database', 'reset_database', 'connect', 'close', 'init_data', 'dbconnection')
__all__=('init_database', 'reset_database', 'connect', 'close', 'dbconnection')
dbconnection = PooledMySQLDatabase(config.database,
max_connections=10,
......@@ -23,7 +23,7 @@ dbconnection = PooledMySQLDatabase(config.database,
password=config.password)
registed_models = (
ProductType, Product, User, UserAddress, Order, OrderItems, CarItems,
Category, Product, User, UserAddress, Order, OrderItems, CarItems,
Session
)
......
......@@ -13,12 +13,12 @@
from peewee import *
from datetime import datetime
__all__ = ('ProductType', 'Product', 'User', 'UserAddress', 'Order',
__all__ = ('Category', 'Product', 'User', 'UserAddress', 'Order',
'OrderItems', 'CarItems', 'Session')
# 商品类型
class ProductType(Model):
class Category(Model):
"""商品类型"""
name = CharField()
flexi = BooleanField(default=False, verbose_name="是否是自选花束", help_text="是否是自选花束")
......@@ -34,7 +34,7 @@ class Product(Model):
inventory = IntegerField(default=0)
sales = IntegerField(default=0)
price = DecimalField(10, 2, default=0.0)
product_type = ForeignKeyField(ProductType, backref='products')
category = ForeignKeyField(Category, backref='products')
image = CharField(null=True)
created_time = DateTimeField(default=datetime.now)
......
......@@ -12,10 +12,15 @@ conn.close()
dbconnection.connect(True)
if dbconnection.table_exists("product"):
print("Abort: 数据库已经存在,请不要重复初始化!")
sys.exit(0)
print("Warning: 数据库已经存在,确定要继续吗?会丢失全部数据!y/N")
ans = input()
if ans=='y':
pass
else:
sys.exit(0)
init_database()
# 清空并创建数据表
reset_database()
with dbconnection.atomic():
User.create(username='admin',
......@@ -27,25 +32,25 @@ with dbconnection.atomic():
is_admin=False,
email='user@wenyuan.com')
type1 = ProductType.create(name='关于爱情')
type2 = ProductType.create(name='关于友情')
type1 = Category.create(name='关于爱情')
type2 = Category.create(name='关于友情')
ProductType.create(name='关于亲情')
ProductType.create(name='多肉绿植')
ProductType.create(name='花束自选')
Category.create(name='关于亲情')
Category.create(name='多肉绿植')
Category.create(name='花束自选')
Product.create(
name="红玫瑰",
price=9.9,
inventory=17,
description=
"红色玫瑰的花语是我爱你,代表着热恋、希望和你泛起激情的爱。 它因花语和寓意非常美好且花朵也十分娇艳美丽,所以成为每年情人节最受欢迎的鲜花,是情侣间表达爱意最好的花束。",
product_type=type1,
category=type1,
image="redrose.png")
Product.create(name="白玫瑰",
price=8.8,
inventory=8,
description="白玫瑰花语:纯洁,浪漫,求爱之花,以及我足以与你相配。",
product_type=type2,
category=type2,
image="whiterose.png")
dbconnection.close()
......
......@@ -6,7 +6,7 @@ from playhouse.pool import PooledMySQLDatabase
import apischemas as api
import database as db
from controllers import api_products, api_producttype, api_user_address, api_users, api_orders, model_crud_base, api_file, api_car
from controllers import api_products, api_category, api_user_address, api_users, api_orders, model_crud_base, api_file, api_car
app = FastAPI(debug=True, title="文苑商城", description="文苑商城后台API系统", version="0.1.0")
......@@ -35,7 +35,7 @@ async def add_process_time_header(request: Request, call_next):
app.include_router(api_users.router)
app.include_router(api_products.router)
app.include_router(api_producttype.router)
app.include_router(api_category.router)
app.include_router(api_user_address.router)
app.include_router(api_orders.router)
app.include_router(api_car.router)
......
......@@ -3,4 +3,5 @@ fastapi
uvicorn
pymysql
aiofiles
python-multipart
\ No newline at end of file
python-multipart
arrow
\ No newline at end of file
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