123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Copyright 2019-present Facebook Inc. All rights reserved.
- // This source code is licensed under the Apache 2.0 license found
- // in the LICENSE file in the root directory of this source tree.
- package main
- import (
- "context"
- "fmt"
- "log"
- "time"
- "code.osinet.fr/fgm/go__ent_demo/ent"
- "code.osinet.fr/fgm/go__ent_demo/ent/group"
- "code.osinet.fr/fgm/go__ent_demo/ent/user"
- )
- func CreateGraph(ctx context.Context, client *ent.Client) error {
- // First create the users
- a8m, err := client.User.
- Create().
- SetAge(30).
- SetName("Ariel").
- Save(ctx)
- if err != nil {
- return err
- }
- neta, err := client.User.
- Create().
- SetAge(28).
- SetName("Neta").
- SetSpouse(a8m).
- AddFollowing(a8m).
- AddFriends(a8m).
- Save(ctx)
- if err != nil {
- return err
- }
- // The, create the cars, and attach them to the users in the creation.
- if err = client.Car.Create().SetModel("Tesla").
- SetRegisteredAt(time.Now()). // Ignore the time in the graph
- SetOwner(a8m). // Attach this graph to Ariel
- Exec(ctx); err != nil {
- return err
- }
- if err = client.Car.Create().SetModel("Mazda").
- SetRegisteredAt(time.Now()). // Ignore the time in the graph
- SetOwner(a8m).
- Exec(ctx); err != nil {
- return err
- }
- if err = client.Car.Create().SetModel("Ford").
- SetRegisteredAt(time.Now()). // Ignore the time in the graph
- SetOwner(neta).
- Exec(ctx); err != nil {
- return err
- }
- // Create the groups, and add their users in the creation
- if err = client.Group.Create().SetName("GitLab").AddUsers(neta, a8m).Exec(ctx); err != nil {
- return err
- }
- if err = client.Group.Create().SetName("GitHub").AddUsers(a8m).Exec(ctx); err != nil {
- return err
- }
- log.Println("The graph was created successfully")
- return nil
- }
- // QueryGithub gets all user's cars within the group named "GitHub":
- func QueryGithub(ctx context.Context, client *ent.Client) error {
- cars, err := client.Group.
- Query().
- Where(group.Name("GitHub")). // (Group(Name=GitHub),)
- QueryUsers(). // (User(Name=Ariel, Age=30),)
- QueryCars(). // (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),
- All(ctx)
- if err != nil {
- return fmt.Errorf("failed getting cars for users in group GitHub: %w", err)
- }
- log.Println("cars in group GitHub: ", cars)
- return nil
- }
- func QueryArielCars(ctx context.Context, client *ent.Client) error {
- cars, err := client.User.
- Query().
- Where(
- user.HasCars(),
- user.Name("Ariel"),
- user.HasSpouse(),
- ).Only(ctx)
- if err != nil {
- return fmt.Errorf("failed getting cars for Ariel: %w", err)
- }
- log.Println("Ariel's cars: ", cars)
- a8m := client.User.Query().Where(user.HasSpouseWith(user.Name("Neta"))).OnlyX(ctx)
- log.Println("Query user who is the spouse of user named Neta: ", a8m)
- a8m = client.User.Query().Where(user.Name("Neta")).QuerySpouse().OnlyX(ctx)
- neta := client.User.Query().Where(user.Name("Neta")).OnlyX(ctx)
- log.Println("Query spouse of user named Neta: ", a8m)
- log.Printf("Followers for Ariel: %v, for Neta: %v",
- a8m.QueryFollowers().AllX(ctx),
- neta.QueryFollowers().AllX(ctx),
- )
- log.Printf("Following for Ariel: %v, for Neta: %v",
- a8m.QueryFollowing().AllX(ctx),
- neta.QueryFollowing().AllX(ctx),
- )
- log.Printf("Neta's friends: %v", neta.QueryFriends().AllX(ctx))
- return nil
- }
- func QueryGroupWithUsers(ctx context.Context, client *ent.Client) error {
- groups, err := client.Group.
- Query().
- Where(group.HasUsers()).
- All(ctx)
- if err != nil {
- return fmt.Errorf("failed getting groups with users: %w", err)
- }
- log.Println("Groups with users: ", groups)
- return nil
- }
|