user.go 4.2 KB

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