// See https://aka.ms/new-console-template for more information using Infrastructure; using Infrastructure.Entities; using Infrastructure.Stub; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; await using (var context = new StubbedContext(options)) { await context.Database.EnsureCreatedAsync(); Console.WriteLine("============Events============"); //Display all events var events = await context.Events.ToListAsync(); Console.WriteLine("--Display of the Events: (Tolal:" + events.Count + ")"); foreach (var eventEntity in events) Console.WriteLine(eventEntity); Console.WriteLine("--Create an event"); //Create a new event var evt = new EventEntity() { Title = "This is a title", Description = "This is a description", Date = DateTime.Now, nbPlaces = 100 }; context.Events.Add(evt); await context.SaveChangesAsync(); //verify if the event has been created var getEvt = await context.Events.FirstOrDefaultAsync(e => e.Title == "This is a title"); if( getEvt == null) { Console.WriteLine("Event not found (Creation failed)"); } else { Console.WriteLine("Event found (Creation success) :"); Console.WriteLine(getEvt); } //Update the event Console.WriteLine("--Update event"); getEvt.Title = "Changed title"; await context.SaveChangesAsync(); //verify if the event has been updated var getEvt2 = await context.Events.FirstOrDefaultAsync(e => e.Title == "Changed title"); if( getEvt2 == null) { Console.WriteLine("Event not found (Update failed)"); } else { Console.WriteLine("Event found (Update success) :"); Console.WriteLine(getEvt2); } //Delete the event Console.WriteLine("--Delete event"); context.Events.Remove(getEvt2); await context.SaveChangesAsync(); //verify if the event has been deleted var getEvt3 = await context.Events.FirstOrDefaultAsync(e => e.Title == "Changed title"); if( getEvt3 == null) { Console.WriteLine("Event not found (Deletion success)"); } else { Console.WriteLine("Event found (Deletion failed) :"); Console.WriteLine(getEvt3); } Console.WriteLine("\n============Alumnis============"); //Display all alumnis var alumnis = await context.Alumni.ToListAsync(); Console.WriteLine("Diplay the Alumnis: (Tolal:" + alumnis.Count + ")"); foreach (var alumniEntity in alumnis) Console.WriteLine(alumniEntity); Console.WriteLine("--Create an alumni"); //Create a new alumni var alumni = new User() { FirstName = "Leo", LastName = "Tuaillon", Email = "leo.tuaillon@gmail.com", EntryYear = "2021", Password = "1234567890", Role = "ADMIN" }; context.Alumni.Add(alumni); await context.SaveChangesAsync(); //verify if the alumni has been created var getAlumni = await context.Alumni.FirstOrDefaultAsync(a => a.FirstName == "Leo"); if( getAlumni == null) { Console.WriteLine("Alumni not found (Creation failed)"); } else { Console.WriteLine("Alumni found (Creation success) :"); Console.WriteLine(getAlumni); } //Update the alumni Console.WriteLine("--Update alumni"); getAlumni.FirstName = "Leo changed name"; await context.SaveChangesAsync(); //verify if the alumni has been updated var getAlumni2 = await context.Alumni.FirstOrDefaultAsync(a => a.FirstName == "Leo changed name"); if( getAlumni2 == null) { Console.WriteLine("Alumni not found (Update failed)"); } else { Console.WriteLine("Alumni found (Update success) :"); Console.WriteLine(getAlumni2); } //Delete the alumni Console.WriteLine("--Delete alumni"); context.Alumni.Remove(getAlumni2); await context.SaveChangesAsync(); //verify if the alumni has been deleted var getAlumni3 = await context.Alumni.FirstOrDefaultAsync(a => a.FirstName == "Leo changed name"); if( getAlumni3 == null) { Console.WriteLine("Alumni not found (Deletion success)"); } else { Console.WriteLine("Alumni found (Deletion failed) :"); Console.WriteLine(getAlumni3); } Console.WriteLine("\n============Experiences============"); //Display all experiences var experiences = await context.Experiences.ToListAsync(); Console.WriteLine("Display the Experiences: (Tolal:" + experiences.Count + ")"); foreach (var experienceEntity in experiences) Console.WriteLine(experienceEntity); Console.WriteLine("--Create an experience"); //Create a new experience var experience = new ExperienceEntity() { Title = "Ingénieur logiciel", StartDate = DateTime.Now, CompanyName = "Capgemini", IsCurrent = true }; context.Experiences.Add(experience); await context.SaveChangesAsync(); //verify if the experience has been created var getExperience = await context.Experiences.FirstOrDefaultAsync(e => e.Title == "Ingénieur logiciel"); if( getExperience == null) { Console.WriteLine("Experience not found (Creation failed)"); } else { Console.WriteLine("Experience found (Creation success) :"); Console.WriteLine(getExperience); } //Update the experience Console.WriteLine("--Update experience"); getExperience.Title = "Ingénieur logiciel changed"; await context.SaveChangesAsync(); //verify if the experience has been updated var getExperience2 = await context.Experiences.FirstOrDefaultAsync(e => e.Title == "Ingénieur logiciel changed"); if( getExperience2 == null) { Console.WriteLine("Experience not found (Update failed)"); } else { Console.WriteLine("Experience found (Update success) :"); Console.WriteLine(getExperience2); } //Delete the experience Console.WriteLine("--Delete experience"); context.Experiences.Remove(getExperience2); await context.SaveChangesAsync(); //verify if the experience has been deleted var getExperience3 = await context.Experiences.FirstOrDefaultAsync(e => e.Title == "Ingénieur logiciel changed"); if( getExperience3 == null) { Console.WriteLine("Experience not found (Deletion success)"); } else { Console.WriteLine("Experience found (Deletion failed) :"); Console.WriteLine(getExperience3); } Console.WriteLine("\n============Formations============"); //Display all formations var formations = await context.Formations.ToListAsync(); Console.WriteLine("Display the Formations: (Tolal:" + formations.Count + ")"); foreach (var formationEntity in formations) Console.WriteLine(formationEntity); Console.WriteLine("--Create a formation"); //Create a new formation var formation = new FormationEntity() { SchoolName = "IUT Clermont-Ferrand", Name = "BUT GEA", StartDate = DateTime.Now, IsCurrent = true }; context.Formations.Add(formation); await context.SaveChangesAsync(); //verify if the formation has been created var getFormation = await context.Formations.FirstOrDefaultAsync(f => f.Name == "BUT GEA"); if( getFormation == null) { Console.WriteLine("Formation not found (Creation failed)"); } else { Console.WriteLine("Formation found (Creation success) :"); Console.WriteLine(getFormation); } //Update the formation Console.WriteLine("--Update formation"); getFormation.Name = "BUT Mesures Physiques"; await context.SaveChangesAsync(); //verify if the formation has been updated var getFormation2 = await context.Formations.FirstOrDefaultAsync(f => f.Name == "BUT Mesures Physiques"); if( getFormation2 == null) { Console.WriteLine("Formation not found (Update failed)"); } else { Console.WriteLine("Formation found (Update success) :"); Console.WriteLine(getFormation2); } //Delete the formation Console.WriteLine("--Delete formation"); context.Formations.Remove(getFormation2); await context.SaveChangesAsync(); //verify if the formation has been deleted var getFormation3 = await context.Formations.FirstOrDefaultAsync(f => f.Name == "BUT Mesures Physiques"); if( getFormation3 == null) { Console.WriteLine("Formation not found (Deletion success)"); } else { Console.WriteLine("Formation found (Deletion failed) :"); Console.WriteLine(getFormation3); } Console.WriteLine("\nAlumni with their experiences, formations and events"); var alumniDetails = await context.Alumni .Include(a => a.Experiences) .Include(a => a.Formations) .Include(a => a.Events) .FirstOrDefaultAsync( a => a.FirstName == "Test"); if (alumniDetails == null) { Console.WriteLine("Alumni not found"); } else { Console.WriteLine(alumniDetails); } Console.WriteLine("Subscribe to an event"); var eventToSubscribe = await context.Events.FirstOrDefaultAsync(e => e.Title == "Laser Game"); var eventToSubscribe2 = await context.Events.FirstOrDefaultAsync(e => e.Title == "Afterwork Raclette"); if (eventToSubscribe == null || eventToSubscribe2 == null) { Console.WriteLine("Event not found"); } else { alumniDetails.Events.Add(eventToSubscribe); alumniDetails.Events.Add(eventToSubscribe2); await context.SaveChangesAsync(); Console.WriteLine(alumniDetails); Console.WriteLine("\tEvents subscribed :"); foreach (var eventEntity in alumniDetails.Events) { Console.WriteLine("\t"+eventEntity); } } Console.WriteLine("Unsubscribe to an event"); var eventToUnsubscribe = await context.Events.FirstOrDefaultAsync(e => e.Title == "Laser Game"); if (eventToUnsubscribe == null) { Console.WriteLine("Event not found"); } else { alumniDetails.Events.Remove(eventToUnsubscribe); await context.SaveChangesAsync(); Console.WriteLine(alumniDetails); Console.WriteLine("\tEvents unsubscribed :"); foreach (var eventEntity in alumniDetails.Events) { Console.WriteLine("\t" + eventEntity); } } Console.WriteLine("Add an experience and a formation to an alumni"); var experienceToAdd = new ExperienceEntity() { Title = "Stage", StartDate = DateTime.Now, CompanyName = "Capgemini", IsCurrent = true }; var formationToAdd = new FormationEntity() { SchoolName = "IUT Clermont-Ferrand", Name = "BUT GEA", StartDate = DateTime.Now, IsCurrent = true }; alumniDetails.Experiences.Add(experienceToAdd); alumniDetails.Formations.Add(formationToAdd); await context.SaveChangesAsync(); Console.WriteLine(alumniDetails); Console.WriteLine("\tExperiences added :"); foreach (var experienceEntity in alumniDetails.Experiences) { Console.WriteLine("\t" + experienceEntity); } Console.WriteLine("\tFormations added :"); foreach (var formationEntity in alumniDetails.Formations) { Console.WriteLine("\t" + formationEntity); } } connection.Close();