CreateNote creates a note with the next usn and updates the user's max_usn. It returns the created note.
(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, client string)
| 28 | // CreateNote creates a note with the next usn and updates the user's max_usn. |
| 29 | // It returns the created note. |
| 30 | func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, client string) (database.Note, error) { |
| 31 | tx := a.DB.Begin() |
| 32 | |
| 33 | nextUSN, err := incrementUserUSN(tx, user.ID) |
| 34 | if err != nil { |
| 35 | tx.Rollback() |
| 36 | return database.Note{}, pkgErrors.Wrap(err, "incrementing user max_usn") |
| 37 | } |
| 38 | |
| 39 | var noteAddedOn int64 |
| 40 | if addedOn == nil { |
| 41 | noteAddedOn = a.Clock.Now().UnixNano() |
| 42 | } else { |
| 43 | noteAddedOn = *addedOn |
| 44 | } |
| 45 | |
| 46 | var noteEditedOn int64 |
| 47 | if editedOn == nil { |
| 48 | noteEditedOn = 0 |
| 49 | } else { |
| 50 | noteEditedOn = *editedOn |
| 51 | } |
| 52 | |
| 53 | uuid, err := helpers.GenUUID() |
| 54 | if err != nil { |
| 55 | tx.Rollback() |
| 56 | return database.Note{}, err |
| 57 | } |
| 58 | |
| 59 | note := database.Note{ |
| 60 | UUID: uuid, |
| 61 | BookUUID: bookUUID, |
| 62 | UserID: user.ID, |
| 63 | AddedOn: noteAddedOn, |
| 64 | EditedOn: noteEditedOn, |
| 65 | USN: nextUSN, |
| 66 | Body: content, |
| 67 | Client: client, |
| 68 | } |
| 69 | if err := tx.Create(¬e).Error; err != nil { |
| 70 | tx.Rollback() |
| 71 | return note, pkgErrors.Wrap(err, "inserting note") |
| 72 | } |
| 73 | |
| 74 | tx.Commit() |
| 75 | |
| 76 | return note, nil |
| 77 | } |
| 78 | |
| 79 | // UpdateNoteParams is the parameters for updating a note |
| 80 | type UpdateNoteParams struct { |