UpdateBook updaates the book, the usn and the user's max_usn
(tx *gorm.DB, user database.User, book database.Book, label *string)
| 80 | |
| 81 | // UpdateBook updaates the book, the usn and the user's max_usn |
| 82 | func (a *App) UpdateBook(tx *gorm.DB, user database.User, book database.Book, label *string) (database.Book, error) { |
| 83 | if user.ID != book.UserID { |
| 84 | return book, errors.New("Not allowed") |
| 85 | } |
| 86 | |
| 87 | nextUSN, err := incrementUserUSN(tx, user.ID) |
| 88 | if err != nil { |
| 89 | return book, errors.Wrap(err, "incrementing user max_usn") |
| 90 | } |
| 91 | |
| 92 | if label != nil { |
| 93 | book.Label = *label |
| 94 | } |
| 95 | |
| 96 | book.USN = nextUSN |
| 97 | book.EditedOn = a.Clock.Now().UnixNano() |
| 98 | book.Deleted = false |
| 99 | |
| 100 | if err := tx.Save(&book).Error; err != nil { |
| 101 | return book, errors.Wrap(err, "updating the book") |
| 102 | } |
| 103 | |
| 104 | return book, nil |
| 105 | } |