def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int, title: str, author: str, content: str, tag: str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not self.store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if self.book_id_exist(store_id, book_id): return error.error_exist_book_id(book_id) # self.conn.execute("INSERT into store(store_id, book_id, book_info, stock_level)" # "VALUES (?, ?, ?, ?)", (store_id, book_id, book_json_str, stock_level)) # self.conn.commit() store_tmp = Store(store_id=store_id, book_id=book_id, book_info=book_json_str, stock_level=stock_level, tag=tag, author=author, content=content, title=title) db_session.add(store_tmp) db_session.commit() except BaseException as e: print(str(e)) return 530, "{}".format(str(e)) return 200, "ok"
def register(self, user_id: str, password: str): try: terminal = "terminal_{}".format(str(time.time())) token = jwt_encode(user_id, terminal) user_tmp = User(user_id, password, 0, token, terminal) db_session.add(user_tmp) db_session.commit() except BaseException as e: return error.error_exist_user_id(user_id) return 200, "ok"
def get_book_info(self, start, size) -> [Book]:#得到爬虫爬到的db.book里的内容 conn = sqlite.connect(self.book_db) cursor = conn.execute( "SELECT id, title, author, " "publisher, original_title, " "translator, pub_year, pages, " "price, currency_unit, binding, " "isbn, author_intro, book_intro, " "content, tags FROM book ORDER BY id " "LIMIT ? OFFSET ?", (size, start)) for row in cursor: # book = Book() # book.id = row[0] # book.title = row[1] # book.author = row[2] # book.publisher = row[3] # book.original_title = row[4] # book.translator = row[5] # book.pub_year = row[6] # book.pages = row[7] # book.price = row[8] # # book.currency_unit = row[9] # book.binding = row[10] # book.isbn = row[11] # book.author_intro = row[12] # book.book_intro = row[13] # book.content = row[14] book_tag=[] tags = row[15] for tag in tags.split("\n"): if tag.strip() != "": book_tag.append(tag) # book.tags.append(tag) # for i in range(0, random.randint(0, 9)): # if picture is not None: # encode_str = base64.b64encode(picture).decode('utf-8') # book_pic.append(encode_str) # book.pictures.append(encode_str) book_tag=str(book_tag)#要转换成string类型 book = Book(book_id=row[0], title=row[1], author=row[2], publisher=row[3], original_title=row[4],translator=row[5],pub_year=row[6], pages=row[7], price=row[8], currency_unit=row[9], binding=row[10],isbn=row[11],author_intro=row[12], book_intro=row[13], content=row[14],tags=book_tag) db_session.add(book) db_session.commit()
def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) # self.conn.execute("INSERT into user_store(store_id, user_id)" # "VALUES (?, ?)", (store_id, user_id)) # self.conn.commit() user_store = User_Store(user_id=user_id, store_id=store_id) db_session.add(user_store) db_session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
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())) for book_id, count in id_and_count: # cursor = self.conn.execute( # "SELECT book_id, stock_level, book_info FROM store " # "WHERE store_id = ? AND book_id = ?;", # (store_id, book_id)) row = Store.query.filter_by(store_id=store_id, book_id=book_id).first() if row is None: return error.error_non_exist_book_id(book_id) + (order_id,) 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,) # 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)) row = Store.query.filter_by(store_id=store_id, book_id=book_id, stock_level=stock_level).update( {'stock_level': stock_level - count}) if row == 0: return error.error_stock_level_low(book_id) + (order_id,) # self.conn.execute( # "INSERT INTO new_order_detail(order_id, book_id, count, price) " # "VALUES(?, ?, ?, ?);", # (uid, book_id, count, price)) row = New_Order_Detail(order_id=uid, book_id=book_id, count=count, price=price, state=0, time=time.time()) db_session.add(row) # self.conn.execute( # "INSERT INTO new_order(order_id, store_id, user_id) " # "VALUES(?, ?, ?);", # (uid, store_id, user_id)) # self.conn.commit() row = New_Order(order_id=uid, store_id=store_id, user_id=user_id) db_session.add(row) order_id = uid db_session.commit() # except sqlite.Error as e: # logging.info("528, {}".format(str(e))) # return 528, "{}".format(str(e)), "" except BaseException as e: print(e) logging.info("530, {}".format(str(e))) return 530, "{}".format(str(e)), "" return 200, "ok", order_id
from be.model.database import db_session from be.table.user import User u = User(123, 231, 30,'sa', 'dsa') db_session.add(u) db_session.commit() # from be.model.db_conn import DBConn # a = DBConn.user_id_exist(user_id=0) # print(a)