Exemplo n.º 1
0
 public void MoveCompany(int nodeId, int?newParentId)
 {
     using (CompaniesDAL dal = new CompaniesDAL())
     {
         Companies company = dal.Companies.Where(x => x.Id == nodeId).Single();
         company.ParentId = newParentId;
         dal.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public ActionResult Delete(int id)
        {
            using (CompaniesDAL context = new CompaniesDAL())
            {
                BusinessLogic.DeleteNodes(context, id);
                context.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
 public void UpdateCompanies(int?nodeId, string title, int?earnings)
 {
     using (CompaniesDAL dal = new CompaniesDAL())
     {
         Companies company = dal.Companies.Where(x => x.Id == nodeId).Single();
         company.Title    = title;
         company.Earnings = earnings;
         dal.SaveChanges();
     }
 }
Exemplo n.º 4
0
        public void DeleteNodes(CompaniesDAL dal, int id)
        {
            Companies[] companiesList = dal.Companies.Where(x => x.ParentId == id && !x.IsDeleted).ToArray();

            foreach (Companies node in companiesList)
            {
                node.IsDeleted = true;
                DeleteNodes(dal, node.Id);
            }
            var deleted = dal.Companies.Where(x => x.Id == id && !x.IsDeleted).Single();

            deleted.IsDeleted = true;
        }
Exemplo n.º 5
0
 public void AddCompanies(int?parentId, string title, int?earnings)
 {
     using (CompaniesDAL dal = new CompaniesDAL())
     {
         Companies company = new Companies()
         {
             ParentId = parentId,
             Title    = title,
             Earnings = earnings
         };
         dal.Companies.Add(company);
         dal.SaveChanges();
     }
 }
Exemplo n.º 6
0
        public bool ContainsChilds(CompaniesDAL dal, int parentId, int id)
        {
            bool presenceOfChild = false;

            Companies[] companiesList = dal.Companies.Where(x => x.ParentId == parentId && !x.IsDeleted).ToArray();

            foreach (var node in companiesList)
            {
                if (node.Id == id && node.ParentId == parentId)
                {
                    return(true);
                }
                presenceOfChild = ContainsChilds(dal, node.Id, id);
            }

            return(presenceOfChild);
        }
Exemplo n.º 7
0
        public ActionResult Move(int nodeId, int?newParentId)
        {
            if (nodeId == newParentId)
            {
                return(RedirectToAction("Index"));
            }

            using (CompaniesDAL dal = new CompaniesDAL())
            {
                if (newParentId.HasValue && BusinessLogic.ContainsChilds(dal, nodeId, newParentId.Value))
                {
                    return(RedirectToAction("Index"));
                }
            }
            BusinessLogic.MoveCompany(nodeId, newParentId);

            return(RedirectToAction("Index"));
        }
            public CompaniesDBInitializer()
            {
                Database.SetInitializer <CompaniesDAL>(null);

                try
                {
                    using (var dal = new CompaniesDAL())
                    {
                        if (!dal.Database.Exists())
                        {
                            ((IObjectContextAdapter)dal).ObjectContext.CreateDatabase();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The companies database could not be initialized.", ex);
                }
            }
Exemplo n.º 9
0
        public CompaniesListModel GetCompanies()
        {
            using (CompaniesDAL dal = new CompaniesDAL())
            {
                Companies[] Companies = dal.Companies.Where(x => !x.IsDeleted).ToArray();
                foreach (Companies company in Companies)
                {
                    int?[] totalChildEarnings = dal.Companies.Where(x => x.ParentId == company.Id).Select(x => x.Earnings).ToArray();
                    company.TotalEarnings = totalChildEarnings.Sum() + company.Earnings;
                }

                CompaniesListModel model = new CompaniesListModel()
                {
                    Companies = Companies.Select(x => new CompaniesModel(x))
                };

                return(model);
            }
        }