123456789101112131415161718192021222324252627282930313233343536 |
- package main
- import (
- "context"
- "fmt"
- "log"
- "code.osinet.fr/fgm/go__ent_demo/ent"
- "code.osinet.fr/fgm/go__ent_demo/ent/user"
- )
- func CreateUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
- u, err := client.User.
- Create().
- SetAge(30).
- SetName(name).
- Save(ctx)
- if err != nil {
- return nil, fmt.Errorf("failed creating user: %w", err)
- }
- log.Println("User created ", u)
- return u, nil
- }
- func QueryUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
- u, err := client.User.
- Query().
- Where(user.Name(name)).
- // `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 found: ", u)
- return u, nil
- }
|