public virtual void Insert(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (ValidateModel(employee, modelState))
            {
                if (!UpdateDatabase)
                {
                    var employees = GetAll();
                    var first     = employees.OrderByDescending(e => e.EmployeeId).FirstOrDefault();
                    var id        = (first != null) ? first.EmployeeId : 0;

                    employee.EmployeeId = id + 1;

                    employees.Insert(0, employee);

                    Session.SetObjectAsJson("EmployeeDirectory", employees);
                }
                else
                {
                    using (var db = GetContext())
                    {
                        var entity = employee.ToEntity();

                        db.EmployeeDirectories.Add(entity);
                        db.SaveChanges();

                        employee.EmployeeId = entity.EmployeeID;
                    }
                }
            }
        }
        public virtual void Delete(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (!UpdateDatabase)
            {
                var employees = GetAll();
                var target    = employees.FirstOrDefault(e => e.EmployeeId == employee.EmployeeId);

                if (target != null)
                {
                    DeleteSessionChildren(target, employees);

                    employees.Remove(target);
                }

                Session.SetObjectAsJson("EmployeeDirectory", employees);
            }
            else
            {
                using (var db = GetContext())
                {
                    var entity = employee.ToEntity();
                    db.EmployeeDirectories.Attach(entity);
                    DeleteEntityChildren(entity);
                    db.SaveChanges();
                }
            }
        }
        public virtual void Delete(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            var entity = employee.ToEntity();

            db.EmployeeDirectory.Attach(entity);
            db.EmployeeDirectory.Remove(entity);
            db.SaveChanges();
        }
        public JsonResult Update([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee)
        {
            if (ModelState.IsValid)
            {
                employeeDirectory.Update(employee, ModelState);
            }

            return Json(new[] { employee }.ToTreeDataSourceResult(request, ModelState));
        }
 public virtual void Update(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
 {
     if (ValidateModel(employee, modelState))
     {
         var entity = employee.ToEntity();
         db.EmployeeDirectory.Attach(entity);
         db.Entry(entity).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
        private bool ValidateModel(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (employee.HireDate < employee.BirthDate)
            {
                modelState.AddModelError("errors", "Employee cannot be hired before birth.");
                return(false);
            }

            return(true);
        }
        private void DeleteSessionChildren(EmployeeDirectoryModel employee, IList <EmployeeDirectoryModel> employees)
        {
            var subordinates = employees.Where(m => m.ReportsTo == employee.EmployeeId).ToList();

            foreach (var subordinate in subordinates)
            {
                DeleteSessionChildren(subordinate, employees);

                employees.Remove(subordinate);
            }
        }
        public virtual void Insert(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (ValidateModel(employee, modelState))
            {
                var entity = employee.ToEntity();

                db.EmployeeDirectory.Add(entity);
                db.SaveChanges();

                employee.EmployeeId = entity.EmployeeID;
            }
        }
        public virtual void Update(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (ValidateModel(employee, modelState))
            {
                if (!UpdateDatabase)
                {
                    var employees = GetAll();
                    var target    = employees.FirstOrDefault(e => e.EmployeeId == employee.EmployeeId);

                    if (target != null)
                    {
                        target.FirstName = employee.FirstName;
                        target.LastName  = employee.LastName;
                        target.Address   = employee.Address;
                        target.City      = employee.City;
                        target.Country   = employee.Country;
                        target.Phone     = employee.Phone;
                        target.Extension = employee.Extension;
                        target.BirthDate = employee.BirthDate;
                        target.HireDate  = employee.HireDate;
                        target.Position  = employee.Position;
                        target.ReportsTo = employee.ReportsTo;
                    }

                    Session.SetObjectAsJson("EmployeeDirectory", employees);
                }
                else
                {
                    using (var db = GetContext())
                    {
                        var entity = employee.ToEntity();
                        db.EmployeeDirectories.Attach(entity);
                        db.Entry(entity).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
        }
        public virtual void Delete(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            var entity = employee.ToEntity();

            db.EmployeeDirectories.Attach(entity);

            Delete(entity);

            db.SaveChanges();
        }
        private bool ValidateModel(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (employee.HireDate < employee.BirthDate)
            {
                modelState.AddModelError("errors", "Employee cannot be hired before birth.");
                return false;
            }

            return true;
        }
 public virtual void Update(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
 {
     if (ValidateModel(employee, modelState))
     {
         var entity = employee.ToEntity();
         db.EmployeeDirectories.Attach(entity);
         db.Entry(entity).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
        public virtual void Insert(EmployeeDirectoryModel employee, ModelStateDictionary modelState)
        {
            if (ValidateModel(employee, modelState))
            {
                var entity = employee.ToEntity();

                db.EmployeeDirectories.Add(entity);
                db.SaveChanges();

                employee.EmployeeId = entity.EmployeeID;
            }
        }