def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): book_json = json.loads(book_json_str) if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if book_id_exist(store_id, book_id): return error.error_exist_book_id(book_id) book_one = Book(book_id=book_id, title=book_json.get("title"), author=book_json.get("author"), publisher=book_json.get("publisher"), original_title=book_json.get("original_title"), translator=book_json.get("translator"), pub_year=book_json.get("pub_year"), pages=book_json.get("pages"), original_price=book_json.get("price"), currency_unit=book_json.get("currency_unit"), binding=book_json.get("binding"), isbn=book_json.get("isbn"), author_intro=book_json.get("author_intro"), book_intro=book_json.get("book_intro"), content=book_json.get("content"), tags=book_json.get("tags"), picture=book_json.get("picture")) # if not session.query(Book).filter(Book.book_id == book_id).first(): # session.add(book_one) store_detail_one = Store_detail(store_id=store_id, book_id=book_id, stock_level=stock_level, price=book_json.get("price")) session.add(store_detail_one) session.commit() return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): book_json = json.loads(book_json_str) # try: if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if book_id_exist(store_id, book_id): return error.error_exist_book_id(book_id) book_one = Book(book_id = book_id,title = book_json.get("title")) if not session.query(Book).filter(Book.book_id==book_id).first(): session.add(book_one) session.commit() store_detail_one=Store_detail( store_id = store_id, book_id = book_id, stock_level = stock_level, price = book_json.get("price") ) session.add(store_detail_one) session.commit() # except BaseException as e: # return 530, "{}".format(str(e)) return 200, "ok"
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 add_book(self, user_id: str, store_id: str, book_info: dict, stock_level: int): try: book_id = book_info.get("id") 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) book = Book_info() book.id = book_id book.title = book_info.get("title") book.store_id = store_id book.author = book_info.get("author", None) book.publisher = book_info.get("publisher", None) book.original_title = book_info.get("original_title", None) book.translator = book_info.get("translator", None) book.pub_year = book_info.get("pub_year", None) book.pages = book_info.get("pages", 0) book.binding = book_info.get("binding", None) book.isbn = book_info.get("isbn", None) book.author_intro = book_info.get("author_intro", None) book.book_intro = book_info.get("book_intro", None) book.content = book_info.get("content", None) book.inventory_count = stock_level book.price = book_info.get("price", 0) self.session.add(book) book.tags = book_info.get("tags", []) for tag in book.tags: book_tag = Book_tag() book_tag.id = book.id book_tag.store_id = store_id book_tag.tag = tag self.session.add(book_tag) pictures = book_info.get("pictures", []) for pic in pictures: book_pic = Book_pic() book_pic.book_id = book.id book_pic.store_id = store_id book_pic.picture = pic.encode('ascii') self.session.add(book_pic) self.session.commit() self.session.close() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): 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.cursor = self.conn.cursor() self.cursor.execute( "INSERT into store(store_id, book_id, book_info, stock_level)" "VALUES ('%s', '%s', '%s',%d)" % (store_id, book_id, book_json_str, stock_level)) self.conn.commit() except psycopg2.errors.UniqueViolation: return error.error_exist_book_id(book_id) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int) -> (int, 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 ('%s', '%s', '%s', %d)" % ( store_id, book_id, book_json_str, stock_level)) self.conn.commit() except sqlalchemy.exc.IntegrityError as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): 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) obj = Store(store_id=store_id, book_id=book_id, book_info=book_json_str, stock_level=stock_level) self.Session.add(obj) this_book = self.Session.query(Book).filter( Book.book_id == book_id).first() # print('title长这样:', this_book.title) # print('书长这样:', this_book.picture) book_onsale_obj = Book_Onsale(store_id=store_id, book_id=book_id, title=this_book.title, author=this_book.author, publisher=this_book.publisher, translator=this_book.translator, pub_year=this_book.pub_year, pages=this_book.pages, price=this_book.price, binding=this_book.binding, isbn=this_book.isbn, author_intro=this_book.author_intro, book_intro=this_book.book_intro, content=this_book.content, tags=this_book.tags, picture=this_book.picture) self.Session.add(book_onsale_obj) # self.Session.execute("INSERT into store(store_id, book_id, book_info, stock_level)" # "VALUES (?, ?, ?, ?)", (store_id, book_id, book_json_str, stock_level)) self.Session.commit() except sqlalchemy.exc.IntegrityError as e: return 528, "{}".format(str(e)) except BaseException as e: print('加书出错:', e) return 530, "{}".format(str(e)) return 200, "ok"
def add_stock_level(self, user_id: str, store_id: str, book_id: str, add_stock_level: int): 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 not self.book_id_exist(store_id, book_id): return error.error_non_exist_book_id(book_id) #将stock_level的值变成stock_level+add_store_level self.cursor = self.conn.cursor() self.cursor.execute( "UPDATE store SET stock_level = stock_level + %d " "WHERE store_id = '%s' AND book_id = '%s'" % (add_stock_level, store_id, book_id)) self.conn.commit() except psycopg2.errors.UniqueViolation: return error.error_exist_book_id(book_id) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): 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.cursor.execute( "INSERT into store(store_id, book_id, book_info, stock_level)" "VALUES (%s, %s, %s, %s)", (store_id, book_id, book_json_str, stock_level)) self.conn.commit() except pymysql.Error as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): try: if not self.user_store_exist(user_id, store_id): 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) return error.error_and_message( 520, error.error_code[520].format(user_id, store_id)) if self.book_id_exist(store_id, book_id): return error.error_exist_book_id(book_id) cur = self.conn.cursor() cur.execute( "INSERT into \"store\"(store_id, book_id, book_info, stock_level)" "VALUES (%s, %s, %s, %s)", (store_id, book_id, book_json_str, stock_level)) self.conn.commit() except (Exception, psycopg2.DatabaseError) as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def add_book(self, user_id: str, store_id: str, book_id: str, stock_level: int, price: int): 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) obj = Store(store_id=store_id, book_id=book_id, stock_level=stock_level, price=price) self.Session.add(obj) this_book = self.Session.query(Book).filter( Book.book_id == book_id).first() if this_book.book_intro: this_book.book_intro = str(this_book.book_intro) if this_book.content: this_book.content = str(this_book.book_intro) book_onsale_obj = Book_Onsale(store_id=store_id, book_id=book_id, title=this_book.title, author=this_book.author, translator=this_book.translator, price=price, book_intro=this_book.book_intro, content=this_book.content, tags=this_book.tags) self.Session.add(book_onsale_obj) self.Session.commit() except sqlalchemy.exc.IntegrityError as e: return 528, "{}".format(str(e)) except BaseException as e: print('加书出错:', e) return 530, "{}".format(str(e)) return 200, "ok"