user.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package sqlite
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/usememos/memos/store"
  6. )
  7. func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
  8. fields := []string{"`username`", "`role`", "`email`", "`nickname`", "`password_hash`"}
  9. placeholder := []string{"?", "?", "?", "?", "?"}
  10. args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash}
  11. if create.AvatarURL != "" {
  12. fields = append(fields, "`avatar_url`")
  13. placeholder = append(placeholder, "?")
  14. args = append(args, create.AvatarURL)
  15. }
  16. if create.RowStatus != "" {
  17. fields = append(fields, "`row_status`")
  18. placeholder = append(placeholder, "?")
  19. args = append(args, create.RowStatus)
  20. }
  21. if create.CreatedTs != 0 {
  22. fields = append(fields, "`created_ts`")
  23. placeholder = append(placeholder, "?")
  24. args = append(args, create.CreatedTs)
  25. }
  26. if create.UpdatedTs != 0 {
  27. fields = append(fields, "`updated_ts`")
  28. placeholder = append(placeholder, "?")
  29. args = append(args, create.UpdatedTs)
  30. }
  31. if create.ID != 0 {
  32. fields = append(fields, "`id`")
  33. placeholder = append(placeholder, "?")
  34. args = append(args, create.ID)
  35. }
  36. stmt := "INSERT INTO user (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING id, avatar_url, created_ts, updated_ts, row_status"
  37. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  38. &create.ID,
  39. &create.AvatarURL,
  40. &create.CreatedTs,
  41. &create.UpdatedTs,
  42. &create.RowStatus,
  43. ); err != nil {
  44. return nil, err
  45. }
  46. return create, nil
  47. }
  48. func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
  49. set, args := []string{}, []any{}
  50. if v := update.UpdatedTs; v != nil {
  51. set, args = append(set, "updated_ts = ?"), append(args, *v)
  52. }
  53. if v := update.RowStatus; v != nil {
  54. set, args = append(set, "row_status = ?"), append(args, *v)
  55. }
  56. if v := update.Username; v != nil {
  57. set, args = append(set, "username = ?"), append(args, *v)
  58. }
  59. if v := update.Email; v != nil {
  60. set, args = append(set, "email = ?"), append(args, *v)
  61. }
  62. if v := update.Nickname; v != nil {
  63. set, args = append(set, "nickname = ?"), append(args, *v)
  64. }
  65. if v := update.AvatarURL; v != nil {
  66. set, args = append(set, "avatar_url = ?"), append(args, *v)
  67. }
  68. if v := update.PasswordHash; v != nil {
  69. set, args = append(set, "password_hash = ?"), append(args, *v)
  70. }
  71. args = append(args, update.ID)
  72. query := `
  73. UPDATE user
  74. SET ` + strings.Join(set, ", ") + `
  75. WHERE id = ?
  76. RETURNING id, username, role, email, nickname, password_hash, avatar_url, created_ts, updated_ts, row_status
  77. `
  78. user := &store.User{}
  79. if err := d.db.QueryRowContext(ctx, query, args...).Scan(
  80. &user.ID,
  81. &user.Username,
  82. &user.Role,
  83. &user.Email,
  84. &user.Nickname,
  85. &user.PasswordHash,
  86. &user.AvatarURL,
  87. &user.CreatedTs,
  88. &user.UpdatedTs,
  89. &user.RowStatus,
  90. ); err != nil {
  91. return nil, err
  92. }
  93. return user, nil
  94. }
  95. func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
  96. where, args := []string{"1 = 1"}, []any{}
  97. if v := find.ID; v != nil {
  98. where, args = append(where, "id = ?"), append(args, *v)
  99. }
  100. if v := find.Username; v != nil {
  101. where, args = append(where, "username = ?"), append(args, *v)
  102. }
  103. if v := find.Role; v != nil {
  104. where, args = append(where, "role = ?"), append(args, *v)
  105. }
  106. if v := find.Email; v != nil {
  107. where, args = append(where, "email = ?"), append(args, *v)
  108. }
  109. if v := find.Nickname; v != nil {
  110. where, args = append(where, "nickname = ?"), append(args, *v)
  111. }
  112. query := `
  113. SELECT
  114. id,
  115. username,
  116. role,
  117. email,
  118. nickname,
  119. password_hash,
  120. avatar_url,
  121. created_ts,
  122. updated_ts,
  123. row_status
  124. FROM user
  125. WHERE ` + strings.Join(where, " AND ") + `
  126. ORDER BY created_ts DESC, row_status DESC
  127. `
  128. rows, err := d.db.QueryContext(ctx, query, args...)
  129. if err != nil {
  130. return nil, err
  131. }
  132. defer rows.Close()
  133. list := make([]*store.User, 0)
  134. for rows.Next() {
  135. var user store.User
  136. if err := rows.Scan(
  137. &user.ID,
  138. &user.Username,
  139. &user.Role,
  140. &user.Email,
  141. &user.Nickname,
  142. &user.PasswordHash,
  143. &user.AvatarURL,
  144. &user.CreatedTs,
  145. &user.UpdatedTs,
  146. &user.RowStatus,
  147. ); err != nil {
  148. return nil, err
  149. }
  150. list = append(list, &user)
  151. }
  152. if err := rows.Err(); err != nil {
  153. return nil, err
  154. }
  155. return list, nil
  156. }
  157. func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
  158. result, err := d.db.ExecContext(ctx, `
  159. DELETE FROM user WHERE id = ?
  160. `, delete.ID)
  161. if err != nil {
  162. return err
  163. }
  164. if _, err := result.RowsAffected(); err != nil {
  165. return err
  166. }
  167. if err := d.Vacuum(ctx); err != nil {
  168. // Prevent linter warning.
  169. return err
  170. }
  171. return nil
  172. }