pcmt/modules/user/user.go

40 lines
933 B
Go
Raw Normal View History

package user
import (
"context"
"fmt"
"log"
"git.dotya.ml/mirre-mt/pcmt/ent"
"git.dotya.ml/mirre-mt/pcmt/ent/user"
)
// CreateUser adds a user entry to the database.
func CreateUser(ctx context.Context, client *ent.Client, username, password string) (*ent.User, error) {
u, err := client.User.
Create().
SetUsername(username).
SetPassword(password).
Save(ctx)
// TODO: saving cleartext password, rework!
if err != nil {
return nil, fmt.Errorf("failed creating user: %w", err)
}
log.Println("user was created: ", u)
return u, nil
}
func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.User, error) {
u, err := client.User.
Query().
Where(user.Username(username)).
// `Only` fails if no user found,
// or more than 1 user returned.
Only(ctx)
if err != nil {
return nil, fmt.Errorf("failed querying user: %w", err)
}
log.Println("user returned: ", u)
return u, nil
}