Skip to content

Blueprint41 CRUD

Link edited this page Apr 25, 2024 · 39 revisions

Blueprint41 CRUD

The following steps will guide you on how to create, read, update and delete an instance of Movie and Person entity.

Setup the database connection and the datastore model

Before your application can access the database using Blueprint41, you need to let it know where the database is, and what model of data that is stored in it.

  1. In the Main method, set up the database connection, specify the database server and its authentication credentials.

    Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]", "[OPTIONAL_DATABASE_NAME]");
    PersistenceProvider.CurrentPersistenceProvider = provider;
  2. In the Main method, create an instance of your datastore model DataStore and call the Execute method to perform a database upgrade if it already exists or creates the database if it's not.

    Datastore model = new Datastore();
    model.Execute(true);

You've successfully set up your database and the database model. Next, you will create data for the Movie and Person entity.

Creating Movie and Person record

  1. In the file Program.cs, create a static method CreateMoviesAndActors and in that method, create a transaction and instantiate the new objects for the Movie and Person entity.

    private static void CreateMoviesAndActors()
    {
        using (Transaction.Begin(true))
        {
            Movie matrix = new Movie()
            {
                title = "The Matrix",
                tagline = "Welcome to the Real World",
                released = 1999
            };
    
            Movie johnWick = new Movie()
            {
                title = "John Wick",
                tagline = "Revenge is all he has left",
                released = 2014
            };
    
            Movie toyStory = new Movie()
            {
                title = "Toy Story 4",
                tagline = "Get Ready To Hit The Road.",
                released = 2019
            };
    
            Movie cloudAtlas = new Movie()
            {
                title = "Cloud Atlas",
                tagline = "Everything is connected",
                released = 2012
            };
    
            Person keanu = new Person()
            {
                name = "Keanu Reeves",
                born = 1964
            };
    
            Person laurence = new Person()
            {
                name = "Laurence Fishburne",
                born = 1961
            };              
    
            Person tom = new Person()
            {
                name = "Tom Hanks",
                born = 1956
            };
        
            Transaction.Commit();
        }
    }

Note

Always commit the transaction to persist the objects created.

Important

When having database changes, you should use the Transaction.Begin(true) else Transaction.Begin() when just having a query.

   Add missing using directive.

   image

Creating Relationship for Movie, Person and Genre

  1. In the CreateMoviesAndActors method, copy the code below and paste it before the line Transaction.Commit().

    matrix.AddActor(keanu, roles: new string[] { "Thomas Anderson" });
    matrix.AddActor(laurence, roles: new string[] { "Morpheus" });
    matrix.AddGenre(Genre.Load(Genre.StaticData.Name.Action));
    matrix.AddGenre(Genre.Load(Genre.StaticData.Name.Sci_Fi));
    
    tom.AddMovieReview(matrix, rating: 5, summary: "Nice Movie! Excellent!");
    keanu.AddFollower(laurence);                               
    
    johnWick.AddActor(keanu, roles: new string[] { "John Wick" });
    johnWick.AddGenre(Genre.Load(Genre.StaticData.Name.Action));
    
    cloudAtlas.AddActor(tom, roles: new string[] { "Dr. Henry Goose", "Hotel Manager", "Isaac Sachs", "Dermot Hoggins", "Zachry" });
    cloudAtlas.AddGenre(Genre.Load(Genre.StaticData.Name.Sci_Fi));
    cloudAtlas.AddGenre(Genre.Load(Genre.StaticData.Name.Drama));
    
    toyStory.AddActor(tom, roles: new string[] { "Woody (voice)" });
    toyStory.AddActor(keanu, roles: new string[] { "Duke Caboom (voice)" });
    toyStory.AddGenre(Genre.Load(Genre.StaticData.Name.Animation));

    This code will add Person keanu and laurence as Actors on the "The Matrix" movie, and tom on "Cloud Atlas" movie. The Movie Matrix will also have Action and Sci-Fi genres; Sci-Fi for "Cloud Atlas" Movie.

  2. In the Main method, call the CreateMoviesAndActors method.

    static void Main(string[] args)
    {
        Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
        PersistenceProvider.CurrentPersistenceProvider = provider;
    
        Datastore model = new Datastore();
        model.Execute(true);
    
        CreateMoviesAndActors();
    }
  3. Run the "MovieGraph" application. Select Debug > Start Debugging or press F5.

You have successfully created records of the Person, Movie and Genre entity.

image

Note

After creating the records, in the Main method, comment the line where the method CreateMoviesAndActors is called so that it won't create duplicate records.

Get Movies and its Genres and Actors

  1. To display the Movie records with its properties (title, tagline,released), Genres and it's Actors. Create the static method GetMoviesAndTheActors. Then in that method, create a transaction and use the Movie.GetAll() method to get all Movie records.

    private static void GetMoviesAndTheActors()
    {
        using (Transaction.Begin())
        {
            foreach (Movie movie in Movie.GetAll())
            {
                Console.WriteLine($"Movie: { movie.title }, Tagline: { movie.tagline }, Released: { movie.released }");
    
                // Get Genres
                foreach (Genre genre in movie.Genres)
                {
                    Console.WriteLine($"    Genre: { genre.Name }");
                }
    
                // Get Actors
                foreach (Person actor in movie.Actors)
                {
                    Console.WriteLine($"    Actor: { actor.name }, Born: { actor.born }");
                }
            }
    
            //No commit needed, since we're only reading anyway...
        }
    }
  2. In the Main method, call the GetMoviesAndTheActors method after the CreateMoviesAndActors method and perform the Console.Read method so that you can see the message in the console.

    static void Main(string[] args)
    {
        Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
        PersistenceProvider.CurrentPersistenceProvider = provider;
    
        Datastore model = new Datastore();
        model.Execute(true);
    
        //CreateMoviesAndActors();
        
        GetMoviesAndTheActors();
    
        Console.ReadKey();
    }
  3. Press F5 to run the "MovieGraph" application.

image

After you've successfully displayed all the Movie records, next steps will guide you on how to delete a Movie record.

Deleting a Movie record

  1. To delete the Movie record, create the static method DeleteMovie(string). In the method, create a transaction and get the Movie record by calling the Movie.LoadBytitle(string) method and set movieTitle parameter value to "Cloud Atlas". After getting the record, make sure to remove the relationships for Actors and Genres first. Use the method movie.Delete() to delete the record.

    private static void DeleteMovie(string movieTitle)
    {
        using (Transaction.Begin(true))
        {
            Movie movie = Movie.LoadBytitle(movieTitle);
    
            if (movie is not null)
            {
                movie.Actors.Clear();
                movie.Genres.Clear();
                movie.Delete();
    
                Console.WriteLine($@"Movie ""{ movieTitle }"" successfully deleted.");
            }
    
            Transaction.Commit();
        }
    }
  2. In the Main method, call the DeleteMovie method after the CreateMoviesAndActors method and before the GetMoviesAndTheActors method.

Note

Comment the line where the method GetMoviesAndTheActors is called so that it won't display the other movie records.

    static void Main(string[] args)
    {
        Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
        PersistenceProvider.CurrentPersistenceProvider = provider;

        Datastore model = new Datastore();
        model.Execute(true);

        //CreateMoviesAndActors();    
    
        DeleteMovie("Cloud Atlas");
    
        //GetMoviesAndTheActors();

        Console.ReadKey();
    }
  1. Press F5 to run the "MovieGraph" application.

Run_DeleteMovie

You've successfully deleted the Movie "Cloud Atlas". Next is to update a Movie record.

Updating a Movie record

  1. To update a Movie record, create the static method UpdateMovie. In the method, create a transaction and get the Movie record by calling the Movie.LoadBytitle(string) method and set title parameter value to "The Matrix". Change the values of the following properties:

    Property Value
    title The Matrix Reload
    tagline Reload before the revolution begins.
    released 2003
    private static void UpdateMovie()
    {
        using (Transaction.Begin(true))
        {
            Movie movie = Movie.LoadBytitle("The Matrix");
    
            if (movie is not null)
            {
                movie.title = "The Matrix Reload";
                movie.tagline = "Reload before the revolution begins.";
                movie.released = 2003;
            }
    
            Transaction.Commit();
        }
    }
  2. In the Main method, call the UpdateMovie method after the DeleteMovie method and before the GetMoviesAndTheActors method.

    static void Main(string[] args)
    {
        Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
        PersistenceProvider.CurrentPersistenceProvider = provider;
    
        Datastore model = new Datastore();
        model.Execute(true);
    
        //CreateMoviesAndActors();
    
        //DeleteMovie("Cloud Atlas");
    
        UpdateMovie();
    
        GetMoviesAndTheActors();
    
        Console.ReadKey();
    }
  3. Press F5 to run the "MovieGraph" application.

image

🎉 You've successfully created, read, updated and deleted a Movie record. Next topic, we will discuss on how to load and update a relationship.