Browse Source

完成buyer路由接口并测试成功

master
杨舜 1 year ago
parent
commit
310a6a886c
8 changed files with 259 additions and 1 deletions
  1. BIN
      figure_require/addfunds_test.png
  2. BIN
      figure_require/neworder_test.png
  3. BIN
      figure_require/payment_test.png
  4. BIN
      modified/be/model/__pycache__/buyer.cpython-38.pyc
  5. +215
    -0
      modified/be/model/buyer.py
  6. BIN
      modified/be/view/__pycache__/buyer.cpython-38.pyc
  7. +38
    -0
      modified/be/view/buyer.py
  8. +6
    -1
      report.md

BIN
figure_require/addfunds_test.png View File

Before After
Width: 728  |  Height: 498  |  Size: 38 KiB

BIN
figure_require/neworder_test.png View File

Before After
Width: 729  |  Height: 475  |  Size: 38 KiB

BIN
figure_require/payment_test.png View File

Before After
Width: 745  |  Height: 512  |  Size: 43 KiB

BIN
modified/be/model/__pycache__/buyer.cpython-38.pyc View File


+ 215
- 0
modified/be/model/buyer.py View File

@ -0,0 +1,215 @@
import sqlite3 as sqlite
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import and_
from model import postgreSQLORM
from model.postgreSQLORM import Store_Book
from model.postgreSQLORM import New_Order_Detail
from model.postgreSQLORM import New_Order
from model.postgreSQLORM import User
import uuid
import json
import logging
from model import db_conn
from model import error
class Buyer(db_conn.DBConn):
def __init__(self):
db_conn.DBConn.__init__(self)
def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str):
order_id = ""
try:
if not self.user_id_exist(user_id):
return error.error_non_exist_user_id(user_id) + (order_id, )
if not self.store_id_exist(store_id):
return error.error_non_exist_store_id(store_id) + (order_id, )
uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1()))
# print("touch0")
for book_id, count in id_and_count:
row = self.session.query(postgreSQLORM.Store_Book).filter(and_(postgreSQLORM.Store_Book.store_id==store_id,postgreSQLORM.Store_Book.book_id==book_id)).first()
# cursor = self.conn.execute(
# "SELECT book_id, stock_level, book_info FROM store "
# "WHERE store_id = ? AND book_id = ?;",
# (store_id, book_id))
# row = cursor.fetchone()
if row is None:
return error.error_non_exist_book_id(book_id) + (order_id, )
# stock_level = row[1]
# book_info = row[2]
stock_level = row.stock_level
book_info = row.book_info
book_info_json = json.loads(book_info)
price = book_info_json.get("price")
if stock_level < count:
return error.error_stock_level_low(book_id) + (order_id,)
# print("touch2")
row = self.session.query(Store_Book).filter(and_(Store_Book.store_id==store_id,Store_Book.book_id==book_id,Store_Book.stock_level>=count)).update({'stock_level':Store_Book.stock_level-count})
# cursor = self.conn.execute(
# "UPDATE store set stock_level = stock_level - ? "
# "WHERE store_id = ? and book_id = ? and stock_level >= ?; ",
# (count, store_id, book_id, count))
# if cursor.rowcount == 0:
# print("touch3")
if row == 0:
return error.error_stock_level_low(book_id) + (order_id, )
# print("touch4")
new_order_detail_entity = New_Order_Detail(order_id=uid,book_id=book_id,count=count,price=price)
# print("touch5")
row = self.session.add(new_order_detail_entity)
# self.session.commit()
# self.conn.execute(
# "INSERT INTO new_order_detail(order_id, book_id, count, price) "
# "VALUES(?, ?, ?, ?);",
# (uid, book_id, count, price))
# print("touch1")
new_order_entity = New_Order(order_id=uid,fk_store_id=store_id,fk_user_id=user_id)
self.session.add(new_order_entity)
# self.conn.execute(
# "INSERT INTO new_order(order_id, store_id, user_id) "
# "VALUES(?, ?, ?);",
# (uid, store_id, user_id))
# self.conn.commit()
self.session.commit()
order_id = uid
except SQLAlchemyError as e:
logging.info("528, {}".format(str(e)))
return 528, "{}".format(str(e)), ""
except BaseException as e:
logging.info("530, {}".format(str(e)))
return 530, "{}".format(str(e)), ""
return 200, "ok", order_id
def payment(self, user_id: str, password: str, order_id: str) -> (int, str):
# conn = self.conn
session = self.session
try:
row = session.query(New_Order).filter(New_Order.order_id==order_id).first()
# cursor = conn.execute("SELECT order_id, user_id, store_id FROM new_order WHERE order_id = ?", (order_id,))
# row = cursor.fetchone()
if row is None:
return error.error_invalid_order_id(order_id)
# order_id = row[0]
# buyer_id = row[1]
# store_id = row[2]
order_id = row.order_id
buyer_id = row.fk_user_id
store_id = row.fk_store_id
if buyer_id != user_id:
return error.error_authorization_fail()
row = session.query(postgreSQLORM.User).filter(postgreSQLORM.User.user_id==buyer_id).first()
# cursor = conn.execute("SELECT balance, password FROM user WHERE user_id = ?;", (buyer_id,))
# row = cursor.fetchone()
if row is None:
return error.error_non_exist_user_id(buyer_id)
# balance = row[0]
balance = row.balance
pwd = row.password
if password != pwd:
return error.error_authorization_fail()
row = session.query(postgreSQLORM.User_Store).filter(postgreSQLORM.User_Store.fk_store_id==store_id).first()
# cursor = conn.execute("SELECT store_id, user_id FROM user_store WHERE store_id = ?;", (store_id,))
# row = cursor.fetchone()
if row is None:
return error.error_non_exist_store_id(store_id)
# seller_id = row[1]
seller_id = row.fk_user_id
if not self.user_id_exist(seller_id):
return error.error_non_exist_user_id(seller_id)
row = session.query(New_Order_Detail).filter(New_Order_Detail.order_id==order_id).all()
# cursor = conn.execute("SELECT book_id, count, price FROM new_order_detail WHERE order_id = ?;", (order_id,))
total_price = 0
# for row in cursor:
# count = row[1]
# price = row[2]
# total_price = total_price + price * count
for i in row:
count = i.count
price = i.price
total_price = total_price + price * count
if balance < total_price:
return error.error_not_sufficient_funds(order_id)
# print('touch0')
## 买家扣钱
row = session.query(User).filter(and_(User.balance>=total_price,User.user_id==buyer_id)).update({'balance':User.balance-total_price})
# cursor = conn.execute("UPDATE user set balance = balance - ?"
# "WHERE user_id = ? AND balance >= ?",
# (total_price, buyer_id, total_price))
# if cursor.rowcount == 0:
if row == 0:
return error.error_not_sufficient_funds(order_id)
# print('touch1')
## 卖家加钱
row = session.query(User).filter(User.user_id==seller_id).update({'balance':User.balance+total_price})
# cursor = conn.execute("UPDATE user set balance = balance + ?"
# "WHERE user_id = ?",
# (total_price, buyer_id))
# if cursor.rowcount == 0:
if row == 0:
return error.error_non_exist_user_id(buyer_id)
# print('touch2')
row = session.query(New_Order).filter(New_Order.order_id==order_id).delete()
# cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id, ))
# if cursor.rowcount == 0:
if row == 0:
return error.error_invalid_order_id(order_id)
row = session.query(New_Order_Detail).filter(New_Order_Detail.order_id==order_id).delete()
# cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id, ))
# if cursor.rowcount == 0:
if row == 0:
return error.error_invalid_order_id(order_id)
# conn.commit()
session.commit()
except SQLAlchemyError as e:
return 528, "{}".format(str(e))
except BaseException as e:
return 530, "{}".format(str(e))
return 200, "ok"
def add_funds(self, user_id, password, add_value) -> (int, str):
try:
row = self.session.query(postgreSQLORM.User).filter(postgreSQLORM.User.user_id==user_id).first()
# cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,))
# row = cursor.fetchone()
if row is None:
return error.error_authorization_fail()
pwd = row.password
if pwd != password:
return error.error_authorization_fail()
row = self.session.query(postgreSQLORM.User).filter_by(user_id=user_id).update({'balance':postgreSQLORM.User.balance+add_value,'user_id':user_id})
# cursor = self.conn.execute(
# "UPDATE user SET balance = balance + ? WHERE user_id = ?",
# (add_value, user_id))
if row == 0:
return error.error_non_exist_user_id(user_id)
self.session.commit()
# self.conn.commit()
except SQLAlchemyError as e:
return 528, "{}".format(str(e))
except BaseException as e:
return 530, "{}".format(str(e))
return 200, "ok"

BIN
modified/be/view/__pycache__/buyer.cpython-38.pyc View File


+ 38
- 0
modified/be/view/buyer.py View File

@ -1,4 +1,42 @@
from flask import Blueprint
from flask import request
from flask import jsonify
from model.buyer import Buyer
bp_buyer = Blueprint("buyer",__name__,url_prefix="/buyer")
@bp_buyer.route("/new_order", methods=["POST"])
def new_order():
user_id: str = request.json.get("user_id")
store_id: str = request.json.get("store_id")
books: [] = request.json.get("books")
id_and_count = []
for book in books:
book_id = book.get("id")
count = book.get("count")
id_and_count.append((book_id, count))
b = Buyer()
code, message, order_id = b.new_order(user_id, store_id, id_and_count)
return jsonify({"message": message, "order_id": order_id}), code
@bp_buyer.route("/payment", methods=["POST"])
def payment():
user_id: str = request.json.get("user_id")
order_id: str = request.json.get("order_id")
password: str = request.json.get("password")
b = Buyer()
code, message = b.payment(user_id, password, order_id)
return jsonify({"message": message}), code
@bp_buyer.route("/add_funds", methods=["POST"])
def add_funds():
user_id = request.json.get("user_id")
password = request.json.get("password")
add_value = request.json.get("add_value")
b = Buyer()
code, message = b.add_funds(user_id, password, add_value)
return jsonify({"message": message}), code

+ 6
- 1
report.md View File

@ -266,4 +266,9 @@
![avatar](./figure_require/addbook_test.png)
![avatar](./figure_require/addstocklevel_test.png)
9.
9. 利用上述类似的实现auth路由接口的方式完成buyer路由接口,并利用postman测试实现(2022.12.02 12:10 杨舜)
![avatar](./figure_require/neworder_test.png)
![avatar](./figure_require/addfunds_test.png)
![avatar](./figure_require/payment_test.png)
10.

Loading…
Cancel
Save