CreateBook creates a book with the next usn and updates the user's max_usn
(user database.User, name string)
| 24 | |
| 25 | // CreateBook creates a book with the next usn and updates the user's max_usn |
| 26 | func (a *App) CreateBook(user database.User, name string) (database.Book, error) { |
| 27 | tx := a.DB.Begin() |
| 28 | |
| 29 | nextUSN, err := incrementUserUSN(tx, user.ID) |
| 30 | if err != nil { |
| 31 | tx.Rollback() |
| 32 | return database.Book{}, errors.Wrap(err, "incrementing user max_usn") |
| 33 | } |
| 34 | |
| 35 | uuid, err := helpers.GenUUID() |
| 36 | if err != nil { |
| 37 | tx.Rollback() |
| 38 | return database.Book{}, err |
| 39 | } |
| 40 | |
| 41 | book := database.Book{ |
| 42 | UUID: uuid, |
| 43 | UserID: user.ID, |
| 44 | Label: name, |
| 45 | AddedOn: a.Clock.Now().UnixNano(), |
| 46 | USN: nextUSN, |
| 47 | } |
| 48 | if err := tx.Create(&book).Error; err != nil { |
| 49 | tx.Rollback() |
| 50 | return book, errors.Wrap(err, "inserting book") |
| 51 | } |
| 52 | |
| 53 | tx.Commit() |
| 54 | |
| 55 | return book, nil |
| 56 | } |
| 57 | |
| 58 | // DeleteBook marks a book deleted with the next usn and updates the user's max_usn |
| 59 | func (a *App) DeleteBook(tx *gorm.DB, user database.User, book database.Book) (database.Book, error) { |