Junior — Senior
Adding books and their covers via API
livecode
Task condition
When performing a POST request to create a new book, the function create_book is called, which creates a record in the Book table and then creates related records in the BookCover table.
A code review is required: check the correctness and reliability of the entity creation process, pay attention to possible issues with operation atomicity, exception handling, and potential optimizations.
def create_book(data: dict):
cover_list: list[dict] = data.pop('covers')
new_book = Book.objects.create(**data)
for cover in cover_list:
BookCover.objects.create(**cover, book_id=new_book.id)