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

Method CreateNote

pkg/server/app/notes.go:30–77  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

28// CreateNote creates a note with the next usn and updates the user's max_usn.
29// It returns the created note.
30func (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(&note).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
80type UpdateNoteParams struct {

Callers 3

createMethod · 0.80
TestCreateNoteFunction · 0.80
TestCreateNote_EmptyBodyFunction · 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 2

TestCreateNoteFunction · 0.64
TestCreateNote_EmptyBodyFunction · 0.64