GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. type userRepository struct {
  9. db *sql.DB
  10. }
  11. func (r *userRepository) Find(ctx context.Context, id string) (*models.User, error) {
  12. user, err := psqlcore.New(r.db).SelectUser(ctx, id)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &models.User{
  17. ID: user.ID,
  18. Nick: user.Nick,
  19. Permissions: user.Permissions,
  20. }, nil
  21. }
  22. func (r *userRepository) List(ctx context.Context) ([]*models.User, error) {
  23. users, err := psqlcore.New(r.db).SelectUsers(ctx)
  24. if err != nil {
  25. return nil, err
  26. }
  27. results := make([]*models.User, 0, len(users))
  28. for _, user := range users {
  29. results = append(results, &models.User{
  30. ID: user.ID,
  31. Nick: user.Nick,
  32. Permissions: user.Permissions,
  33. })
  34. }
  35. return results, nil
  36. }
  37. func (r *userRepository) Insert(ctx context.Context, user models.User) (*models.User, error) {
  38. err := psqlcore.New(r.db).InsertUser(ctx, psqlcore.InsertUserParams{
  39. ID: user.ID,
  40. Nick: user.Nick,
  41. Permissions: user.Permissions,
  42. })
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &user, nil
  47. }