public async Task <IActionResult> Create(MemberViewModel memberViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(memberViewModel));
            }

            Member member = new Member
            {
                Name             = memberViewModel.Name,
                Document         = memberViewModel.Document,
                DateBirth        = memberViewModel.DateBirth,
                Address          = memberViewModel.Address,
                Neighborhood     = memberViewModel.Neighborhood,
                City             = memberViewModel.City,
                State            = memberViewModel.State,
                Mailbox          = memberViewModel.Mailbox,
                Email            = memberViewModel.Email,
                PhoneNumber      = memberViewModel.PhoneNumber,
                Baptized         = memberViewModel.Baptized,
                Status           = memberViewModel.Status,
                RegistrationDate = memberViewModel.RegistrationDate
            };

            foreach (var item in memberViewModel.GroupsIds ?? Enumerable.Empty <Guid>())
            {
                member.AddGroup(await _groupRepository.GetEntityById(item));
            }

            foreach (var item in memberViewModel.RolesIds ?? Enumerable.Empty <Guid>())
            {
                member.AddRole(await _roleRepository.GetEntityById(item));
            }

            try
            {
                await _memberRepository.CreateEntity(member);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                InitializeMember();
                return(View(MemberVM));
            }
        }
Exemple #2
0
        public void Member_AddRole()
        {
            var user    = FakeObjects.TestUserWithId();
            var roles   = FakeObjects.TestRoles();
            var newRole = new Role("Admin", "Administrator", "Administrator Member", FakeObjects.TestPermissions());

            var member = new Member(
                user,
                user,
                FakeObjects.TestProjectWithId(),
                roles
                );

            Assert.IsTrue(member.Roles.ToList().Count == 1);

            member.AddRole(newRole);

            Assert.IsTrue(member.Roles.ToList().Count == 2);
        }