def create_address(session: Session, address: AddressModel): """新增收货地址 :param session: db session :param address: 收货地址 :return:None """ session.add(address)
def create_account(session: Session, account: AccountModel): """新增用户账户 :param session:db session :param account:账户信息 :return:None """ session.add(account)
def recharge(session: Session, account: AccountModel): """充值账户 :param session: db session :param account: 要充值的账户以及充值金额 :return: None """ old = (session.query(AccountModel).filter( AccountModel.mobile == account.mobile).first()) old.surplus = old.surplus + account.surplus session.commit()
def cancel(session: Session, order: OrderModel): """ 撤销订单 :param session: db session :param order: 订单 :return: """ exist_order = (session.query(OrderModel).filter( OrderModel.order_id == order.order_id).first()) exist_order.status = 2 # 已取消 session.commit()
def delete(session: Session, cart: CartModel): """ 从购物车删除图书 :param session: db session :param cart: 要删除的用户和图书 :return: """ exist = ( session.query(CartModel) .filter(CartModel.mobile == cart.mobile) .filter(CartModel.book_id == cart.book_id) .first() ) if exist is not None: session.delete(exist)
def get_book_by_category(session: Session, category: str) -> []: """ 根据图书类型查询 :param session: db session :param category: 图书类型 :return: BookModel List """ return session.query(BookModel).filter(BookModel.category == category)
def add(session: Session, cart: CartModel): """ 添加到购物车,如果有了的话只需要增加数量 :param session: db session :param cart: 要增加的用户和图书 :return: """ exist = ( session.query(CartModel) .filter(CartModel.mobile == cart.mobile) .filter(CartModel.book_id == cart.book_id) .first() ) if exist is not None: exist.count += cart.count else: exist = cart session.commit()
def pay(session: Session, order: OrderModel): """ 支付订单 :param session: db session :param order: 订单 :return: """ # 实际上这里需要大量的校验,校验逻辑应该在外层就完成, # 比如余额要大于订单金额、订单状态是否正常之类的, # 暂时先省略,以后再补上 exist_order = (session.query(OrderModel).filter( OrderModel.order_id == order.order_id).first()) exist_order.status = 1 # 已支付 account = (session.query(AccountModel).filter( AccountModel.mobile == exist_order.mobile).first()) account.surplus -= order.fee session.commit()
def get_account_by_mobile(session: Session, mobile: str) -> AccountModel: """查询账户 :param session: db session :param mobile: 账户手机号 :return: AccountModel """ return (session.query(AccountModel).filter( AccountModel.mobile == mobile).first())
def get(self): try: user = self.get_current_user() s = Session() account = get_account_by_mobile(s, user) self.response(Result.success(account)) except Exception as ex: logging.exception("query surplus error!", exc_info=True) self.response(Result.error("query surplus error."))
def get_book_by_id(session: Session, book_id: str) -> BookModel: """ 根据图书id查询 :param session: db session :param book_id: 图书类型 :return: BookModel """ return session.query(BookModel).filter( BookModel.book_id == book_id).first()
def post(self): try: mobile = self.get_current_user() s = Session() info = get_info_by_mobile(mobile) self.response(Result.success(info)) except Exception as ex: logging.exception("query info error!", exc_info=True) self.response(Result.error("query info error."))
def post(self): try: s = Session() category = self.get_body_argument("category") books = get_book_by_category(category) self.response(Result.success(books)) except Exception as ex: logging.exception("query books error!", exc_info=True) self.response(Result.error("query books error."))
def post(self): try: s = Session() book_id = self.get_body_argument("book_id") book = get_book_by_id(book_id) self.response(Result.success(book)) except Exception as ex: logging.exception("query book error!", exc_info=True) self.response(Result.error("query book error."))
def post(self): try: s = Session() address = get_params(self) create_address(s, address) self.response(Result.success("success")) except Exception as ex: logging.exception("add address error!", exc_info=True) self.response(Result.error("add address error."))
def get_info_by_mobile(session: Session, mobile: str): """查询用户基础信息和用户账户余额 用于个人信息页面展示 """ return (session.query( AccountModel.mobile, AccountModel.surplus, ProfileModel.nick_name, ProfileModel.whats_up, ).join(ProfileModel).filter(AccountModel.mobile == mobile).first())
def post(self): try: mobile = self.get_current_user() s = Session() book_id = self.get_body_argument("book_id") cart = CartModel(mobile, book_id, 1, datetime.datetime.now()) delete(cart) self.response(Result.success("success")) except Exception as ex: logging.exception("remove cart error!", exc_info=True) self.response(Result.error("remove cart error."))
def post(self): try: s = Session() order = get_order_from_params(self) order_details = get_order_detail_from_params(self, order) order.fee = sum([detail.price for detail in order_details]) create(s, order, order_details) self.response(Result.success("success")) except Exception as ex: logging.exception("create order error!", exc_info=True) self.response(Result.error("create order error."))
def post(self): try: s = Session() # 只是需要一个 Order model,所以用虚假的数据生成一个 mobile = self.get_current_user() order = OrderModel(mobile, 1, 0) order.order_id = self.get_body_argument("order_id") cancel(s, order) self.response(Result.success("success")) except Exception as ex: logging.exception("cancel order error!", exc_info=True) self.response(Result.error("cancel order error."))
def get_by_mobile(session: Session, mobile: str) -> CartModel: """ 根据查询手机号查询用户购物车 :param session: db session :param mobile: 手机号 :return: CartModel """ return ( session.query(CartModel, BookModel) .filter(CartModel.mobile == mobile) .all() )
def post(self): try: user = self.get_current_user() mobile = self.get_body_argument("mobile") fee = self.get_body_argument("fee") if mobile != user: self.response( Result.error("Not same user, please logout and re-login.")) s = Session() recharge(s, AccountModel(mobile, fee)) self.response(Result.success("success")) except Exception as ex: logging.exception("signup error!", exc_info=True) self.response(Result.error("SignUp error."))
def create(session: Session, order: OrderModel, order_details: []): """ 创建订单 :param session: db session :param order: 订单 :param order_details: 订单详情 :return: """ session.add(order) session.add_all(order_details) # 批量插入 session.commit()
def post(self): try: s = Session() profile = get_params(self) if exist(s, profile.mobile): self.response(Result.error("mobile has been used.")) return new_profile = ProfileModel(profile.mobile, "", profile.password) new_profile.salt = generate_random_string() new_profile.password = encrypt_password(new_profile.password, new_profile.salt) create_profile(s, new_profile) self.response(Result.success("success")) except Exception as ex: logging.exception("signup error!", exc_info=True) self.response(Result.error("SignUp error."))
def post(self): try: s = Session() profile = get_params(self) account = get_profile_by_mobile(s, profile.mobile) if account is None: # 手机号没注册,需要先注册,跳转到注册页面 self.response(Result.error("Mobile not find")) return is_right = is_right_password(profile.password, account.salt, account.password) if not is_right: self.response(Result.error("password not correct!")) return # 可以再做一个全局dict维护登陆状态并用guid代替mobile,提高速度和安全性 self.set_secure_cookie("dejavu_user", profile.mobile) self.response(Result.success("success")) except Exception as ex: logging.exception("login error!", exc_info=True) self.response(Result.error("Some error, please try later."))