public async Task<IActionResult> Create(SkillEditModel model) { if (ModelState.IsValid) { if (!User.IsUserType(UserType.SiteAdmin)) { model.OwningOrganizationId = User.GetOrganizationId(); } 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 }); } return View("Edit", model); }
public async Task AddNewSkill() { // Arrange var handler = new SkillEditCommandHandlerAsync(Context); var newSkill = new SkillEditModel { Name = "New", Description = "Desc" }; // Act var result = await handler.Handle(new SkillEditCommandAsync { Skill = newSkill }); // Assert Assert.Equal(8, Context.Skills.Count()); Assert.Equal(8, result); }
public async Task UpdatingExistingSkill() { // Arrange var handler = new SkillEditCommandHandlerAsync(Context); var newSkill = new SkillEditModel { Id = 2, Name = "New", Description = "Desc", OwningOrganizationId = 1 }; // Act var result = await handler.Handle(new SkillEditCommandAsync { 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 HttpUnauthorizedResult(); // Edge case of user having Org Admin claim but not Org Id claim var model = new SkillEditModel(); if(User.IsUserType(UserType.SiteAdmin)) { model.ParentSelection = await _bus.SendAsync(new SkillListQueryAsync()); model.OrganizationSelection = await _bus.SendAsync(new OrganizationSelectListQueryAsync()); } else { model.ParentSelection = await _bus.SendAsync(new SkillListQueryAsync { OrganizationId = organizationId.Value }); } return View("Edit", model); }
private static Mock<IMediator> MockMediatorSkillEditQuery(out SkillController controller, SkillEditModel model = null) { if (model == null) model = new SkillEditModel { Id = 1, Name = "Name", Description = "Description" }; var mockMediator = new Mock<IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny<SkillEditQueryAsync>())).Returns(() => Task.FromResult(model)).Verifiable(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny<SkillListQueryAsync>())).Returns(() => Task.FromResult(SummaryListItems())) .Verifiable(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny<OrganizationSelectListQueryAsync>())) .Returns(() => Task.FromResult<IEnumerable<SelectListItem>>(new List<SelectListItem> { new SelectListItem { Text = "Item 1", Value = "1" } })) .Verifiable(); controller = new SkillController(mockMediator.Object); return mockMediator; }
public async Task<IActionResult> Edit(SkillEditModel model) { if (ModelState.IsValid) { await _bus.SendAsync(new SkillEditCommandAsync { Skill = model }); return RedirectToAction("Index", new { area = "Admin" }); } if (User.IsUserType(UserType.SiteAdmin)) { model.ParentSelection = await _bus.SendAsync(new SkillListQueryAsync()); model.OrganizationSelection = await _bus.SendAsync(new OrganizationSelectListQueryAsync()); } else { var organizationId = User.GetOrganizationId(); if (!organizationId.HasValue) return new HttpUnauthorizedResult(); // Edge case of user having Org Admin claim but not Org Id claim model.ParentSelection = await _bus.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); }