system_setting.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package store
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/usememos/memos/api"
  8. "github.com/usememos/memos/common"
  9. )
  10. type systemSettingRaw struct {
  11. Name api.SystemSettingName
  12. Value string
  13. Description string
  14. }
  15. func (raw *systemSettingRaw) toSystemSetting() *api.SystemSetting {
  16. return &api.SystemSetting{
  17. Name: raw.Name,
  18. Value: raw.Value,
  19. Description: raw.Description,
  20. }
  21. }
  22. func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *api.SystemSettingUpsert) (*api.SystemSetting, error) {
  23. tx, err := s.db.BeginTx(ctx, nil)
  24. if err != nil {
  25. return nil, FormatError(err)
  26. }
  27. defer tx.Rollback()
  28. systemSettingRaw, err := upsertSystemSetting(ctx, tx, upsert)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if err := tx.Commit(); err != nil {
  33. return nil, err
  34. }
  35. systemSetting := systemSettingRaw.toSystemSetting()
  36. return systemSetting, nil
  37. }
  38. func (s *Store) FindSystemSettingList(ctx context.Context, find *api.SystemSettingFind) ([]*api.SystemSetting, error) {
  39. tx, err := s.db.BeginTx(ctx, nil)
  40. if err != nil {
  41. return nil, FormatError(err)
  42. }
  43. defer tx.Rollback()
  44. systemSettingRawList, err := findSystemSettingList(ctx, tx, find)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if err := tx.Commit(); err != nil {
  49. return nil, err
  50. }
  51. list := []*api.SystemSetting{}
  52. for _, raw := range systemSettingRawList {
  53. list = append(list, raw.toSystemSetting())
  54. }
  55. return list, nil
  56. }
  57. func (s *Store) FindSystemSetting(ctx context.Context, find *api.SystemSettingFind) (*api.SystemSetting, error) {
  58. tx, err := s.db.BeginTx(ctx, nil)
  59. if err != nil {
  60. return nil, FormatError(err)
  61. }
  62. defer tx.Rollback()
  63. systemSettingRawList, err := findSystemSettingList(ctx, tx, find)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if len(systemSettingRawList) == 0 {
  68. return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
  69. }
  70. return systemSettingRawList[0].toSystemSetting(), nil
  71. }
  72. func upsertSystemSetting(ctx context.Context, tx *sql.Tx, upsert *api.SystemSettingUpsert) (*systemSettingRaw, error) {
  73. query := `
  74. INSERT INTO system_setting (
  75. name, value, description
  76. )
  77. VALUES (?, ?, ?)
  78. ON CONFLICT(name) DO UPDATE
  79. SET
  80. value = EXCLUDED.value,
  81. description = EXCLUDED.description
  82. RETURNING name, value, description
  83. `
  84. var systemSettingRaw systemSettingRaw
  85. if err := tx.QueryRowContext(ctx, query, upsert.Name, upsert.Value, upsert.Description).Scan(
  86. &systemSettingRaw.Name,
  87. &systemSettingRaw.Value,
  88. &systemSettingRaw.Description,
  89. ); err != nil {
  90. return nil, FormatError(err)
  91. }
  92. return &systemSettingRaw, nil
  93. }
  94. func findSystemSettingList(ctx context.Context, tx *sql.Tx, find *api.SystemSettingFind) ([]*systemSettingRaw, error) {
  95. where, args := []string{"1 = 1"}, []interface{}{}
  96. if find.Name.String() != "" {
  97. where, args = append(where, "name = ?"), append(args, find.Name.String())
  98. }
  99. query := `
  100. SELECT
  101. name,
  102. value,
  103. description
  104. FROM system_setting
  105. WHERE ` + strings.Join(where, " AND ")
  106. rows, err := tx.QueryContext(ctx, query, args...)
  107. if err != nil {
  108. return nil, FormatError(err)
  109. }
  110. defer rows.Close()
  111. systemSettingRawList := make([]*systemSettingRaw, 0)
  112. for rows.Next() {
  113. var systemSettingRaw systemSettingRaw
  114. if err := rows.Scan(
  115. &systemSettingRaw.Name,
  116. &systemSettingRaw.Value,
  117. &systemSettingRaw.Description,
  118. ); err != nil {
  119. return nil, FormatError(err)
  120. }
  121. systemSettingRawList = append(systemSettingRawList, &systemSettingRaw)
  122. }
  123. if err := rows.Err(); err != nil {
  124. return nil, FormatError(err)
  125. }
  126. return systemSettingRawList, nil
  127. }