workspace_setting.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package postgres
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/usememos/memos/store"
  6. )
  7. func (d *DB) UpsertWorkspaceSetting(ctx context.Context, upsert *store.WorkspaceSetting) (*store.WorkspaceSetting, error) {
  8. stmt := `
  9. INSERT INTO system_setting (
  10. name, value, description
  11. )
  12. VALUES ($1, $2, $3)
  13. ON CONFLICT(name) DO UPDATE
  14. SET
  15. value = EXCLUDED.value,
  16. description = EXCLUDED.description
  17. `
  18. if _, err := d.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil {
  19. return nil, err
  20. }
  21. return upsert, nil
  22. }
  23. func (d *DB) ListWorkspaceSettings(ctx context.Context, find *store.FindWorkspaceSetting) ([]*store.WorkspaceSetting, error) {
  24. where, args := []string{"1 = 1"}, []any{}
  25. if find.Name != "" {
  26. where, args = append(where, "name = "+placeholder(len(args)+1)), append(args, find.Name)
  27. }
  28. query := `
  29. SELECT
  30. name,
  31. value,
  32. description
  33. FROM system_setting
  34. WHERE ` + strings.Join(where, " AND ")
  35. rows, err := d.db.QueryContext(ctx, query, args...)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer rows.Close()
  40. list := []*store.WorkspaceSetting{}
  41. for rows.Next() {
  42. systemSettingMessage := &store.WorkspaceSetting{}
  43. if err := rows.Scan(
  44. &systemSettingMessage.Name,
  45. &systemSettingMessage.Value,
  46. &systemSettingMessage.Description,
  47. ); err != nil {
  48. return nil, err
  49. }
  50. list = append(list, systemSettingMessage)
  51. }
  52. if err := rows.Err(); err != nil {
  53. return nil, err
  54. }
  55. return list, nil
  56. }