|
38 | 38 | id |
39 | 39 | pages |
40 | 40 | } |
41 | | - user_book_reads(order_by: {started_at: desc}, limit: 1) { |
| 41 | + user_book_reads(order_by: {started_at: desc}, where: {finished_at: {_is_null: true}}) { |
42 | 42 | id |
43 | 43 | started_at |
44 | 44 | finished_at |
@@ -108,37 +108,40 @@ def get_user_book(self, ids): |
108 | 108 | def update_reading_progress(self, identifiers, progress_percent): |
109 | 109 | ids = self.parse_identifiers(identifiers) |
110 | 110 | book = self.get_user_book(ids) |
111 | | - if not book: |
| 111 | + if not book: # Book doesn't exist, add it in Reading status |
112 | 112 | book = self.add_book(ids, status=2) |
113 | | - if book.get("status_id") is not 2: |
| 113 | + if book.get("status_id") != 2: # Book is either WTR or Read |
114 | 114 | book = self.change_book_status(book, 2) |
115 | 115 | pages = book.get("edition",{}).get("pages",0) |
116 | 116 | if pages: |
117 | 117 | pages_read = round(pages * (progress_percent / 100)) |
118 | 118 | read = next(iter(book.get("user_book_reads")),None) |
119 | 119 | if not read: |
120 | | - read = self.add_read(book, pages_read) |
121 | | - mutation = """ |
122 | | - mutation ($readId: Int!, $pages: Int, $editionId: Int, $startedAt: date, $finishedAt: date) { |
123 | | - update_user_book_read(id: $readId, object: { |
124 | | - progress_pages: $pages, |
125 | | - edition_id: $editionId, |
126 | | - started_at: $startedAt, |
127 | | - finished_at: $finishedAt |
128 | | - }) { |
129 | | - id |
| 120 | + # read = self.add_read(book, pages_read) |
| 121 | + # No read exists for some reason, return since we can't update anything. |
| 122 | + return |
| 123 | + else: |
| 124 | + mutation = """ |
| 125 | + mutation ($readId: Int!, $pages: Int, $editionId: Int, $startedAt: date, $finishedAt: date) { |
| 126 | + update_user_book_read(id: $readId, object: { |
| 127 | + progress_pages: $pages, |
| 128 | + edition_id: $editionId, |
| 129 | + started_at: $startedAt, |
| 130 | + finished_at: $finishedAt |
| 131 | + }) { |
| 132 | + id |
| 133 | + } |
| 134 | + }""" |
| 135 | + variables = { |
| 136 | + "readId": int(read.get("id")), |
| 137 | + "pages": pages_read, |
| 138 | + "editionId": int(book.get("edition").get("id")), |
| 139 | + "startedAt":read.get("started_at",datetime.now().strftime("%Y-%m-%d")), |
| 140 | + "finishedAt": datetime.now().strftime("%Y-%m-%d") if progress_percent is 100 else None |
130 | 141 | } |
131 | | - }""" |
132 | | - variables = { |
133 | | - "readId": int(read.get("id")), |
134 | | - "pages": pages_read, |
135 | | - "editionId": int(book.get("edition").get("id")), |
136 | | - "startedAt":read.get("started_at",datetime.now().strftime("%Y-%m-%d")), |
137 | | - "finishedAt": datetime.now().strftime("%Y-%m-%d") if progress_percent is 100 else None |
138 | | - } |
139 | | - if progress_percent is 100: |
140 | | - self.change_book_status(book, 3) |
141 | | - return self.execute(query=mutation, variables=variables) |
| 142 | + if progress_percent is 100: |
| 143 | + self.change_book_status(book, 3) |
| 144 | + self.execute(query=mutation, variables=variables) |
142 | 145 | return |
143 | 146 |
|
144 | 147 | def change_book_status(self, book, status): |
|
0 commit comments