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.

41 lines
836 B

  1. package mongodb
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/models"
  5. "git.aiterp.net/rpdata/api/repositories"
  6. "github.com/globalsign/mgo"
  7. )
  8. type userRepository struct {
  9. users *mgo.Collection
  10. }
  11. func newUserRepository(db *mgo.Database) (repositories.UserRepository, error) {
  12. collection := db.C("core.users")
  13. return &userRepository{users: collection}, nil
  14. }
  15. func (r *userRepository) Find(ctx context.Context, id string) (*models.User, error) {
  16. user := new(models.User)
  17. err := r.users.FindId(id).One(user)
  18. if err != nil {
  19. if err == mgo.ErrNotFound {
  20. return nil, repositories.ErrNotFound
  21. }
  22. return nil, err
  23. }
  24. return user, nil
  25. }
  26. func (r *userRepository) Insert(ctx context.Context, user models.User) (*models.User, error) {
  27. err := r.users.Insert(user)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &user, nil
  32. }