workspace_setting.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package mysql
  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 := "INSERT INTO `system_setting` (`name`, `value`, `description`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?, `description` = ?"
  9. _, err := d.db.ExecContext(
  10. ctx,
  11. stmt,
  12. upsert.Name,
  13. upsert.Value,
  14. upsert.Description,
  15. upsert.Value,
  16. upsert.Description,
  17. )
  18. if 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` = ?"), append(args, find.Name)
  27. }
  28. query := "SELECT `name`, `value`, `description` FROM `system_setting` WHERE " + strings.Join(where, " AND ")
  29. rows, err := d.db.QueryContext(ctx, query, args...)
  30. if err != nil {
  31. return nil, err
  32. }
  33. defer rows.Close()
  34. list := []*store.WorkspaceSetting{}
  35. for rows.Next() {
  36. systemSettingMessage := &store.WorkspaceSetting{}
  37. if err := rows.Scan(
  38. &systemSettingMessage.Name,
  39. &systemSettingMessage.Value,
  40. &systemSettingMessage.Description,
  41. ); err != nil {
  42. return nil, err
  43. }
  44. list = append(list, systemSettingMessage)
  45. }
  46. if err := rows.Err(); err != nil {
  47. return nil, err
  48. }
  49. return list, nil
  50. }
  51. func (d *DB) DeleteWorkspaceSetting(ctx context.Context, delete *store.DeleteWorkspaceSetting) error {
  52. stmt := "DELETE FROM `system_setting` WHERE `name` = ?"
  53. _, err := d.db.ExecContext(ctx, stmt, delete.Name)
  54. return err
  55. }