def post(self): """ 书籍添加 --- tags: - 书籍相关接口 description: 书籍添加接口,json格式 responses: 201: description: 添加书籍成功 401: description: 没有权限 """ params = request.get_json() # check_user() name = params.get("name") category = params.get("category") price = params.get("price") book = Book(name=name, category=category, price=price, user_id=g.user_id) db.session.add(book) db.session.commit() return success(message="新增一本书籍成功", data=book.to_dict())
def post(self): """ 用户注册 --- tags: - 用户相关接口 description: 用户注册接口,json格式 parameters: - name: body in: body required: true schema: id: 用户注册 required: - nickname - password - mobile properties: nickname: type: string description: 昵称 password: type: string description: 密码 mobile: type: string description: 手机号. responses: 201: description: 注册成功 406: description: 注册参数有误 """ params = request.get_json() mobile = params.get("mobile") nickname = params.get("nickname") password = params.get("password") # if User.query.filter_by(mobile=mobile).first(): # return redirect(url_for('admin.signin')) user = User(mobile=mobile,nickname=nickname,password=password) db.session.add(user) db.session.commit() return success(message="注册成功!",data=user.to_dict())
def post(self): """ 用户登录 --- tags: - 用户相关接口 description: 用户登录接口,json格式 parameters: - name: body in: body required: true schema: id: 用户登录 required: - password - mobile properties: password: type: string description: 密码 mobile: type: string description: 手机号 responses: 201: description: '登录成功' 406: description: 登录参数有误 """ params = request.get_json() mobile = params.get("mobile") password = params.get("password") user = User.query.filter(User.mobile == mobile).first() if not user: return restful_result(data=None,code="-1",message="Non-existent user, please register!") if user.password!=password: return restful_result(data=None,code="-1",message="Wrong password!") session["user_id"] = user.id session["mobile"] = user.mobile db.session.add(user) db.session.commit() return success(message="登录成功!",data=user.to_dict())
def get(self, id): """ 书籍查询 --- tags: - 书籍相关接口 description: 书籍查询接口,json格式 responses: 200: description: 获取书籍成功 schema: id: 所有书籍列表 properties: name: type: string description: 书名 # example: Python Cookbook id: type: integer description: 书号 # example:1 category: type: string description: 类别 # example: programming price: type: float description: 价格 # example: 10.50 401: description: 没有权限 """ # check_user() book = db.session.query(Book).get(id) if book and book.user_id == g.user_id: return success(data=book.to_dict()) else: return restful_result(None, "-1", "Not authorized to check this book!")
def get(self): """ 书籍列表 --- tags: - 书籍相关接口 description: 书籍列表接口,json格式 responses: 200: description: 获取书籍列表成功 schema: id: 所有书籍列表 properties: name: type: string description: 书名 # example: Python Cookbook id: type: integer description: 书号 # example:1 category: type: string description: 类别 # example: programming price: type: float description: 价格 # example: 10.50 401: description: 登录状态有误 """ books = db.session.query(Book).filter(Book.user_id == g.user_id).all() data = [i.to_dict() for i in books] return success(data=data)
def put(self, id): """ 书籍修改 --- tags: - 书籍相关接口 description: 书籍修改接口,json格式 responses: 201: description: 修改书籍成功 401: description: 没有权限 """ params = request.get_json() book = db.session.query(Book).get(id) price = params.get("price") # check_user() if book.user_id == g.user_id: book.price = price db.session.commit() return success(message="修改价格成功!", data=book.to_dict())