public void Given_valid_request_When_Save_New_Responsibility_Then_should_call_correct_methods()
 {
     //given
     var target = GetTarget();
     var request = new SaveResponsibilityRequest
                       {
                           ResponsibilityCategoryId = default(long),
                           Title = string.Empty,
                           Description = string.Empty,
                           SiteId = default(long),
                           ResponsibilityReasonId = default(long),
                           OwnerId = new Guid(),
                           TaskReoccurringType = TaskReoccurringType.Weekly,
                       };
     //when
     target.SaveResponsibility(request);
     //then
     _responsibilityRepository.Verify(x=>x.SaveOrUpdate(It.IsAny<Responsibility>()),Times.Once());
 }
        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);
        }
 private Employee GetResponsibilityOwner(SaveResponsibilityRequest request, UserForAuditing user)
 {
     Guid id = request.OwnerId == null ? user.Employee.Id : request.OwnerId.Value;
     return _employeeRepository.GetByIdAndCompanyId(id, request.CompanyId);
 }
        private Responsibility GetResponsibility(SaveResponsibilityRequest request, ResponsibilityReason reason, Employee owner, UserForAuditing user, Site site, ResponsibilityCategory category)
        {
            Responsibility responsibility;

            if (request.ResponsibilityId != default(long))
            {
                responsibility = _responsibilityRepository.GetById(request.ResponsibilityId);

                responsibility.Update(
                    request.CompanyId,
                    category,
                    request.Title,
                    request.Description,
                    site,
                    reason,
                    owner,
                    request.TaskReoccurringType,
                    user
                    );
            }
            else
            {
                responsibility = Responsibility.Create(
                    request.CompanyId,
                    category,
                    request.Title,
                    request.Description,
                    site,
                    reason,
                    owner,
                    request.TaskReoccurringType, null,
                    user
                    );
            }
            return responsibility;
        }
        public long SaveResponsibility(SaveResponsibilityRequest request)
        {
            _log.Add(request);
            long result;
            try
            {
                var category = _responsibilityCategoryRepository.GetById(request.ResponsibilityCategoryId);
                var site = _siteRepository.GetById(request.SiteId);
                var reason = _responsibilityReasonRepository.GetById(request.ResponsibilityReasonId);
                var user = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);
                var owner = GetResponsibilityOwner(request, user);

                var responsibility = GetResponsibility(request, reason, owner, user, site, category);

                _responsibilityRepository.SaveOrUpdate(responsibility);

                result = responsibility.Id;
            }
            catch (Exception ex)
            {
                _log.Add(ex);
                throw;
            }
            return result;
        }