MCPcopy Index your code
hub / github.com/dnote/dnote / CreateBook

Method CreateBook

pkg/server/app/books.go:26–56  ·  view source on GitHub ↗

CreateBook creates a book with the next usn and updates the user's max_usn

(user database.User, name string)

Source from the content-addressed store, hash-verified

24
25// CreateBook creates a book with the next usn and updates the user's max_usn
26func (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
59func (a *App) DeleteBook(tx *gorm.DB, user database.User, book database.Book) (database.Book, error) {

Callers 2

createMethod · 0.80
TestCreateBookFunction · 0.80

Calls 7

GenUUIDFunction · 0.92
incrementUserUSNFunction · 0.85
CreateMethod · 0.80
BeginMethod · 0.65
RollbackMethod · 0.65
NowMethod · 0.65
CommitMethod · 0.65

Tested by 1

TestCreateBookFunction · 0.64