UserRepository.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "errors"
  4. )
  5. type UserRepository struct {
  6. //TODO: should contain db connection. Only a mockup for now
  7. }
  8. func NewUserRepository() *UserRepository {
  9. return &UserRepository{}
  10. }
  11. func (r *UserRepository) GetUserById(id string) (User, error) {
  12. for i := 0; i < len(usersList); i++ {
  13. if usersList[i].Id == id {
  14. return usersList[i], nil
  15. }
  16. }
  17. //TODO: create a more robust error type for handling user errors
  18. return User{}, errors.New("could not find user")
  19. }
  20. func (r *UserRepository) GetUserByUsername(username string) (User, error) {
  21. for i := 0; i < len(usersList); i++ {
  22. if usersList[i].Username == username {
  23. return usersList[i], nil
  24. }
  25. }
  26. //TODO: create a more robust error type for handling user errors
  27. return User{}, errors.New("could not find user")
  28. }
  29. func (r *UserRepository) AddUser(username string, password string) string {
  30. newUser := User{Id: "2", Username: username, PasswordHash: password}
  31. usersList = append(usersList, newUser)
  32. return newUser.Id
  33. }
  34. func (r *UserRepository) GetAllUsers() ([]User, error) {
  35. return usersList, nil
  36. }
  37. // mock user
  38. type User struct {
  39. Id string
  40. Username string
  41. PasswordHash string
  42. }
  43. var usersList = []User{
  44. {
  45. Id: "1",
  46. Username: "cmtedouglas",
  47. PasswordHash: "$2a$14$xEOuOEjuA48ko4IFBjcmoOrtY0dWXMQOPhp0sfcWkUXEw.ABsNkfu",
  48. },
  49. }