public void ServiceFunctionService_Create_CallInsertsFunctionAndCallsSaveChanges()
        {
            #region Arrange

            var dateTimeNow = DateTime.Now;

            var serviceFunction = new ServiceFunction()
            {
                Id            = 5,
                FunctionType  = _serviceFunctionTypes.FirstOrDefault(x => x.Id == 1),
                ServiceDomain = new ServiceDomain {
                    Id = 1, ServiceDeskId = 1
                },
                InsertedBy   = Username,
                InsertedDate = dateTimeNow,
                UpdatedBy    = Username,
                UpdatedDate  = dateTimeNow,
            };

            #endregion

            #region Act

            var response = _serviceFunctionService.Create(serviceFunction);

            #endregion

            #region Assert

            _mockServiceFunctionRepository.Verify(x => x.Insert(It.IsAny <ServiceFunction>()), Times.Once());
            _mockUnitOfWork.Verify(x => x.Save(), Times.Exactly(1));

            Assert.IsNotNull(response);
            Assert.AreEqual(_serviceFunctions.Count, response);

            #endregion
        }
예제 #2
0
        public ActionResult CreateAjaxServiceAddFunctionGrid([DataSourceRequest] DataSourceRequest request,
                                                             [Bind(Prefix = "models")] IEnumerable <BulkServiceFunctionViewModel> serviceFunctions)
        {
            var inputFunctions  = serviceFunctions as IList <BulkServiceFunctionViewModel> ?? serviceFunctions.ToList();
            var outputFunctions = new List <BulkServiceFunctionViewModel>();

            if (ModelState.IsValid)
            {
                try
                {
                    var customerSpecificTypeThreshold = _parameterService.GetParameterByNameAndCache <int>(ParameterNames.CustomerSpecificTypeThreshold);

                    if (serviceFunctions != null && inputFunctions.Any())
                    {
                        var userName   = _contextManager.UserManager.Name;
                        var now        = DateTime.Now;
                        var customerId = _appUserContext.Current.CurrentCustomer.Id;

                        // Make sure all the of the specified Service Domains exist.
                        var checkServiceDomainIds = inputFunctions
                                                    .Where(w => w.ServiceDomainId.HasValue)
                                                    .Select(s => s.ServiceDomainId.Value)
                                                    .Distinct()
                                                    .ToList();
                        var serviceDomainIds = _serviceDomainService.GetByCustomer(customerId)
                                               .Where(w => checkServiceDomainIds.Contains(w.Id))
                                               .Select(s => s.Id)
                                               .ToList();

                        foreach (var inputFunction in inputFunctions)
                        {
                            if (!inputFunction.ServiceDomainId.HasValue ||
                                inputFunction.ServiceDomainId == 0 ||
                                inputFunction.FunctionTypeId == 0 ||
                                !serviceDomainIds.Contains(inputFunction.ServiceDomainId.Value))
                            {
                                continue;
                            }

                            var diagramDisplayOrder = inputFunction.DiagramOrder ?? 5;
                            if (diagramDisplayOrder < 1)
                            {
                                diagramDisplayOrder = 5;
                            }

                            var newServiceFunction = new ServiceFunction
                            {
                                AlternativeName = inputFunction.AlternativeName,
                                DiagramOrder    = diagramDisplayOrder,
                                FunctionTypeId  = inputFunction.FunctionTypeId,
                                ServiceDomainId = inputFunction.ServiceDomainId.Value,
                                InsertedBy      = userName,
                                InsertedDate    = now,
                                UpdatedBy       = userName,
                                UpdatedDate     = now
                            };

                            newServiceFunction.Id = _serviceFunctionService.Create(newServiceFunction);
                            outputFunctions.Add(Mapper.Map <BulkServiceFunctionViewModel>(newServiceFunction));

                            var functionType = _functionTypeRefDataService.GetById(inputFunction.FunctionTypeId);
                            if (!functionType.Visible && _functionTypeRefDataService.GetNumberOfFunctionTypeReferences(inputFunction.FunctionTypeId) >= customerSpecificTypeThreshold)
                            {
                                functionType.Visible = true;
                                _functionTypeRefDataService.Update(functionType);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _contextManager.ResponseManager.StatusCode = 500;
                    _contextManager.ResponseManager.AppendHeader(ModelStateErrorNames.ErrorMessage, ex.Message);
                }
            }

            return(Json(outputFunctions.ToDataSourceResult(request, ModelState)));
        }