dnote/cli/core/queries.go
Sung Won Cho f526124243
Simplify view and edit command (#184)
* Simplify the view command

* Simplify the edit command

* Migrate number-only book names

* Run migration

* Simplify remove

* Print note info when adding, editing, or removing

* Disallow users from editing or removing already removed notes
2019-05-18 16:52:12 +10:00

38 lines
978 B
Go

package core
import (
"database/sql"
"github.com/dnote/dnote/cli/infra"
"github.com/pkg/errors"
)
// NoteInfo is a basic information about a note
type NoteInfo struct {
RowID int
BookLabel string
UUID string
Content string
AddedOn int64
EditedOn int64
}
// GetNoteInfo returns a NoteInfo for the note with the given noteRowID
func GetNoteInfo(ctx infra.DnoteCtx, noteRowID string) (NoteInfo, error) {
var ret NoteInfo
db := ctx.DB
err := db.QueryRow(`SELECT books.label, notes.uuid, notes.body, notes.added_on, notes.edited_on, notes.rowid
FROM notes
INNER JOIN books ON books.uuid = notes.book_uuid
WHERE notes.rowid = ? AND notes.deleted = false`, noteRowID).
Scan(&ret.BookLabel, &ret.UUID, &ret.Content, &ret.AddedOn, &ret.EditedOn, &ret.RowID)
if err == sql.ErrNoRows {
return ret, errors.Errorf("note %s not found", noteRowID)
} else if err != nil {
return ret, errors.Wrap(err, "querying the note")
}
return ret, nil
}