private void Validate(ResponsibilityViewModel viewModel)
        {
            if (viewModel.CategoryId == null)
            {
                ModelState.AddModelError("CategoryId", "Please select a Category");
            }
            if (viewModel.SiteId == null)
            {
                ModelState.AddModelError("SiteId", "Please select a Site");
            }
            
            if (viewModel.HasMultipleFrequencies == false && viewModel.FrequencyId == null || viewModel.FrequencyId == default(int))
            {
                 ModelState.AddModelError("FrequencyId", "Please select a Frequency");
            }

            if (!viewModel.IsCreatorResponsibilityOwner && viewModel.OwnerId == null)
            {
                ModelState.AddModelError("OwnerId", "Please select a Responsibility Owner");
            }
        }
        private ActionResult ReturnInvalidEditResponsibilityViewResult(ResponsibilityViewModel responsibilityViewModel)
        {
            var viewModel = _responsibilityViewModelFactory
                         .WithCompanyId(responsibilityViewModel.CompanyId)
                         .WithResponsibilityId(responsibilityViewModel.ResponsibilityId)
                         .GetViewModel();

            viewModel.IsCreatorResponsibilityOwner = responsibilityViewModel.IsCreatorResponsibilityOwner;
            return View("Edit", viewModel);
        }
 private Guid? GetOwnerId(ResponsibilityViewModel viewModel)
 {
     if (!viewModel.IsCreatorResponsibilityOwner && viewModel.OwnerId != null)
     {
         return viewModel.OwnerId;
     }
     return null;
 }
        public ActionResult Update(ResponsibilityViewModel viewModel)
        {
            Validate(viewModel);

            if (!ModelState.IsValid)
            {
                return ReturnInvalidEditResponsibilityViewResult(viewModel);
            }

            try
            {
                var request = new SaveResponsibilityRequest
                {
                    CompanyId = CurrentUser.CompanyId,
                    ResponsibilityId = viewModel.ResponsibilityId,
                    ResponsibilityCategoryId = viewModel.CategoryId.HasValue ? viewModel.CategoryId.Value : default(long),
                    Title = viewModel.Title,
                    Description = viewModel.Description,
                    SiteId = viewModel.SiteId.HasValue ? viewModel.SiteId.Value : default(long),
                    ResponsibilityReasonId = viewModel.ReasonId.HasValue ? viewModel.ReasonId.Value : default(long),
                    OwnerId = GetOwnerId(viewModel),
                    TaskReoccurringType = viewModel.HasMultipleFrequencies && viewModel.FrequencyId == null ? TaskReoccurringType.None : (TaskReoccurringType)viewModel.FrequencyId,
                    UserId = CurrentUser.UserId
                };

                _responsibilitiesService.SaveResponsibility(request);

                return RedirectToEditAction(viewModel.ResponsibilityId, viewModel.CompanyId, null);
            }
            catch (ValidationException validationException)
            {
                ModelState.AddValidationErrors(validationException);
            }
            return ReturnInvalidCreateResponsibilityViewResult(viewModel);
        }
Beispiel #5
0
        public void When_post_update_valid_viewmodel_Then_should_call_correct_methods()
        {
            //Given
            var target = GetTarget();
            var viewModel = new ResponsibilityViewModel
                                {
                                    ResponsibilityId = 1L,
                                    CategoryId = default(int),
                                    Title = "R1",
                                    Description = "Responsibility1",
                                    SiteId = default(int),
                                    ReasonId = default(int),
                                    OwnerId = new Guid(),
                                    FrequencyId = (int) TaskReoccurringType.Annually
                                };

            //When
            var result = target.Update(viewModel) as ViewResult;

            //Then
            _responsibilitiesService
                .Verify(x => x.SaveResponsibility(It.IsAny<SaveResponsibilityRequest>()), Times.Once());
        }
Beispiel #6
0
        public void Given_MultipleFrequency_is_false_and_frequency_not_specfied_When_UpdateResponsibility_Then_validation_error()
        {
            //given
            var target = GetTarget();
            //when
            var viewModel = new ResponsibilityViewModel
            {
                CompanyId = 1L,
                ResponsibilityId = 1L,
                Title = "the title 12345",
                Description = "test description 12345",
                HasMultipleFrequencies = false,
                FrequencyId = null
            };

            target.Update(viewModel);

            //then the save responsibilty method is not called
            _responsibilitiesService.Verify(x => x.SaveResponsibility(It.IsAny<SaveResponsibilityRequest>()), Times.Never());
            Assert.IsFalse(target.ModelState.IsValid);
            Assert.True(target.ModelState.Keys.Contains("FrequencyId"), "FrequencyId is mandatory when not mulitple frequencies");


        }
Beispiel #7
0
        public void Give_IsRecurring_is_Multiple_When_UpdateResponsibility_Then_Update()
        {
            //given
            var target = GetTarget();
            //when
            var viewModel = new ResponsibilityViewModel
            {
                CompanyId = 1L,
                ResponsibilityId = 1L,
                Title = "the title 12345",
                Description = "test description 12345",
                HasMultipleFrequencies = true,
                CategoryId =  123,
                SiteId = 123123,
                OwnerId = Guid.NewGuid(),
                ReasonId = 43534
            };

            target.Update(viewModel);

            //then
            _responsibilitiesService.Verify(x => x.SaveResponsibility(It.IsAny<SaveResponsibilityRequest>()), Times.Once());
            
        }