57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from app.services.stock_service import StockService
|
|
from app.schemas.stock_schema import stock_buy_schema
|
|
|
|
stocks_bp = Blueprint('stocks', __name__)
|
|
|
|
|
|
@stocks_bp.route('/inbound', methods=['GET'])
|
|
def get_inbound_list():
|
|
page = request.args.get('page', 1, type=int)
|
|
limit = request.args.get('pageSize', 10, type=int)
|
|
|
|
result = StockService.get_list(page, limit)
|
|
|
|
return jsonify({
|
|
'code': 200,
|
|
'msg': 'success',
|
|
'data': result
|
|
})
|
|
|
|
|
|
@stocks_bp.route('/inbound', methods=['POST'])
|
|
def create_inbound():
|
|
json_data = request.get_json()
|
|
try:
|
|
# 1. 参数校验
|
|
data = stock_buy_schema.load(json_data)
|
|
# 2. 调用业务逻辑
|
|
new_stock = StockService.create_inbound(data)
|
|
# 3. 返回成功
|
|
return jsonify({
|
|
'code': 200,
|
|
'msg': '入库成功',
|
|
'data': new_stock.to_dict()
|
|
}), 201
|
|
except Exception as e:
|
|
# 捕获 ValueError 或 SQLAlchemyError
|
|
return jsonify({'code': 400, 'msg': str(e)}), 400
|
|
|
|
|
|
@stocks_bp.route('/inbound/<int:id>', methods=['PUT'])
|
|
def update_inbound(id):
|
|
json_data = request.get_json()
|
|
try:
|
|
StockService.update_inbound(id, json_data)
|
|
return jsonify({'code': 200, 'msg': '更新成功'})
|
|
except Exception as e:
|
|
return jsonify({'code': 400, 'msg': str(e)}), 400
|
|
|
|
|
|
@stocks_bp.route('/inbound/<int:id>', methods=['DELETE'])
|
|
def delete_inbound(id):
|
|
try:
|
|
StockService.delete_inbound(id)
|
|
return jsonify({'code': 200, 'msg': '删除成功'})
|
|
except Exception as e:
|
|
return jsonify({'code': 400, 'msg': str(e)}), 400 |