user.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package store
  2. import (
  3. "context"
  4. )
  5. // Role is the type of a role.
  6. type Role string
  7. const (
  8. // RoleHost is the HOST role.
  9. RoleHost Role = "HOST"
  10. // RoleAdmin is the ADMIN role.
  11. RoleAdmin Role = "ADMIN"
  12. // RoleUser is the USER role.
  13. RoleUser Role = "USER"
  14. )
  15. func (e Role) String() string {
  16. switch e {
  17. case RoleHost:
  18. return "HOST"
  19. case RoleAdmin:
  20. return "ADMIN"
  21. case RoleUser:
  22. return "USER"
  23. }
  24. return "USER"
  25. }
  26. const (
  27. SystemBotID int32 = 0
  28. )
  29. var (
  30. SystemBot = &User{
  31. ID: SystemBotID,
  32. Username: "system_bot",
  33. Role: RoleAdmin,
  34. Email: "",
  35. Nickname: "Bot",
  36. }
  37. )
  38. type User struct {
  39. ID int32
  40. // Standard fields
  41. RowStatus RowStatus
  42. CreatedTs int64
  43. UpdatedTs int64
  44. // Domain specific fields
  45. Username string
  46. Role Role
  47. Email string
  48. Nickname string
  49. PasswordHash string
  50. AvatarURL string
  51. }
  52. type UpdateUser struct {
  53. ID int32
  54. UpdatedTs *int64
  55. RowStatus *RowStatus
  56. Username *string
  57. Role *Role
  58. Email *string
  59. Nickname *string
  60. Password *string
  61. AvatarURL *string
  62. PasswordHash *string
  63. }
  64. type FindUser struct {
  65. ID *int32
  66. RowStatus *RowStatus
  67. Username *string
  68. Role *Role
  69. Email *string
  70. Nickname *string
  71. }
  72. type DeleteUser struct {
  73. ID int32
  74. }
  75. func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
  76. user, err := s.driver.CreateUser(ctx, create)
  77. if err != nil {
  78. return nil, err
  79. }
  80. s.userCache.Store(user.ID, user)
  81. return user, nil
  82. }
  83. func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
  84. user, err := s.driver.UpdateUser(ctx, update)
  85. if err != nil {
  86. return nil, err
  87. }
  88. s.userCache.Store(user.ID, user)
  89. return user, nil
  90. }
  91. func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
  92. list, err := s.driver.ListUsers(ctx, find)
  93. if err != nil {
  94. return nil, err
  95. }
  96. for _, user := range list {
  97. s.userCache.Store(user.ID, user)
  98. }
  99. return list, nil
  100. }
  101. func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
  102. if find.ID != nil {
  103. if *find.ID == SystemBotID {
  104. return SystemBot, nil
  105. }
  106. if cache, ok := s.userCache.Load(*find.ID); ok {
  107. return cache.(*User), nil
  108. }
  109. }
  110. list, err := s.ListUsers(ctx, find)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if len(list) == 0 {
  115. return nil, nil
  116. }
  117. user := list[0]
  118. s.userCache.Store(user.ID, user)
  119. return user, nil
  120. }
  121. func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
  122. err := s.driver.DeleteUser(ctx, delete)
  123. if err != nil {
  124. return err
  125. }
  126. s.userCache.Delete(delete.ID)
  127. return nil
  128. }