Skip to content

Commit 2ab2eae

Browse files
author
Stanislawa Kutyepov
committed
[add]: ability to delete books
1 parent 7aa3a06 commit 2ab2eae

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

app/books/schema.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class UpdateBook(graphene.Mutation):
3737

3838
class Arguments:
3939
book_id = graphene.Int(required=True)
40-
title = graphene.String()
41-
author = graphene.String()
42-
description = graphene.String()
43-
url = graphene.String()
40+
title = graphene.String(required=False)
41+
author = graphene.String(required=False)
42+
description = graphene.String(required=False)
43+
url = graphene.String(required=False)
4444

4545
def mutate(self, info, book_id, title, author, description, url):
4646
user = info.context.user
@@ -58,6 +58,25 @@ def mutate(self, info, book_id, title, author, description, url):
5858

5959
return UpdateBook(book=book)
6060

61+
62+
class DeleteBook(graphene.Mutation):
63+
book_id = graphene.Int()
64+
65+
class Arguments:
66+
book_id = graphene.Int(required=True)
67+
68+
def mutate(self, info, book_id):
69+
user = info.context.user
70+
book = Book.objects.get(id=book_id)
71+
72+
if book.posted_by != user:
73+
raise Exception("Not permited to delete this book")
74+
75+
book.delete()
76+
77+
return DeleteBook(book_id=book_id)
78+
6179
class Mutation(graphene.ObjectType):
6280
create_book = CreateBook.Field()
63-
update_book = UpdateBook.Field()
81+
update_book = UpdateBook.Field()
82+
delete_book = DeleteBook.Field()

app/db.sqlite3

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)