Blueprint41 CRUD - circles-arrows/blueprint41 GitHub Wiki
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.
-
In the
Mainmethod, 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; -
In the
Mainmethod, create an instance of your datastore modelDataStoreand call theExecutemethod 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
-
In the file
Program.cs, create a static methodCreateMoviesAndActorsand in that method, create a transaction and instantiate the new objects for theMovieandPersonentity.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)elseTransaction.Begin()when just having a query.
Add missing using directive.
Creating Relationship for Movie, Person and Genre
-
In the
CreateMoviesAndActorsmethod, copy the code below and paste it before the lineTransaction.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
PersonkeanuandlaurenceasActorson the"The Matrix"movie, andtomon"Cloud Atlas"movie. TheMovieMatrixwill also haveActionandSci-Figenres;Sci-Fifor"Cloud Atlas"Movie. -
In the
Mainmethod, call theCreateMoviesAndActorsmethod.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(); } -
Run the "MovieGraph" application. Select Debug > Start Debugging or press F5.
You have successfully created records of the Person, Movie and Genre entity.
[!Note] After creating the records, in the
Mainmethod, comment the line where the methodCreateMoviesAndActorsis called so that it won't create duplicate records.
Get Movies and its Genres and Actors
-
To display the
Movierecords with its properties (title,tagline,released),Genresand it'sActors. Create the static methodGetMoviesAndTheActors. Then in that method, create a transaction and use theMovie.GetAll()method to get allMovierecords.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... } } -
In the
Mainmethod, call theGetMoviesAndTheActorsmethod after theCreateMoviesAndActorsmethod and perform theConsole.Readmethod 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(); } -
Press F5 to run the "MovieGraph" application.
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
-
To delete the
Movierecord, create the static methodDeleteMovie(string). In the method, create a transaction and get theMovierecord by calling theMovie.LoadBytitle(string)method and setmovieTitleparameter value to"Cloud Atlas". After getting the record, make sure to remove the relationships forActorsandGenresfirst. Use the methodmovie.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(); } } -
In the
Mainmethod, call theDeleteMoviemethod after theCreateMoviesAndActorsmethod and before theGetMoviesAndTheActorsmethod.
[!Note] Comment the line where the method
GetMoviesAndTheActorsis 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();
}
- Press F5 to run the "MovieGraph" application.
You've successfully deleted the Movie "Cloud Atlas". Next is to update a Movie record.
Updating a Movie record
-
To update a
Movierecord, create the static methodUpdateMovie. In the method, create a transaction and get theMovierecord by calling theMovie.LoadBytitle(string)method and settitleparameter 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(); } } -
In the
Mainmethod, call theUpdateMoviemethod after theDeleteMoviemethod and before theGetMoviesAndTheActorsmethod.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(); } -
Press F5 to run the "MovieGraph" application.
🎉 You've successfully created, read, updated and deleted a Movie record. Next topic, we will discuss on how to load and update a relationship.