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

package postgres
import (
"context"
"database/sql"
"git.aiterp.net/rpdata/api/database/postgres/psqlcore"
"git.aiterp.net/rpdata/api/models"
)
type userRepository struct {
db *sql.DB
}
func (r *userRepository) Find(ctx context.Context, id string) (*models.User, error) {
user, err := psqlcore.New(r.db).SelectUser(ctx, id)
if err != nil {
return nil, err
}
return &models.User{
ID: user.ID,
Nick: user.Nick,
Permissions: user.Permissions,
}, nil
}
func (r *userRepository) List(ctx context.Context) ([]*models.User, error) {
users, err := psqlcore.New(r.db).SelectUsers(ctx)
if err != nil {
return nil, err
}
results := make([]*models.User, 0, len(users))
for _, user := range users {
results = append(results, &models.User{
ID: user.ID,
Nick: user.Nick,
Permissions: user.Permissions,
})
}
return results, nil
}
func (r *userRepository) Insert(ctx context.Context, user models.User) (*models.User, error) {
err := psqlcore.New(r.db).InsertUser(ctx, psqlcore.InsertUserParams{
ID: user.ID,
Nick: user.Nick,
Permissions: user.Permissions,
})
if err != nil {
return nil, err
}
return &user, nil
}