34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from app.services.stock_service import create_inbound_stock
|
|
from app.schemas.stock_schema import StockBuySchema
|
|
|
|
stock_bp = Blueprint('stocks', __name__)
|
|
|
|
@stock_bp.route('/buy-inbound', methods=['POST'])
|
|
def buy_inbound():
|
|
"""
|
|
采购入库接口
|
|
POST /api/v1/buy-inbound
|
|
Body: { "material_id": 1, "qty_inbound": 100, "price_unit": 10.5 ... }
|
|
"""
|
|
# 1. 接收 JSON 数据
|
|
json_data = request.get_json()
|
|
if not json_data:
|
|
return jsonify({"message": "No input data provided"}), 400
|
|
|
|
# 2. 数据校验
|
|
schema = StockBuySchema()
|
|
try:
|
|
# 这一步只做校验,不直接生成对象,因为我们要在 Service 里手动处理逻辑
|
|
data = schema.load(json_data, partial=True)
|
|
except Exception as e:
|
|
return jsonify({"message": "Validation error", "errors": e.messages}), 422
|
|
|
|
# 3. 调用业务逻辑
|
|
try:
|
|
new_stock = create_inbound_stock(data)
|
|
# 4. 返回成功结果
|
|
result = schema.dump(new_stock)
|
|
return jsonify({"message": "Inbound successful", "data": result}), 201
|
|
except Exception as e:
|
|
return jsonify({"message": "Internal Server Error", "error": str(e)}), 500 |