def create_author(): try: data = request.get_json() author_schema = AuthorSchema() author = author_schema.load(data) result = author_schema.dump(author.create()) return response_with(resp.SUCCESS_201, value={"author": result}) except Exception as err: return response_with(resp.INVALID_INPUT_422)
def create_book(): try: data = request.get_json() schema = BookSchema() book = schema.load(data) result = schema.dump(book.create()) return response_with(resp.SUCCESS_201, value={"book": result}) except Exception as err: print(err) return response_with(resp.INVALID_INPUT_422)
def create_user(): try: data = request.get_json() data['password'] = User.generate_hash(data.get("password")) user_schema = UserSchema() user = user_schema.load(data) user = user_schema.dump(user.create()) return response_with(resp.SUCCESS_201) except Exception as err: print(err) return response_with(resp.INVALID_INPUT_422)
def delete_author(author_id): get_author = Author.query.get_or_404(author_id) db.session.delete(get_author) db.session.commit() return response_with(resp.SUCCESS_204)
def get_age(): employee_schema = EmployeeSchema() older = employee_schema.dump(get_query_older()) younger = employee_schema.dump(get_query_younger()) return resp.response_with(resp.SUCCESS_200, value={ 'younger': younger, 'older': older })
def get_salary_gap(): employee_schema = EmployeeSchema() lowest = employee_schema.dump(get_lowest_salary()) highest = employee_schema.dump(get_highest_salary()) return resp.response_with(resp.SUCCESS_200, value={ 'lowest': lowest, 'highest': highest })
def update_author_detail(author_id): data = request.get_json() author = Author.query.get_or_404(author_id) author.first_name = data.get("first_name") author.last_name = data.get("last_name") db.session.add(author) db.session.commit() author_schema = AuthorSchema() author = author_schema.dump(author) return response_with(resp.SUCCESS_200, value={"author": author})
def update_book_detail(id): data = request.get_json() book = Book.query.get_or_404(id) book.author_id = data.get("author_id") book.year = data.get("year") book.title = data.get("title") db.session.add(book) db.session.commit() schema = BookSchema() book = schema.dump(book) return response_with(resp.SUCCESS_200, value={"book": book})
def authenticate_user(): try: data = request.get_json() user = User.find_by_username(data.get("username")) if not user: return response_with(resp.SERVER_ERROR_404) if User.verify_hash(data.get("password"), user.password): access_token = create_access_token(identity=data.get("username")) return response_with(resp.SUCCESS_201, value={ "message": "Logged in as {}".format(user.username), "access_token": access_token }) else: return response_with(resp.UNAUTHORIZED_403) except Exception as err: print(err) return response_with(resp.INVALID_INPUT_422)
def update_author(author_id): # get author id from json data = request.get_json() get_author = Author.query.get_or_404(author_id) get_author.first_name = data['first_name'] get_author.last_name = data['last_name'] db.session.add(get_author) db.session.commit() # dump up the schema author_schema = AuthorSchema() author = author_schema.dump(get_author) return response_with(resp.SUCCESS_200, value={"author": author})
def modify_author(author_id): data = request.get_json() get_author = Author.query.get_or_404(author_id) if data.get('first_name'): get_author.first_name = data['first_name'] if data.get('last_name'): get_author.last_name = data['last_name'] db.session.add(get_author) db.session.commit() author_schema = AuthorSchema() author = author_schema.dump(get_author) return response_with(resp.SUCCESS_200, value={"author": author})
def bad_request(error): logging.error(error) return response_with(resp.BAD_REQUEST_400)
def not_found(error): logging.error(error) return response_with(resp.SERVER_ERROR_404)
def get_book_list(): fetched = Book.query.all() schema = BookSchema(many=True, only=["title", "year", "author_id"]) books = schema.dump(fetched) return response_with(resp.SUCCESS_200, value={"books": books})
def get_author_detail(author_id): fetched = Author.query.get_or_404(author_id) author_schema = AuthorSchema() author = author_schema.dump(fetched) return response_with(resp.SUCCESS_200, value={"author": author})
def get_author_list(): fetched = Author.query.all() author_schema = AuthorSchema(many=True, only=["first_name", "last_name", "id"]) authors = author_schema.dump(fetched) return response_with(resp.SUCCESS_200, value={"authors": authors})
def delete_book(id): book = Book.query.get_or_404(id) db.session.delete(book) db.session.commit() return response_with(resp.SUCCESS_204)
def get_books_list(): fetched_books = Book.query.all() book_schema = BookSchema(many=True) books = book_schema.dump(fetched_books) return response_with(resp.SUCCESS_200, value={"books": books})
def get_authors(): fetched_authors = Author.query.all() author_schema = AuthorSchema(many=True) authors = author_schema.dump(fetched_authors) return response_with(resp.SUCCESS_200, value={"authors": authors})
def server_error(error): logging.error(error) return response_with(resp.SERVER_ERROR_500)
def get_book_detail(book_id): fetched_book = Book.query.get_or_404(book_id) book_schema = BookSchema() book = book_schema.dump(fetched_book) return response_with(resp.SUCCESS_200, value={"book": book})