user.go 3.8 KB

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