示例#1
0
        public static void FilledBy(this JoinModel model, Project project, int userId)
        {
            model.Item = new FullItemModel();
            model.Children = new List<JoinModel>();

            model.Item.FilledBy(project);
            model.HasJoined = project.Authorizations.Count(x => x.User.Id == userId) > 0;
            foreach (Project childProject in project.Children)
            {
                JoinModel childModel = new JoinModel();
                childModel.FilledBy(childProject, userId);
                model.Children.Add(childModel);
            }
        }
        public ActionResult Join(JoinModel model)
        {
            string[] items = Request.Form["Item.LiteItem.Id"].Split(',');
            string[] selected = Request.Form["Selected"].Split(',');

            //offset used here because asp.net mvc add hidden input for every checkbox,
            //and only checked will post back with true value
            //so selected'length = item's lenght + offset(selected number)
            int offset = 0;
            for (int i = 0; i < selected.Length; i++)
            {
                if (Convert.ToBoolean(selected[i]))
                {
                    int projectId = Convert.ToInt32(items[i - offset]);

                    if (!_projectService.HasJoined(projectId, userHelper.CurrentUserId.Value))
                    {
                        _projectService.Join(projectId, userHelper.CurrentUserId.Value);
                    }
                    else
                    {
                        //TODO: is it necessary?
                        throw new Exception("trying to join project twice with project id {0} and user id {1}");
                    }

                    offset++;
                }
            }

            return RedirectToAction("Join");
        }
        public JoinModel GetJoinTree(int projectId, int currentUserId)
        {
            JoinModel model = new JoinModel();
            Project project = session.Load<Project>(projectId);
            model.FilledBy(project, currentUserId);

            return model;
        }
        public void JoinModel_FilledBy_Project_And_User()
        {
            #region Set Projects Tree

            Project root = new Project();
            root.MockId(0);
            Project child_project_1 = new Project();
            child_project_1.MockId(1);
            Project child_project_2 = new Project();
            child_project_2.MockId(2);
            Project child_project_1_1 = new Project();
            child_project_1_1.MockId(11);
            Project child_project_1_2 = new Project();
            child_project_1_2.MockId(12);
            Project child_project_1_3 = new Project();
            child_project_1_3.MockId(13);
            Project child_project_2_1 = new Project();
            child_project_2_1.MockId(21);
            Project child_project_1_2_1 = new Project();
            child_project_1_2_1.MockId(121);
            Project child_project_1_2_2 = new Project();
            child_project_1_2_2.MockId(122);

            child_project_1_2_1.Parent = child_project_1_2;
            child_project_1_2_2.Parent = child_project_1_2;
            child_project_1_2.Children = new List<Project> { child_project_1_2_1, child_project_1_2_2 };

            child_project_1_1.Parent = child_project_1;
            child_project_1_2.Parent = child_project_1;
            child_project_1_3.Parent = child_project_1;
            child_project_1.Children = new List<Project> { child_project_1_1, child_project_1_2, child_project_1_3 };

            child_project_2_1.Parent = child_project_2;
            child_project_2.Children = new List<Project> { child_project_2_1 };

            child_project_1.Parent = root;
            child_project_2.Parent = root;
            root.Children = new List<Project> { child_project_1, child_project_2 };

            #endregion

            #region user_1_2_1

            User user_1_2_1 = new User();
            user_1_2_1.MockId(12100);
            user_1_2_1.Join(child_project_1_2_1);

            JoinModel model_1_2_1 = new JoinModel();
            model_1_2_1.FilledBy(root, user_1_2_1.Id);

            Assert.That(model_1_2_1.Children, Is.Null);
            Assert.That(model_1_2_1.HasJoined, Is.True);
            Assert.That(model_1_2_1.Item.LiteItem.Id, Is.EqualTo(0));
            Assert.That(model_1_2_1.Selected, Is.True);

            #endregion

            #region user_1_2

            #region Joined Only Single child_project_1_2

            User user_1_2 = new User();
            user_1_2.MockId(1200);
            user_1_2.Join(child_project_1_2);

            JoinModel model_1_2_single = new JoinModel();
            model_1_2_single.FilledBy(child_project_1_2, user_1_2.Id);

            //the user doesn't join child_project_1_1_x, so its Children still null
            Assert.That(model_1_2_single.Children, Is.Null);
            Assert.That(model_1_2_single.HasJoined, Is.True);
            Assert.That(model_1_2_single.Item.LiteItem.Id, Is.EqualTo(0));
            Assert.That(model_1_2_single.Selected, Is.True);

            #endregion

            #region Joined both child_project_1_2 and child_project_1_2_2

            user_1_2.Join(child_project_1_2_2);

            JoinModel model_1_2_multiple = new JoinModel();
            model_1_2_multiple.FilledBy(child_project_1_2, user_1_2.Id);

            Assert.That(model_1_2_multiple.HasJoined, Is.True);
            Assert.That(model_1_2_multiple.Item.LiteItem.Id, Is.EqualTo(0));
            Assert.That(model_1_2_multiple.Selected, Is.True);

            Assert.That(model_1_2_multiple.Children.Count, Is.EqualTo(2));

            JoinModel model_1_2_child_1 = model_1_2_multiple.Children.Where(m => m.Item.LiteItem.Id == child_project_1_2_1.Id).SingleOrDefault();
            Assert.That(model_1_2_child_1.HasJoined, Is.False);
            Assert.That(model_1_2_child_1.Selected, Is.False);

            JoinModel model_1_2_child_2 = model_1_2_multiple.Children.Where(m => m.Item.LiteItem.Id == child_project_1_2_2.Id).SingleOrDefault();
            Assert.That(model_1_2_child_2.HasJoined, Is.True);
            Assert.That(model_1_2_child_2.Selected, Is.True);

            #endregion

            #endregion

            #region user_1

            User user_1 = new User();
            user_1.MockId(1);

            user_1.Join(child_project_1);
            user_1.Join(child_project_1_1);
            user_1.Join(child_project_1_2_1);
            user_1.Join(child_project_1_2_2);

            JoinModel model_1 = new JoinModel();
            model_1.FilledBy(child_project_1, user_1.Id);

            //make sure child_project_1_3 is in the tree
            Assert.That(model_1.Children.Count, Is.EqualTo(3));

            //make sure child_project_1_2_1, child_project_1_2_2 is in the tree
            JoinModel model_1_child_2 = model_1.Children.Where(m => m.Item.LiteItem.Id == child_project_1_2.Id).SingleOrDefault();
            Assert.That(model_1_child_2.Children.Count, Is.EqualTo(2));
            //and child_project_1_2 is not joined
            Assert.That(model_1_child_2.HasJoined, Is.False);
            Assert.That(model_1_child_2.Selected, Is.False);

            #endregion
        }
        public JoinModel GetJoinTree(int projectId, int currentUserId)
        {
            JoinModel model_1_1 = new JoinModel
            {
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 18, Name = "MS SQL" } }
            };
            JoinModel model_1_2 = new JoinModel
            {
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 19, Name = "MySQL" } }
            };
            JoinModel model_1 = new JoinModel
            {
                HasJoined = false,
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 7, Name = "DBA" } },
                Children = new List<JoinModel> { model_1_1, model_1_2 }
            };
            JoinModel model_2_1 = new JoinModel
            {
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 22, Name = "BLL" } }
            };
            JoinModel model_2_2 = new JoinModel
            {
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 23, Name = "Service层" } }
            };
            JoinModel model_2 = new JoinModel
            {
                HasJoined = true,
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 8, Name = "C#" } },
                Children = new List<JoinModel> { model_2_1, model_2_2 }
            };
            JoinModel model = new JoinModel
            {
                HasJoined = true,
                Item = new FullItemModel { LiteItem = new LiteItemModel { Id = 3, Name = "后台" } },
                Children = new List<JoinModel> { model_1, model_2 }
            };

            return model;
        }