45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gen"
|
|
)
|
|
|
|
// NoteEdit denotes one edit done to one known note.
|
|
// The table is (within this application) append only
|
|
// (Except for the case where a note is hard deleted,
|
|
// where the edits for it also need to be deleted.
|
|
// Direct access to the db also circumvents this).
|
|
// Every edit will append one entry per edited field, denoting
|
|
// the field and how it was changed, in chronological order
|
|
type NoteEdit struct {
|
|
Note Note
|
|
NoteId string `gorm:"primaryKey;autoIncrement:false;<-:create"`
|
|
EditNr uint64 `gorm:"primaryKey;autoIncrement:false;<-:create"`
|
|
CreatedAt time.Time `gorm:"<-:create"`
|
|
Before string `gorm:"<-:create"`
|
|
After string `gorm:"<-:create"`
|
|
Field string `gorm:"<-:create"` // What was edited
|
|
}
|
|
|
|
type INoteEdit interface {
|
|
// Append a new edit to a note
|
|
//
|
|
// INSERT INTO @@table
|
|
// (note_id, edit_nr, created_at, before, after, field)
|
|
// VALUES (
|
|
// @note_id,
|
|
// (SELECT COUNT(*) FROM @@table WHERE note_id = @noteId)+1,
|
|
// NOW(),
|
|
// @before,
|
|
// @after,
|
|
// @field
|
|
// )
|
|
AppendEdit(noteId, before, after, field string) error
|
|
|
|
// Find all edits for a field
|
|
//
|
|
// SELECT * FROM @@table WHERE note_id = @noteId AND field = @field
|
|
FindEditsForField(noteId, field string) ([]gen.T, error)
|
|
}
|