Changeset 0.17.1 (#76)
This commit is contained in:
199
server/store/sqlite/note.go
Normal file
199
server/store/sqlite/note.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
|
||||
type NoteStore struct{}
|
||||
|
||||
func NewNoteStore() *NoteStore { return &NoteStore{} }
|
||||
|
||||
func (s *NoteStore) Create(ctx context.Context, n *models.Note) error {
|
||||
n.ID = store.NewID()
|
||||
now := time.Now().UTC()
|
||||
n.CreatedAt = now
|
||||
n.UpdatedAt = now
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO notes (id, user_id, title, content, folder_path, tags, metadata, source_channel_id, team_id, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)`,
|
||||
n.ID, n.UserID, n.Title, n.Content, n.FolderPath,
|
||||
ArrayToJSON(n.Tags), ToJSON(n.Metadata),
|
||||
models.NullString(n.SourceChannelID), models.NullString(n.TeamID),
|
||||
now.Format(timeFmt), now.Format(timeFmt),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *NoteStore) GetByID(ctx context.Context, id string) (*models.Note, error) {
|
||||
var n models.Note
|
||||
var sourceChannelID, teamID sql.NullString
|
||||
var tagsJSON, metadataJSON string
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, user_id, title, content, folder_path, tags, metadata,
|
||||
source_channel_id, team_id, created_at, updated_at
|
||||
FROM notes WHERE id = ?`, id).Scan(
|
||||
&n.ID, &n.UserID, &n.Title, &n.Content, &n.FolderPath,
|
||||
&tagsJSON, &metadataJSON,
|
||||
&sourceChannelID, &teamID, st(&n.CreatedAt), st(&n.UpdatedAt),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n.Tags = ScanArray(tagsJSON)
|
||||
n.SourceChannelID = NullableStringPtr(sourceChannelID)
|
||||
n.TeamID = NullableStringPtr(teamID)
|
||||
json.Unmarshal([]byte(metadataJSON), &n.Metadata)
|
||||
return &n, nil
|
||||
}
|
||||
|
||||
func (s *NoteStore) Update(ctx context.Context, id string, fields map[string]interface{}) error {
|
||||
b := NewUpdate("notes")
|
||||
for k, v := range fields {
|
||||
if k == "metadata" {
|
||||
b.SetJSON(k, v)
|
||||
} else if k == "tags" {
|
||||
if tags, ok := v.([]string); ok {
|
||||
b.Set(k, ArrayToJSON(tags))
|
||||
}
|
||||
} else {
|
||||
b.Set(k, v)
|
||||
}
|
||||
}
|
||||
if !b.HasSets() {
|
||||
return nil
|
||||
}
|
||||
b.Where("id", id)
|
||||
_, err := b.Exec(DB)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *NoteStore) Delete(ctx context.Context, id string) error {
|
||||
_, err := DB.ExecContext(ctx, "DELETE FROM notes WHERE id = ?", id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *NoteStore) ListForUser(ctx context.Context, userID string, opts store.NoteListOptions) ([]models.Note, int, error) {
|
||||
b := NewSelect(
|
||||
"id, user_id, title, content, folder_path, tags, metadata, source_channel_id, team_id, created_at, updated_at",
|
||||
"notes",
|
||||
).Where("user_id = ?", userID)
|
||||
|
||||
if opts.FolderPath != "" {
|
||||
b.Where("folder_path = ?", opts.FolderPath)
|
||||
}
|
||||
if opts.Tag != "" {
|
||||
// SQLite: check JSON array membership with json_each.
|
||||
b.Where("EXISTS (SELECT 1 FROM json_each(tags) WHERE value = ?)", opts.Tag)
|
||||
}
|
||||
if opts.TeamID != "" {
|
||||
b.Where("team_id = ?", opts.TeamID)
|
||||
}
|
||||
|
||||
// Count
|
||||
countQ, countArgs := b.CountBuild()
|
||||
var total int
|
||||
DB.QueryRowContext(ctx, countQ, countArgs...).Scan(&total)
|
||||
|
||||
if opts.Sort == "" {
|
||||
b.OrderBy("updated_at", "DESC")
|
||||
}
|
||||
b.Paginate(opts.ListOptions)
|
||||
|
||||
q, args := b.Build()
|
||||
rows, err := DB.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return s.scanNotes(rows, total)
|
||||
}
|
||||
|
||||
// Search uses LIKE against title and content (SQLite fallback for tsvector).
|
||||
func (s *NoteStore) Search(ctx context.Context, userID, query string, opts store.ListOptions) ([]models.Note, int, error) {
|
||||
// Split query into words, build LIKE clauses for each.
|
||||
words := strings.Fields(query)
|
||||
if len(words) == 0 {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
b := NewSelect(
|
||||
"id, user_id, title, content, folder_path, tags, metadata, source_channel_id, team_id, created_at, updated_at",
|
||||
"notes",
|
||||
).Where("user_id = ?", userID)
|
||||
|
||||
for _, w := range words {
|
||||
pattern := "%" + w + "%"
|
||||
b.Where("(title LIKE ? OR content LIKE ?)", pattern, pattern)
|
||||
}
|
||||
|
||||
// Count
|
||||
countQ, countArgs := b.CountBuild()
|
||||
var total int
|
||||
DB.QueryRowContext(ctx, countQ, countArgs...).Scan(&total)
|
||||
|
||||
b.OrderBy("updated_at", "DESC")
|
||||
b.Paginate(opts)
|
||||
|
||||
q, args := b.Build()
|
||||
rows, err := DB.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return s.scanNotes(rows, total)
|
||||
}
|
||||
|
||||
func (s *NoteStore) BulkDelete(ctx context.Context, ids []string, userID string) (int, error) {
|
||||
if len(ids) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
placeholders := make([]string, len(ids))
|
||||
args := make([]interface{}, 0, len(ids)+1)
|
||||
args = append(args, userID)
|
||||
for i, id := range ids {
|
||||
placeholders[i] = "?"
|
||||
args = append(args, id)
|
||||
}
|
||||
result, err := DB.ExecContext(ctx,
|
||||
fmt.Sprintf("DELETE FROM notes WHERE user_id = ? AND id IN (%s)",
|
||||
strings.Join(placeholders, ",")),
|
||||
args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
// ── helpers ─────────────────────────────────
|
||||
|
||||
func (s *NoteStore) scanNotes(rows *sql.Rows, total int) ([]models.Note, int, error) {
|
||||
var result []models.Note
|
||||
for rows.Next() {
|
||||
var n models.Note
|
||||
var sourceChannelID, teamID sql.NullString
|
||||
var tagsJSON, metadataJSON string
|
||||
err := rows.Scan(&n.ID, &n.UserID, &n.Title, &n.Content, &n.FolderPath,
|
||||
&tagsJSON, &metadataJSON,
|
||||
&sourceChannelID, &teamID, st(&n.CreatedAt), st(&n.UpdatedAt))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
n.Tags = ScanArray(tagsJSON)
|
||||
n.SourceChannelID = NullableStringPtr(sourceChannelID)
|
||||
n.TeamID = NullableStringPtr(teamID)
|
||||
json.Unmarshal([]byte(metadataJSON), &n.Metadata)
|
||||
result = append(result, n)
|
||||
}
|
||||
return result, total, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user