public async Task<IActionResult> Create(SkillEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!User.IsUserType(UserType.SiteAdmin))
                {
                    model.OwningOrganizationId = User.GetOrganizationId();
                }

                await _mediator.SendAsync(new SkillEditCommand { Skill = model });

                return RedirectToAction(nameof(Index), new { area = "Admin" });
            }

            if (User.IsUserType(UserType.SiteAdmin))
            {
                model.ParentSelection = await _mediator.SendAsync(new SkillListQuery());
                model.OrganizationSelection = await _mediator.SendAsync(new OrganizationSelectListQuery());
            }
            else
            {
                var organizationId = User.GetOrganizationId();
                if (!organizationId.HasValue)
                {
                    return new UnauthorizedResult(); // Edge case of user having Org Admin claim but not Org Id claim
                }
                
                model.ParentSelection = await _mediator.SendAsync(new SkillListQuery { OrganizationId = organizationId.Value });
            }

            return View("Edit", model);
        }
        public async Task AddNewSkill()
        {
            // Arrange
            var handler = new SkillEditCommandHandler(Context);
            var newSkill = new SkillEditViewModel { Name = "New", Description = "Desc" };

            // Act
            var result = await handler.Handle(new SkillEditCommand { Skill = newSkill });

            // Assert
            Assert.Equal(8, Context.Skills.Count());
            Assert.Equal(8, result);
        }
        public async Task UpdatingExistingSkill()
        {
            // Arrange
            var handler = new SkillEditCommandHandler(Context);
            var newSkill = new SkillEditViewModel { Id = 2, Name = "New", Description = "Desc", OwningOrganizationId = 1 };

            // Act
            var result = await handler.Handle(new SkillEditCommand { Skill = newSkill });
            var savedSkill = Context.Skills.SingleOrDefault(s => s.Id == 2);

            // Assert
            Assert.Equal(7, Context.Skills.Count());
            Assert.Equal(2, result);
            Assert.Equal("New", savedSkill.Name);
        }
        public async Task<IActionResult> Create()
        {
            var organizationId = User.GetOrganizationId();

            if (!User.IsUserType(UserType.SiteAdmin) && !organizationId.HasValue)
            {
                return new UnauthorizedResult(); // Edge case of user having Org Admin claim but not Org Id claim
            }
            
            var model = new SkillEditViewModel();

            if (User.IsUserType(UserType.SiteAdmin))
            {
                model.ParentSelection = await _mediator.SendAsync(new SkillListQuery());
                model.OrganizationSelection = await _mediator.SendAsync(new OrganizationSelectListQuery());
            }
            else
            {              
                model.ParentSelection = await _mediator.SendAsync(new SkillListQuery { OrganizationId = organizationId.Value });
            }

            return View("Edit", model);
        }
        private static Mock<IMediator> MockMediatorSkillEditQuery(out SkillController controller, SkillEditViewModel model = null)
        {
            if (model == null) model = new SkillEditViewModel { Id = 1, Name = "Name", Description = "Description" };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<SkillEditQuery>())).Returns(() => Task.FromResult(model)).Verifiable();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<SkillListQuery>())).Returns(() => Task.FromResult(SummaryListItems()))
                .Verifiable();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<OrganizationSelectListQuery>()))
                .Returns(() => Task.FromResult<IEnumerable<SelectListItem>>(new List<SelectListItem> { new SelectListItem { Text = "Item 1", Value = "1" } }))
                .Verifiable();
            controller = new SkillController(mockMediator.Object);
            return mockMediator;
        }
Example #6
0
        public async Task<IActionResult> Edit(SkillEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                await _mediator.SendAsync(new SkillEditCommandAsync { Skill = model });
                return RedirectToAction(nameof(Index), new { area = "Admin" });
            }

            if (User.IsUserType(UserType.SiteAdmin))
            {
                model.ParentSelection = await _mediator.SendAsync(new SkillListQueryAsync());
                model.OrganizationSelection = await _mediator.SendAsync(new OrganizationSelectListQueryAsync());
            }
            else
            {
                var organizationId = User.GetOrganizationId();
                if (!organizationId.HasValue)
                {
                    return new UnauthorizedResult(); // Edge case of user having Org Admin claim but not Org Id claim
                }

                model.ParentSelection = await _mediator.SendAsync(new SkillListQueryAsync { OrganizationId = organizationId.Value });
            }

            model.ParentSelection = model.ParentSelection.Where(p => p.Id != model.Id); // remove self from the parent select list

            return View(model);
        }