user.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. type User struct {
  27. ID int32
  28. // Standard fields
  29. RowStatus RowStatus
  30. CreatedTs int64
  31. UpdatedTs int64
  32. // Domain specific fields
  33. Username string
  34. Role Role
  35. Email string
  36. Nickname string
  37. PasswordHash string
  38. AvatarURL string
  39. }
  40. type UpdateUser struct {
  41. ID int32
  42. UpdatedTs *int64
  43. RowStatus *RowStatus
  44. Username *string
  45. Role *Role
  46. Email *string
  47. Nickname *string
  48. Password *string
  49. AvatarURL *string
  50. PasswordHash *string
  51. }
  52. type FindUser struct {
  53. ID *int32
  54. RowStatus *RowStatus
  55. Username *string
  56. Role *Role
  57. Email *string
  58. Nickname *string
  59. }
  60. type DeleteUser struct {
  61. ID int32
  62. }
  63. func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
  64. user, err := s.driver.CreateUser(ctx, create)
  65. if err != nil {
  66. return nil, err
  67. }
  68. s.userCache.Store(user.ID, user)
  69. return user, nil
  70. }
  71. func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
  72. user, err := s.driver.UpdateUser(ctx, update)
  73. if err != nil {
  74. return nil, err
  75. }
  76. s.userCache.Store(user.ID, user)
  77. return user, nil
  78. }
  79. func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
  80. list, err := s.driver.ListUsers(ctx, find)
  81. if err != nil {
  82. return nil, err
  83. }
  84. for _, user := range list {
  85. s.userCache.Store(user.ID, user)
  86. }
  87. return list, nil
  88. }
  89. func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
  90. if find.ID != nil {
  91. if cache, ok := s.userCache.Load(*find.ID); ok {
  92. return cache.(*User), nil
  93. }
  94. }
  95. list, err := s.ListUsers(ctx, find)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if len(list) == 0 {
  100. return nil, nil
  101. }
  102. user := list[0]
  103. s.userCache.Store(user.ID, user)
  104. return user, nil
  105. }
  106. func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
  107. err := s.driver.DeleteUser(ctx, delete)
  108. if err != nil {
  109. return err
  110. }
  111. s.userCache.Delete(delete.ID)
  112. return nil
  113. }