|
|
|
|
from datetime import datetime
|
|
|
|
|
from flask import request, jsonify
|
|
|
|
|
|
|
|
|
|
from flask import *
|
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
import pymysql
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:LH20021212@localhost:3306/智能家居系统'
|
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with app.app_context():
|
|
|
|
|
# 尝试连接数据库
|
|
|
|
|
|
|
|
|
|
db.create_all()
|
|
|
|
|
print("数据库连接成功!")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"数据库连接失败: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JiaJu(db.Model):
|
|
|
|
|
_tablename_ = 'JiaJu' # 这里应该是__tablename__
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
name = db.Column(db.String(100), nullable=False)
|
|
|
|
|
color = db.Column(db.String(50))
|
|
|
|
|
brand = db.Column(db.String(50))
|
|
|
|
|
price = db.Column(db.Float, nullable=False)
|
|
|
|
|
production_date = db.Column(db.Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
#增
|
|
|
|
|
@app.route('/add_jiaju', methods=['POST'])
|
|
|
|
|
def add_jiaju():
|
|
|
|
|
data = request.get_json() # 假设前端发送的是JSON数据
|
|
|
|
|
new_jiaju = JiaJu(
|
|
|
|
|
name=data['name'],
|
|
|
|
|
color=data.get('color'), # 如果color可选,则使用get
|
|
|
|
|
brand=data.get('brand'),
|
|
|
|
|
price=data['price'],
|
|
|
|
|
production_date=datetime.strptime(data['production_date'], '%Y-%m-%d') # 假定日期格式为YYYY-MM-DD
|
|
|
|
|
)
|
|
|
|
|
db.session.add(new_jiaju)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return jsonify({"message": "家居添加成功"}), 201
|
|
|
|
|
#查
|
|
|
|
|
@app.route('/jiaju', methods=['GET'])
|
|
|
|
|
def get_all_jiaju():
|
|
|
|
|
jiaju_list = JiaJu.query.all()
|
|
|
|
|
result = [{"id": item.id, "name": item.name, "color": item.color, "brand": item.brand,
|
|
|
|
|
"price": item.price, "production_date": item.production_date.strftime('%Y-%m-%d')} for item in jiaju_list]
|
|
|
|
|
return jsonify(result)
|
|
|
|
|
#获取
|
|
|
|
|
@app.route('/jiaju/<int:jiaju_id>', methods=['GET'])
|
|
|
|
|
def get_jiaju(jiaju_id):
|
|
|
|
|
jiaju = JiaJu.query.get_or_404(jiaju_id)
|
|
|
|
|
return jsonify({
|
|
|
|
|
"id": jiaju.id,
|
|
|
|
|
"name": jiaju.name,
|
|
|
|
|
"color": jiaju.color,
|
|
|
|
|
"brand": jiaju.brand,
|
|
|
|
|
"price": jiaju.price,
|
|
|
|
|
"production_date": jiaju.production_date.strftime('%Y-%m-%d')
|
|
|
|
|
})
|
|
|
|
|
#更
|
|
|
|
|
@app.route('/jiaju/<int:jiaju_id>', methods=['PUT'])
|
|
|
|
|
def update_jiaju(jiaju_id):
|
|
|
|
|
jiaju = JiaJu.query.get_or_404(jiaju_id)
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
if 'name' in data:
|
|
|
|
|
jiaju.name = data['name']
|
|
|
|
|
if 'color' in data:
|
|
|
|
|
jiaju.color = data['color']
|
|
|
|
|
if 'brand' in data:
|
|
|
|
|
jiaju.brand = data['brand']
|
|
|
|
|
if 'price' in data:
|
|
|
|
|
jiaju.price = data['price']
|
|
|
|
|
if 'production_date' in data:
|
|
|
|
|
jiaju.production_date = datetime.strptime(data['production_date'], '%Y-%m-%d')
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return jsonify({"message": "家居信息更新成功"})
|
|
|
|
|
#删
|
|
|
|
|
@app.route('/jiaju/<int:jiaju_id>', methods=['DELETE'])
|
|
|
|
|
def delete_jiaju(jiaju_id):
|
|
|
|
|
jiaju = JiaJu.query.get_or_404(jiaju_id)
|
|
|
|
|
db.session.delete(jiaju)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return jsonify({"message": "家居删除成功"})
|
|
|
|
|
|