MCPcopy
hub / github.com/dnote/dnote / writeNote

Function writeNote

pkg/cli/cmd/add/add.go:144–198  ·  view source on GitHub ↗
(ctx context.DnoteCtx, bookLabel string, content string, ts int64)

Source from the content-addressed store, hash-verified

142}
143
144func writeNote(ctx context.DnoteCtx, bookLabel string, content string, ts int64) (int, error) {
145 tx, err := ctx.DB.Begin()
146 if err != nil {
147 return 0, errors.Wrap(err, "beginning a transaction")
148 }
149
150 var bookUUID string
151 err = tx.QueryRow("SELECT uuid FROM books WHERE label = ?", bookLabel).Scan(&bookUUID)
152 if err == sql.ErrNoRows {
153 bookUUID, err = utils.GenerateUUID()
154 if err != nil {
155 return 0, errors.Wrap(err, "generating uuid")
156 }
157
158 b := database.NewBook(bookUUID, bookLabel, 0, false, true)
159 err = b.Insert(tx)
160 if err != nil {
161 tx.Rollback()
162 return 0, errors.Wrap(err, "creating the book")
163 }
164 } else if err != nil {
165 return 0, errors.Wrap(err, "finding the book")
166 }
167
168 noteUUID, err := utils.GenerateUUID()
169 if err != nil {
170 return 0, errors.Wrap(err, "generating uuid")
171 }
172
173 n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, true)
174
175 err = n.Insert(tx)
176 if err != nil {
177 tx.Rollback()
178 return 0, errors.Wrap(err, "creating the note")
179 }
180
181 var noteRowID int
182 err = tx.QueryRow(`SELECT notes.rowid
183 FROM notes
184 WHERE notes.uuid = ?`, noteUUID).
185 Scan(&noteRowID)
186 if err != nil {
187 tx.Rollback()
188 return noteRowID, errors.Wrap(err, "getting the note rowid")
189 }
190
191 err = tx.Commit()
192 if err != nil {
193 tx.Rollback()
194 return noteRowID, errors.Wrap(err, "committing a transaction")
195 }
196
197 return noteRowID, nil
198}

Callers 1

newRunFunction · 0.85

Calls 9

InsertMethod · 0.95
InsertMethod · 0.95
GenerateUUIDFunction · 0.92
NewBookFunction · 0.92
NewNoteFunction · 0.92
BeginMethod · 0.65
QueryRowMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected