Beispiel #1
0
 def get_publisher(self, id) -> dict:
     publisher = self.session.query(Publisher).filter(
         Publisher.id == id).first()
     if not isinstance(publisher, Publisher):
         return JSONResponse.error('Publisher id is invalid')
     response = [{'id': publisher.id, 'title': publisher.name}]
     return JSONResponse.success({'publishers': response})
Beispiel #2
0
    def delete_author(self, id: int) -> dict:
        author = self.session.query(Author).filter(Author.id == id).first()
        if not isinstance(author, Author):
            return JSONResponse.error('Author id is invalid')

        self.session.delete(author)

        return JSONResponse.success({'action': 'author deleted'})
Beispiel #3
0
    def delete_publisher(self, id: int) -> dict:
        publisher = self.session.query(Publisher).filter(
            Publisher.id == id).first()
        if not isinstance(publisher, Publisher):
            return JSONResponse.error('Publisher id is invalid')

        self.session.delete(publisher)

        return JSONResponse.success({'action': 'publisher deleted'})
Beispiel #4
0
    def edit_publisher(self, id: int, args: object) -> dict:
        publisher = self.session.query(Publisher).filter(
            Publisher.id == id).first()
        if not isinstance(publisher, Publisher):
            return JSONResponse.error('Publisher id is invalid')

        publisher.update(args['name'])

        return JSONResponse.success({'action': 'publisher edited'})
Beispiel #5
0
    def get_authors(self, id: int) -> dict:
        author = self.session.query(Author).filter(Author.id == id).first()
        if not isinstance(author, Author):
            return JSONResponse.error('Author id is invalid')

        response = [{
            'firstname': author.firstname,
            'lastname': author.lastname,
            'nickname': author.nickname,
            'birthdate': author.birthdate.strftime('%d/%m/%Y')
        }]
        return JSONResponse.success({'Author': response})
Beispiel #6
0
    def delete_book(self, id):
        book = self.session.query(Book).filter(Book.id == id).first()
        if not isinstance(book, Book):
            return JSONResponse.error('Publisher id is invalid')

        book_author = self.session.query(Book_author).filter(
            Book.book_id == id).first()

        self.session.delete(book_author)
        self.session.delete(book)

        return JSONResponse.success({'action': 'book deleted'})
Beispiel #7
0
    def create_authors(self, args: object) -> dict:
        if not self.check_data(args['birthdate']):
            return JSONResponse.error(
                "author_birthdate: shoud by in format YYYY/MM/DD (for example 2000-02-23"
            )

        authors = Author(args['firstname'], args['lastname'], args['nickname'],
                         self.to_date(args['birthdate']))

        self.session.add(authors)

        return JSONResponse.success({'action': 'author add'})
Beispiel #8
0
    def edit_book(self, id: str, args: object) -> dict:
        publisher = self.session.query(Publisher).filter(
            Publisher.id == id).first()
        if not isinstance(publisher, Publisher):
            return JSONResponse.error('Publisher id is invalid')

        book = self.session.query(Book).filter(Book.id == id).first()
        if not isinstance(book, Book):
            return JSONResponse.error('Book id is invalid')
        book.update(args['book_title'], publisher, args['book_page_num'],
                    args['book_cover_image'])

        return JSONResponse.success({'action': 'book edited'})
Beispiel #9
0
    def edit_author(self, id: int, args: object) -> dict:
        if not self.check_data(args['author_birthdate']):
            return JSONResponse.error(
                "author_birthdate: shoud by in format YYYY/MM/DD (for example 2000-02-23"
            )

        author = self.session.query(Author).filter(Author.id == id).first()
        if not isinstance(author, Author):
            return JSONResponse.error('Author id is invalid')

        author.update(args['firstname'], args['lastname'], args['nickname'],
                      self.to_date(args['birthdate']))

        return JSONResponse.success({'action': 'author edited'})
Beispiel #10
0
    def get_all_publishers(self) -> dict:
        response = [{
            'id': x.id,
            'title': x.name
        } for x in self.session.query(Publisher).all()]

        return JSONResponse.success({'publishers': response})
Beispiel #11
0
    def get_all_authors(self) -> dict:
        response = [{
            'firstname': x.firstname,
            'lastname': x.lastname,
            'nickname': x.nickname,
            'birthdate': x.birthdate.strftime('%d/%m/%Y')
        } for x in self.session.query(Author).all()]

        return JSONResponse.success({'authors': response})
Beispiel #12
0
    def get_book(self, id) -> dict:
        data = self.session.query(Book_author).join(Author).join(Book).join(Publisher, Book.publisher).\
            filter(Book.id == id).first()

        if not isinstance(data, Book_author):
            JSONResponse.error('id not foud')
        print(dir(data.book.publisher))
        response = {
            'id': data.book.id,
            'title': data.book.title,
            'cover_images': data.book.cover_image,
            'publisher_name': data.book.publisher.name,
            'page_number': data.book.pages_num,
            'author': {
                'firstname': data.author.firstname,
                'lastname': data.author.lastname
            }
        }

        return JSONResponse.success({'book': response})
Beispiel #13
0
    def get_all_books(self) -> dict:
        response = [{
            'id': x.id,
            'title': x.title,
            'cover_images': x.cover_image,
            'publisher_name': x.publisher.name
        }
                    for x in self.session.query(Book).join(
                        Publisher, Book.publisher).all()]

        return JSONResponse.success({'books': response})
Beispiel #14
0
    def insert_book(self, args):
        publisher = self.session.query(Publisher).filter(
            Publisher.id == args['publisher_id']).first()

        author = self.session.query(Author).filter(
            Author.id == args['author_id']).first()

        book = Book(args['book_title'], publisher, args['book_page_num'],
                    args['book_cover_image'])

        book_author = Book_author(book, author)

        self.session.add(book)
        self.session.add(book_author)

        return JSONResponse.success({'action': 'book added'})
Beispiel #15
0
    def create_publishers(self, args: object) -> dict:
        publisher = Publisher(args['name'])
        self.session.add(publisher)

        return JSONResponse.success({'action': 'publisher created'})