|
|
@ -0,0 +1,77 @@ |
|
|
|
from sqlalchemy.exc import SQLAlchemyError |
|
|
|
from model import error |
|
|
|
from model import postgreSQLORM |
|
|
|
from model import db_conn |
|
|
|
from sqlalchemy import and_ |
|
|
|
|
|
|
|
class Seller(db_conn.DBConn): |
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
db_conn.DBConn.__init__(self) |
|
|
|
|
|
|
|
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.session.query(postgreSQLORM.Store_Book). |
|
|
|
new_book = postgreSQLORM.Store_Book(store_id=store_id,book_id=book_id,book_info=book_json_str,stock_level=stock_level) |
|
|
|
self.session.add(new_book) |
|
|
|
# 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() |
|
|
|
self.session.query(postgreSQLORM.Store).filter_by(store_id=store_id).update({'stock_level':postgreSQLORM.Store.stock_level+1}) |
|
|
|
self.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_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) |
|
|
|
self.session.query(postgreSQLORM.Store).filter_by(store_id=store_id).update({'stock_level':add_stock_level}) |
|
|
|
self.session.query(postgreSQLORM.Store_Book).filter_by(store_id=store_id,book_id=book_id).update({'stock_level':add_stock_level}) |
|
|
|
|
|
|
|
# self.conn.execute("UPDATE store SET stock_level = stock_level + ? " |
|
|
|
# "WHERE store_id = ? AND book_id = ?", (add_stock_level, store_id, book_id)) |
|
|
|
# self.conn.commit() |
|
|
|
self.session.commit() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
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) |
|
|
|
new_store = postgreSQLORM.Store(store_id=store_id,stock_level=0) |
|
|
|
new_user_store = postgreSQLORM.User_Store(fk_user_id=user_id,fk_store_id=store_id) |
|
|
|
self.session.add(new_store) |
|
|
|
# self.session.commit() |
|
|
|
self.session.add(new_user_store) |
|
|
|
# self.conn.execute("INSERT into user_store(store_id, user_id)" |
|
|
|
# "VALUES (?, ?)", (store_id, user_id)) |
|
|
|
# self.conn.commit() |
|
|
|
self.session.commit() |
|
|
|
# print('touch1') |
|
|
|
except SQLAlchemyError as e: |
|
|
|
# print('touch2') |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
# print('touch3') |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|
return 200, "ok" |