Example #1
0
        public Department GetDepartmentById(int id)
        {
            MySqlConnection conn = Connect();

            try
            {
                conn.Open();
                string          sql = "SELECT * FROM department WHERE DepartmentId = " + id;
                MySqlCommand    cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                /*
                 * Create company(rdr[0 - 45])
                 * Create Product(rdr,45);
                 * row[bound+1)
                 */

                Department department = DepartmentFactory.Create(rdr);
                return(department);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }
            finally
            {
                conn.Close();
            }
        }
        // Create Department
        public static DepartmentCreate.Response Handle(IRepository repository, DepartmentCreate.Request request)
        {
            // Validation now performed in the dispacther decorators (See AutoValidate<T> in the DomainBootstrapper class)

            var container         = DepartmentFactory.Create(request.CommandModel);
            var validationDetails = repository.Save(container);

            var deptId = default(int?);

            if (!validationDetails.HasValidationIssues)
            {
                deptId = container.FindEntity <Department>().DepartmentID;
            }

            return(new DepartmentCreate.Response(validationDetails, deptId));
        }
        public void createOracletestRecords()
        {
            ModelContext.Current.config.DoCascadeDeletes = true;
            ModelContext.beginTrans();
            ModelContext.Current.addGlobalModelValidator(typeof(Employee), typeof(EmployeeValidator));

            try {
                Employee e = EmployeeFactory.Create();
                e.PrDepartment = DepartmentFactory.Create();
                e.PrDepartment.PrDepartmentName = "My New Dept";
                e.PrDepartment.PrLocationId     = LocationDataUtils.findList()[0].PrLocationId;

                e.PrFirstName   = "test";
                e.PrLastName    = "Lastname";
                e.PrSALARY      = 100m;
                e.PrEMAIL       = "*****@*****.**";
                e.PrPhoneNumber = "1030045";
                e.PrHireDate    = new DateTime(DateTime.Now.Year, 1, 1);
                e.PrJobId       = JobDataUtils.findList()[0].PrJobId;

                e.PrTrainingHistoryAdd(EmployeeTrainingHistoryFactory.Create());
                EmployeeTrainingHistory emplProj = e.PrTrainingHistoryGetAt(0);
                emplProj.PrDateFrom = new DateTime(DateTime.Now.Year, 3, 1);
                emplProj.PrDateTo   = new DateTime(DateTime.Now.Year, 6, 1);

                emplProj.PrTrainingCourse           = TrainingCourseFactory.Create();
                emplProj.PrTrainingCourse.PrCODE    = "X1";
                emplProj.PrTrainingCourse.PrDescrEn = "New Course";
                emplProj.PrTrainingCourse.PrDescrGr = "Νέο";
                Assert.IsTrue(e.isNew);
                Assert.IsTrue(e.isDirty);
                Assert.IsTrue(e.NeedsSave);

                // 3 ways to persist to database
                // method 1: use ModelContext.Current().save

                Assert.IsTrue(e.CreateDate == null, "Before save, created date is null");
                Assert.IsTrue(e.UpdateDate == null, "Before save, UpdateDate is not null");
                ModelContext.Current.saveModelObject(e);
                Assert.IsTrue(e.PrPhoneNumber == "12345XX", "12345XX value in PrPhoneNumber is Proof that validator was called");
                Assert.IsTrue(e.CreateDate != null, "Before save, created date is not null");
                Assert.IsTrue(e.UpdateDate != null, "Before save, UpdateDate is not null");
                Assert.IsTrue(e.CreateUser != null, "Before save, CreateUser date is not null");
                Assert.IsTrue(e.UpdateUser != null, "Before save, UpdateUser is not null");
                //Assert.IsTrue(e.UpdateDate.Value.Ticks == e.CreateDate.Value.Ticks, "update date = create date after saving new");
                Assert.IsTrue(e.UpdateUser == e.CreateUser, "update date = create date after saving new");

                long x = e.PrEmployeeId;
                Assert.IsFalse(e.isNew, "After save, model object isNew property must return false");
                Assert.IsFalse(e.isDirty, "After save to db, model object isDirty property must return false");

                e = EmployeeDataUtils.findByKey(x);

                Assert.IsNotNull(e, "New employee not found");

                Assert.IsFalse(e.isNew, "After load from db, model object isNew property returns false");
                Assert.IsFalse(e.isDirty, "After load from db, model object isDirty property returns false");

                Assert.AreEqual(e.PrDepartment.PrDepartmentName, "My New Dept");
                Assert.AreEqual(e.PrSALARY, 100m);
                Assert.AreEqual(e.PrLastName, "Lastname");
                Assert.AreEqual(e.PrPhoneNumber, "12345XX");
                Assert.AreEqual(e.PrHireDate, new DateTime(2015, 1, 1));
                Assert.AreEqual(e.PrTrainingHistory.ToList().Count, 1);
                Assert.AreEqual(e.PrTrainingHistoryGetAt(0).PrTrainingCourse.PrDescrEn, "New Course");

                //change some values on child and parent objects
                e.PrTrainingHistoryGetAt(0).PrDateTo = new DateTime(DateTime.Now.Year, 6, 1);
                e.PrTrainingHistoryGetAt(0).PrTrainingCourse.PrDescrEn = "New Course Updated"; // here we are updating parent record of child object of employee!
                Assert.IsTrue(e.NeedsSave, "After changing parent or child obejcts values, e.NeedsSave must be true");
                Assert.IsFalse(e.isDirty, "After changing parent or child obejcts values, e.isDirty must be false since we did not change anything on the Model Object");

                // method 2: call [ModelObject]DataUtils.save
                EmployeeDataUtils.saveEmployee(e);
                //Assert.IsTrue(e.UpdateDate > e.CreateDate, "after update of record, update must be date > create date ");
                // note that above test cannot be sucess since save is happening too fast

                Assert.AreEqual(e.PrTrainingHistoryGetAt(0).PrDateTo, new DateTime(DateTime.Now.Year, 6, 1));
                Assert.AreEqual(e.PrTrainingHistoryGetAt(0).PrTrainingCourse.PrDescrEn, "New Course Updated", "Expected to have parent record of child updated!");

                e.PrPhoneNumber = "XXXXX";
                Assert.IsTrue(e.NeedsSave, "After changing value, e.NeedsSave must be true");
                Assert.IsTrue(e.isDirty, "After changing value e.isDirty must be true");

                // method 3: call [ModelObject]dbMapper.save
                new EmployeeDBMapper().saveEmployee(e);
                e = EmployeeDataUtils.findByKey(x);
                Assert.AreEqual(e.PrPhoneNumber, "XXXXX");
                Assert.AreEqual(e.PrTrainingHistoryGetAt(0).PrDateTo, new DateTime(DateTime.Now.Year, 6, 1));
                Assert.AreEqual(e.PrTrainingHistoryGetAt(0).PrTrainingCourse.PrDescrEn, "New Course Updated", "Expected to have parent record of child updated!");

                e.PrTrainingHistoryClear();
                Assert.AreEqual(e.PrTrainingHistory.ToList().Count, 0, "Expected to have no Projects linked after call to clear");
                EmployeeDataUtils.saveEmployee(e);

                e = EmployeeDataUtils.findByKey(x);
                Assert.AreEqual(e.PrTrainingHistory.ToList().Count, 0, "Expected to have no Projects linked, after reloading from db");

                EmployeeDataUtils.deleteEmployee(e);
                e = EmployeeDataUtils.findByKey(x);
                Assert.IsNull(e, "New employee must have been deleted!");

                // now let's test string primary key
                Country et = CountryFactory.Create();
                et.PrCountryName = "A Description";
                et.PrCountryId   = "XX";

                Country et1 = CountryFactory.Create();
                et1.PrCountryName = "A Description 1";
                et1.PrCountryId   = "Y7";

                Country et2 = CountryFactory.Create();
                et2.PrCountryName = "A Description 2";
                et2.PrCountryId   = "H8";

                CountryDataUtils.saveCountry(et, et1, et2);

                et2 = CountryDataUtils.findByKey("H8");
                Assert.IsNotNull(et2, "New Country must have been created!");
                et1 = CountryDataUtils.findByKey("Y7");
                Assert.IsNotNull(et1, "New Country must have been created!");
            } finally {
                ModelContext.rollbackTrans();
            }
        }