onasty/cmd/seed/notes.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
package main
import (
"context"
"log/slog"
"time"
"github.com/olexsmir/onasty/internal/hasher"
"github.com/olexsmir/onasty/internal/store/psqlutil"
)
var notesData = []struct {
id string
content string
slug string
burnBeforeExpiration bool
password string
expiresAt time.Time
hasAuthor bool
authorID int
}{
{ //nolint:exhaustruct
content: "that test note one",
slug: "one",
burnBeforeExpiration: false,
},
{ //nolint:exhaustruct
content: "that test note two",
slug: "two",
burnBeforeExpiration: true,
password: "",
expiresAt: time.Now().Add(24 * time.Hour),
},
{ //nolint:exhaustruct
content: "that passworded note",
slug: "passwd",
burnBeforeExpiration: false,
password: "pass",
},
{ //nolint:exhaustruct
content: "that note with author",
slug: "user",
burnBeforeExpiration: false,
hasAuthor: true,
authorID: 0,
},
{ //nolint:exhaustruct
content: "that another authored note",
slug: "user2",
burnBeforeExpiration: false,
hasAuthor: true,
authorID: 0,
},
{ //nolint:exhaustruct
content: "that another authored note",
slug: "user2",
password: "passwd",
burnBeforeExpiration: false,
hasAuthor: true,
authorID: 0,
},
}
func seedNotes(
ctx context.Context,
hash hasher.Hasher,
db *psqlutil.DB,
) error {
for i, note := range notesData {
passwd := ""
if note.password != "" {
var err error
passwd, err = hash.Hash(note.password)
if err != nil {
return err
}
}
err := db.QueryRow(
ctx, `
insert into notes (content, slug, burn_before_expiration, password, expires_at)
values ($1, $2, $3, $4, $5)
on conflict (slug) do update set
content = excluded.content,
burn_before_expiration = excluded.burn_before_expiration,
password = excluded.password,
expires_at = excluded.expires_at
returning id`,
note.content,
note.slug,
note.burnBeforeExpiration,
passwd,
note.expiresAt,
).Scan(¬esData[i].id)
if err != nil {
return err
}
if note.hasAuthor {
slog.Info("setting author", "note", note.id, "author", note.authorID)
if err := setAuthor(ctx, db, notesData[i].id, usersData[note.authorID].id); err != nil {
return err
}
}
}
return nil
}
func setAuthor(
ctx context.Context,
db *psqlutil.DB,
noteID string,
authorID string,
) error {
_, err := db.Exec(
ctx,
`insert into notes_authors (note_id, user_id) values ($1, $2)`,
noteID, authorID)
return err
}
|