mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
2 years ago
|
package postgres
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
2 years ago
|
"strings"
|
||
2 years ago
|
|
||
|
"github.com/usememos/memos/store"
|
||
|
)
|
||
|
|
||
1 year ago
|
func (d *DB) UpsertWorkspaceSetting(ctx context.Context, upsert *store.WorkspaceSetting) (*store.WorkspaceSetting, error) {
|
||
2 years ago
|
stmt := `
|
||
|
INSERT INTO system_setting (
|
||
|
name, value, description
|
||
|
)
|
||
|
VALUES ($1, $2, $3)
|
||
|
ON CONFLICT(name) DO UPDATE
|
||
|
SET
|
||
|
value = EXCLUDED.value,
|
||
|
description = EXCLUDED.description
|
||
|
`
|
||
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil {
|
||
2 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return upsert, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func (d *DB) ListWorkspaceSettings(ctx context.Context, find *store.FindWorkspaceSetting) ([]*store.WorkspaceSetting, error) {
|
||
2 years ago
|
where, args := []string{"1 = 1"}, []any{}
|
||
2 years ago
|
if find.Name != "" {
|
||
2 years ago
|
where, args = append(where, "name = "+placeholder(len(args)+1)), append(args, find.Name)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
query := `
|
||
|
SELECT
|
||
|
name,
|
||
|
value,
|
||
|
description
|
||
|
FROM system_setting
|
||
|
WHERE ` + strings.Join(where, " AND ")
|
||
2 years ago
|
|
||
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
1 year ago
|
list := []*store.WorkspaceSetting{}
|
||
2 years ago
|
for rows.Next() {
|
||
1 year ago
|
systemSettingMessage := &store.WorkspaceSetting{}
|
||
2 years ago
|
if err := rows.Scan(
|
||
|
&systemSettingMessage.Name,
|
||
|
&systemSettingMessage.Value,
|
||
|
&systemSettingMessage.Description,
|
||
|
); err != nil {
|
||
2 years ago
|
return nil, err
|
||
|
}
|
||
2 years ago
|
list = append(list, systemSettingMessage)
|
||
2 years ago
|
}
|
||
|
|
||
|
if err := rows.Err(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return list, nil
|
||
|
}
|