public ActionResult Update(Bam.Net.Data.Repositories.Tests.MainObject dao)
 {
     try
     {
         dao.Save();
         return(Json(new { Success = true, Message = "", Dao = dao.ToJsonSafe() }));
     }
     catch (Exception ex)
     {
         return(GetErrorResult(ex));
     }
 }
 public ActionResult Delete(long id)
 {
     try
     {
         string msg = "";
         Bam.Net.Data.Repositories.Tests.MainObject dao = Bam.Net.Data.Repositories.Tests.MainObject.OneWhere(c => c.KeyColumn == id);
         if (dao != null)
         {
             dao.Delete();
         }
         else
         {
             msg = string.Format("The specified id ({0}) was not found in the table (MainObject)", id);
         }
         return(Json(new { Success = true, Message = msg, Dao = "" }));
     }
     catch (Exception ex)
     {
         return(GetErrorResult(ex));
     }
 }
        public void ShouldBeAbleToCopyDaoAsGeneratedDtoType()
        {
            SQLiteDatabase saveTo = GetTestDatabase(false);
            MainObject     toAdd  = new MainObject();

            toAdd.Name = 4.RandomLetters().Plus(":: The Name");
            toAdd.Save(saveTo);

            object dtoInstance = Dto.Copy(toAdd);

            OutLine("Dto info", ConsoleColor.Cyan);
            OutLineFormat("Type: {0}.{1}", dtoInstance.GetType().Namespace, dtoInstance.GetType().Name);
            OutLine(dtoInstance.TryPropertiesToString());

            MainObject daoInstance = MainObject.FirstOneWhere(c => c.Name == dtoInstance.Property("Name"), saveTo);

            Expect.IsNotNull(daoInstance);
            Expect.AreSame(saveTo, daoInstance.Database);
            OutLine("Dao info", ConsoleColor.Cyan);
            OutLineFormat("Type: {0}.{1}", daoInstance.GetType().Namespace, daoInstance.GetType().Name);
            OutLine(daoInstance.TryPropertiesToString());
        }
        public void ShouldBeAbleToSetValuesFromQueryFilter()
        {
            SQLiteDatabase testDatabase = GetTestDatabase();
            string         testName     = 8.RandomLetters();
            QueryFilter    filter       = Query.Where("Name") == testName;
            MainObject     one          = MainObject.OneWhere(filter, testDatabase);

            Expect.IsNull(one);
            if (one == null)
            {
                one = new MainObject();
                filter.Parameters.Each(p =>
                {
                    one.Property(p.ColumnName, p.Value);
                });
                one.Save(testDatabase);
            }

            MainObject check = MainObject.OneWhere(filter, testDatabase);

            Expect.IsNotNull(check);
            OutLine(check.TryPropertiesToString());
        }
        private static void FillDatabaseWithTestData(SQLiteDatabase toBackup)
        {
            3.Times(i =>
            {
                OutLineFormat("Creating main...", ConsoleColor.DarkCyan);
                MainObject m = new MainObject();
                m.Created    = DateTime.UtcNow;
                m.Name       = "MainObject: {0}"._Format(i);
                m.Save(toBackup);
                OutLine(m.PropertiesToString(), ConsoleColor.Blue);

                OutLineFormat("\tAdding secondary...", ConsoleColor.Yellow);
                RandomNumber.Between(1, 3).Times(n =>
                {
                    SecondaryObject s = m.SecondaryObjectsByMainId.AddNew();
                    s.Created         = DateTime.UtcNow;
                    s.Name            = "\tSecondaryObject: {0}"._Format(n + 1);
                });
                m.Save(toBackup);
                m.SecondaryObjectsByMainId.Each(s =>
                {
                    OutLineFormat("\t\tAdding ternary...", ConsoleColor.Blue);
                    OutLine(s.PropertiesToString(), ConsoleColor.Cyan);
                    RandomNumber.Between(1, 3).Times(n =>
                    {
                        TernaryObject t = s.TernaryObjects.AddNew();
                        t.Created       = DateTime.UtcNow;
                        t.Name          = "\t\tTernaryObject: {0}"._Format(n + 1);
                    });
                    s.Save(toBackup);
                    s.TernaryObjects.Each(t =>
                    {
                        OutLineFormat(t.PropertiesToString(), ConsoleColor.DarkGray);
                    });
                });
            });
        }
 private static void SetupTestData(SQLiteDatabase testDatabase)
 {
     10.Times(i =>
     {
         MainObject o = new MainObject();
         o.Name       = 8.RandomLetters().Plus("::Main_Object {0}", i);
         o.Created    = DateTime.UtcNow;
         o.Save(testDatabase);
     });
     10.Times(i =>
     {
         SecondaryObject o = new SecondaryObject();
         o.Name            = 4.RandomLetters().Plus("::Secondary_object {0}", i);
         o.Created         = DateTime.UtcNow;
         o.Save(testDatabase);
     });
     10.Times(i =>
     {
         TernaryObject o = new TernaryObject();
         o.Name          = 3.RandomLetters().Plus("::Ternary_object ", i);
         o.Created       = DateTime.UtcNow;
         o.Save(testDatabase);
     });
 }
 public ActionResult Create(Bam.Net.Data.Repositories.Tests.MainObject dao)
 {
     return(Update(dao));
 }
        public void ChildCollectionsShouldRestoreProperly()
        {
            string sourceName = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Source");
            string destName   = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Destintation");

            Dao.GlobalInitializer = (dao) =>
            {
                dao.Property("Created", DateTime.UtcNow, false);
            };
            Dao.BeforeCommitAny += (db, dao) =>
            {
                dao.Property("Modified", DateTime.UtcNow, false);
            };

            SQLiteDatabase source;
            SQLiteDatabase dest;

            CreateTestDatabases(sourceName, destName, out source, out dest);

            MainObject main = new MainObject();

            main.Name = "The Main Parent";
            main.Save(source);
            SecondaryObject secondary        = main.SecondaryObjectsByMainId.AddNew();
            string          secondaryOneName = 8.RandomLetters();

            secondary.Name = secondaryOneName;
            SecondaryObject secondary2       = main.SecondaryObjectsByMainId.AddNew();
            string          secondaryTwoName = 6.RandomLetters();

            secondary2.Name = secondaryTwoName;
            main.Save(source);
            Expect.IsNotNullOrEmpty(main.Uuid);
            TernaryObject ternary = secondary2.TernaryObjects.AddNew();

            ternary.Name = 4.RandomLetters();
            ternary.Save(source);
            secondary2.Save(source);
            ternary = TernaryObject.OneWhere(c => c.Uuid == ternary.Uuid, source);
            Expect.AreEqual(1, ternary.SecondaryObjects.Count);
            string     uuid  = main.Uuid;
            MainObject check = MainObject.OneWhere(c => c.Id == main.Id, source);

            Expect.IsTrue(check.SecondaryObjectsByMainId.Count == 2);

            string             methodName = MethodBase.GetCurrentMethod().Name;
            List <IRepository> repos      = new List <IRepository>();

            repos.Add(new DaoRepository(new SQLiteDatabase("BackupRepo_{0}"._Format(methodName), "BackupRepoDb")));
            //repos.Add(new ObjectRepository("ObjectRepo_{0}"._Format(methodName)));

            foreach (IRepository repo in repos)
            {
                DaoBackup backup = new DaoBackup(typeof(MainObject).Assembly, source, repo);
                backup.Backup();
                HashSet <OldToNewIdMapping> wasIs = backup.Restore(dest);
                OutputWasIs(wasIs);
                MainObject toValidate = MainObject.OneWhere(c => c.Uuid == uuid, dest);
                Expect.IsTrue(toValidate.SecondaryObjectsByMainId.Count == 2);
                List <string> names = toValidate.SecondaryObjectsByMainId.Select(s => s.Name).ToList();
                Expect.IsTrue(names.Contains(secondaryOneName));
                Expect.IsTrue(names.Contains(secondaryTwoName));
                SecondaryObject secondary2Check = SecondaryObject.OneWhere(c => c.Uuid == secondary2.Uuid, dest);
                Expect.IsNotNull(secondary2Check);
                Expect.AreEqual(1, secondary2Check.TernaryObjects.Count);
                Expect.IsTrue(secondary2Check.TernaryObjects[0].Name.Equals(ternary.Name));
                TernaryObject ternaryCheck = TernaryObject.OneWhere(c => c.Uuid == secondary2.TernaryObjects[0].Uuid, dest);
                Expect.IsTrue(ternaryCheck.SecondaryObjects.Count == 1);
            }
        }