Exemplo n.º 1
0
class AuthorList(Resource):

    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()
        self.book_service = BookService()

    new_author_model = api_app.model("Author model",
                                     {
                                         "name": fields.String(required=True, description="Author name",
                                                               help="Name cannot be blank"),
                                         "surname": fields.String(required=True, description="Author surname",
                                                                  help="Surname cannot be null")
                                     })

    @api_app.expect(new_author_model)
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {"message": "Added new author", "save_author_id": saved_author_id ,
                      "DELETE":"http://*****:*****@api_app.doc(responses={200: "OK"})
    def get(self):
        paginated_autor_response = self.author_service.get_paginated_authors_response()
        return paginated_autor_response

    def parse_request_arg_or_zero(self, request, param, default_value):
        val = request.args.get(param, default_value)
        val = int(val) if val.isdigit() else 0
        return val
Exemplo n.º 2
0
class Author(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()

    new_author_model = api_app.model(
        "Author model", {
            "name":
            fields.String(required=True,
                          description="Author name",
                          help="Name cannot be blank"),
            "surname":
            fields.String(required=True,
                          description="Author surname",
                          help="Surname cannot be null")
        })

    @api_app.expect(new_author_model)
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {
                "message": "Added new author",
                "save_author_id": saved_author_id
            }

            return result

        except AuthorAlreadyExistsException as e:
            author_namespace.abort(
                409,
                e.__doc__,
                status="Could not save author. Already exists",
                statusCode=409)
class Author(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()

    new_author_model = api_app.model(
        "Author model", {
            "name":
            fields.String(required=True,
                          description="Author name",
                          help="Name cannot be blank"),
            "surname":
            fields.String(required=True,
                          description="Author surname",
                          help="Surname cannot be null")
        })

    @api_app.expect(new_author_model)
    @jwt_required
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {
                "message": "Added new author",
                "save_author_id": saved_author_id
            }

            return result

        except AuthorAlreadyExistsException as e:
            author_namespace.abort(
                409,
                e.__doc__,
                status="Could not save author. Already exists",
                statusCode=409)

    @api_app.doc(responses={
        200: "OK",
        400: "Invalid argument"
    },
                 params={
                     "name": "Specify author name",
                     "surname": "Specify author surname"
                 })
    @api_app.param("name", "Specify author name.")
    @api_app.param("surname", "Specify author surname.")
    @jwt_required
    def get(self):
        try:
            name = request.args.get("name")
            surname = request.args.get("surname")

            author = self.author_service.get_author_by_names(name, surname)
            return author.__dict__

        except Exception as e:
            author_namespace.abort(400,
                                   e.__doc__,
                                   status="Could not find author by names",
                                   statusCode="400")