all repos

onasty @ 11e5f7b

a one-time notes service

onasty/cmd/seed/notes.go (view raw)

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