Exemple #1
0
 def delete(self, id):
     # 删除单条数据
     author = AuthorModel.query.get(id)
     db.session.delete(author)
     db.session.commit()
     res_data = response_200(data=author.nickname)
     return make_response(jsonify(res_data))
Exemple #2
0
 def delete(self, id):
     # 删除单条数据
     user = UserModel.query.get(id)
     db.session.delete(user)
     db.session.commit()
     res_data = response_200(data=user.username)
     return make_response(jsonify(res_data))
Exemple #3
0
 def post(self):
     # 新增数据
     data = request.get_json()
     author = AuthorSchema().load(data)
     db.session.add(author)
     db.session.commit()
     author_data = AuthorSchema().dump(author)
     res_data = response_200({"author": author_data})
     return make_response(jsonify(res_data))
Exemple #4
0
 def get(self):
     # 返回所有数据
     try:
         book_list = BookModel.query.all()
         book_data = BookSchema().dump(book_list, many=True)
         res_data = response_200({"books": book_data})
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #5
0
 def get(self, id):
     # 返回单条数据
     try:
         book = BookModel.query.get(id)
         if not book:
             res_data = response_400(msg="book not found")
             return make_response(jsonify(res_data))
         book_data = BookSchema().dump(book)
         res_data = response_200({"authors": book_data})
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #6
0
 def put(self, id):
     # 修改单条数据
     data = request.get_json()
     AuthorSchema().validate(data)
     author = AuthorModel.query.get(id)
     author_json = AuthorSchema().dump(author)
     for field in author_json:
         if field in data:
             setattr(author, field, data[field])
     db.session.add(author)
     db.session.commit()
     author_data = AuthorSchema().dump(author)
     res_data = response_200({"author": author_data})
     return make_response(jsonify(res_data))
Exemple #7
0
 def delete(self, id):
     # 删除单条数据
     try:
         book = BookModel.query.get(id)
         if not book:
             res_data = response_400(msg="book not found")
             return make_response(jsonify(res_data))
         db.session.delete(book)
         db.session.commit()
         res_data = response_200(data=book.title)
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #8
0
 def put(self, id):
     # 修改单条数据
     data = request.get_json()
     UserSchema().validate(data)
     user = UserModel.query.get(id)
     user_json = UserSchema().dump(user)
     for field in user_json:
         if field in data:
             setattr(user, field, data[field])
     db.session.add(user)
     db.session.commit()
     user_data = UserSchema().dump(user)
     res_data = response_200({"authors": user_data})
     return make_response(jsonify(res_data))
Exemple #9
0
 def post(self):
     # 新增数据
     data = request.get_json()
     error = BookSchema().validate(data)
     if error:
         res_data = response_400(msg=str(error))
         return make_response(jsonify(res_data))
     try:
         book = BookSchema().load(data)
         db.session.add(book)
         db.session.commit()
         book_data = BookSchema().dump(book)
         res_data = response_200({"book": book_data})
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #10
0
 def post(self):
     try:
         data = request.get_json()
         error = UserSchema().validate(data)
         if error:
             res_data = response_400(msg=str(error))
             return make_response(jsonify(res_data))
         data["password"] = UserModel.generate_password(
             password=data["password"])
         print(data)
         user = UserSchema().load(data)
         db.session.add(user)
         db.session.commit()
         user_data = UserSchema().dump(user)
         res_data = response_200({"user": user_data})
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #11
0
 def post(self):
     try:
         data = request.get_json()
         error = LoginSchema().validate(data)
         if error:
             res_data = response_400(msg=str(error))
             return make_response(jsonify(res_data))
         user = UserModel.query.filter_by(email=data["email"]).first()
         if not user:
             res_data = response_400(msg="user not found")
             return make_response(jsonify(res_data))
         if user.check_password(user.password_hash, data["password"]):
             access_token = create_access_token(identity=data["email"])
             res_data = response_200(data={"token": access_token})
             return make_response(jsonify(res_data))
         else:
             res_data = response_400(
                 msg="email or the password is incorrect")
             return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #12
0
 def put(self, id):
     # 修改单条数据
     data = request.get_json()
     error = BookSchema().validate(data)
     if error:
         res_data = response_400(msg=str(error))
         return make_response(jsonify(res_data))
     try:
         book = BookModel.query.get(id)
         if not book:
             res_data = response_400(msg="book not found")
             return make_response(jsonify(res_data))
         book_json = BookSchema().dump(book)
         for field in book_json:
             if field in data:
                 setattr(book, field, data[field])
         db.session.add(book)
         db.session.commit()
         book_data = BookSchema().dump(book)
         res_data = response_200({"authors": book_data})
         return make_response(jsonify(res_data))
     except Exception as e:
         res_data = response_400(msg=str(e))
         return make_response(jsonify(res_data))
Exemple #13
0
 def get(self, id):
     # 返回单条数据
     author = AuthorModel.query.get(id)
     author_data = AuthorSchema().dump(author)
     res_data = response_200({"author": author_data})
     return make_response(jsonify(res_data))
Exemple #14
0
 def get(self):
     # 返回所有数据
     author_list = AuthorModel.query.all()
     author_data = AuthorSchema().dump(author_list, many=True)
     res_data = response_200({"authors": author_data})
     return make_response(jsonify(res_data))
Exemple #15
0
 def get(self, id):
     # 返回单条数据
     user = UserModel.query.get(id)
     user_data = UserSchema().dump(user)
     res_data = response_200({"authors": user_data})
     return make_response(jsonify(res_data))
Exemple #16
0
 def get(self):
     # 返回所有数据
     user_list = UserModel.query.all()
     user_data = UserSchema().dump(user_list, many=True)
     res_data = response_200({"users": user_data})
     return make_response(jsonify(res_data))