예제 #1
0
        public ServiceResult <Service> Post([FromBody] Service model)
        {
            ServiceResult <Service> result = null;
            var validatorResult            = validator.Validate(model);

            if (validatorResult.IsValid)
            {
                try
                {
                    result = this.appService.Insert(model);
                }
                catch (Exception ex)
                {
                    result         = new ServiceResult <Service>();
                    result.Errors  = new string[] { ex.Message };
                    result.Success = false;
                }
            }
            else
            {
                result         = new ServiceResult <Service>();
                result.Errors  = validatorResult.GetErrors();
                result.Success = false;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        ///Post service base.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="attachProposedChannels"></param>
        /// <returns></returns>
        protected IActionResult Post(IVmOpenApiServiceInVersionBase request, bool attachProposedChannels = false)
        {
            if (request == null)
            {
                ModelState.AddModelError("RequestIsNull", CoreMessages.OpenApi.RequestIsNull);
                return(new BadRequestObjectResult(ModelState));
            }
            // Validate the items
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            // Get the base model for service
            request = request.VersionBase();

            // Check the item values from db and validate
            if (request.PublishingStatus != PublishingStatus.Published.ToString())
            {
                request.PublishingStatus = PublishingStatus.Draft.ToString();
            }

            ServiceValidator service = new ServiceValidator(request, generalDescriptionService, codeService, fintoService, commonService, channelService, request.AvailableLanguages, UserRole());

            service.Validate(this.ModelState);

            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var result = serviceService.AddService(request, Settings.AllowAnonymous, versionNumber, attachProposedChannels);

            return(Ok(result));
        }
예제 #3
0
        bool OnCanSave(object obj)
        {
            var name          = Name;
            var laborCost     = LaborCost;
            var taxPercentage = TaxPercentage;

            return(ServiceValidator.Validate(name, laborCost, taxPercentage));
        }
예제 #4
0
        private bool ValidateDataService()
        {
            ServiceValidator serviceValidator = new ServiceValidator();

            FluentValidation.Results.ValidationResult dataValidationResult = serviceValidator.Validate(Service);
            IList <ValidationFailure> validationFailures = dataValidationResult.Errors;
            UserFeedback userFeedback = new UserFeedback(FormGrid, validationFailures);

            userFeedback.ShowFeedback();
            return(dataValidationResult.IsValid && ValidateCity() && ValidateState());
        }
예제 #5
0
        public override bool Validate()
        {
            if (ServiceValidator.isNotNull())
            {
                var result = ServiceValidator.Validate(Owner);
                if (result.IsValid.isFalse())
                {
                    Debug.WriteLine($"service : {Owner.GetType().Name}");
                    Debug.WriteLine($"error : {result.Errors.First().ErrorMessage}");
                    return(false);
                }
            }

            return(true);
        }
예제 #6
0
        public void TestServiceValidation()
        {
            IValidator <Service> validator = new ServiceValidator();

            Service validSrv = new Service()
            {
                ID       = 1,
                Name     = "washing",
                Price    = 10,
                Duration = 20,
                Medicine = "shampoo"
            };

            Service invalidSrv = new Service()
            {
                ID       = 1,
                Name     = "",
                Price    = -10,
                Duration = -20,
                Medicine = ""
            };

            //validating a valid service
            validator.Validate(validSrv);

            //trying to validate a service with invalid name, price, duration and medicie
            try
            {
                validator.Validate(invalidSrv);
                Assert.Fail();
            }
            catch (ValidationException ve)
            {
                Assert.AreEqual(ve.Message, "Invalid name!\nInvalid price!\nInvalid duration!\nInvalid medicine!\n");
            }
        }
예제 #7
0
        public void OnlyPublishingStatusDefined()
        {
            // Arrange & act
            var validator = new ServiceValidator(new VmOpenApiServiceInVersionBase()
            {
                PublishingStatus = PublishingStatus.Published.ToString()
            },
                                                 generalDescriptionService, codeService, fintoService, commonService,
                                                 channelService, null, UserRoleEnum.Eeva);

            // Act
            validator.Validate(controller.ModelState);

            // Assert
            controller.ModelState.IsValid.Should().BeTrue();
        }
예제 #8
0
        [InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid
        public void GeneralDescriptionSet_NotValid(string gdId)
        {
            // Arrange & act
            var vm = new VmOpenApiServiceInVersionBase()
            {
                PublishingStatus = PublishingStatus.Published.ToString(),
                StatutoryServiceGeneralDescriptionId = gdId
            };
            var validator = new ServiceValidator(vm, generalDescriptionService, codeService, fintoService, commonService,
                                                 channelService, null, UserRoleEnum.Eeva);

            // Act
            validator.Validate(controller.ModelState);

            // Assert
            controller.ModelState.IsValid.Should().BeFalse();
        }
예제 #9
0
        public void GeneralDescriptionSet_NotExists()
        {
            // Arrange & act
            var gdId = Guid.NewGuid();
            var vm   = new VmOpenApiServiceInVersionBase()
            {
                PublishingStatus = PublishingStatus.Published.ToString(),
                StatutoryServiceGeneralDescriptionId = gdId.ToString()
            };

            generalDescriptionServiceMockSetup.Setup(s => s.GetGeneralDescriptionVersionBase(gdId, 0, true)).Returns((VmOpenApiGeneralDescriptionVersionBase)null);
            var validator = new ServiceValidator(vm, generalDescriptionService, codeService, fintoService, commonService,
                                                 channelService, null, UserRoleEnum.Eeva);

            // Act
            validator.Validate(controller.ModelState);

            // Assert
            controller.ModelState.IsValid.Should().BeFalse();
        }
예제 #10
0
        /// <summary>
        /// Put service base.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="id"></param>
        /// <param name="sourceId"></param>
        /// <param name="attachProposedChannels"></param>
        /// <returns></returns>
        protected IActionResult Put(IVmOpenApiServiceInVersionBase request, string id = null, string sourceId = null, bool attachProposedChannels = false)
        {
            if (request == null)
            {
                ModelState.AddModelError("RequestIsNull", CoreMessages.OpenApi.RequestIsNull);
                return(new BadRequestObjectResult(ModelState));
            }

            if (id.IsNullOrEmpty() && sourceId.IsNullOrEmpty())
            {
                return(NotFound(new VmError()
                {
                    ErrorMessage = $"Service with id '{id}' not found."
                }));
            }

            // get the base model for service
            request = request.VersionBase();

            // Validate the items
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            IVmOpenApiServiceVersionBase currentVersion = null;

            if (!string.IsNullOrEmpty(id))
            {
                Guid?serviceId = id.ParseToGuid();

                // check that service exists
                if (!serviceId.HasValue)
                {
                    return(NotFound(new VmError()
                    {
                        ErrorMessage = $"Service with id '{id}' not found."
                    }));
                }

                request.Id     = serviceId;
                currentVersion = serviceService.GetServiceByIdSimple(request.Id.Value, false);
            }
            else if (!string.IsNullOrEmpty(sourceId))
            {
                currentVersion = serviceService.GetServiceBySource(sourceId);
            }

            // Check current version and data
            if (currentVersion == null || string.IsNullOrEmpty(currentVersion.PublishingStatus))
            {
                if (request.Id.IsAssigned())
                {
                    return(NotFound(new VmError()
                    {
                        ErrorMessage = $"Service with id '{request.Id.Value}' not found."
                    }));
                }
                else
                {
                    return(NotFound(new VmError()
                    {
                        ErrorMessage = $"Version for service with source id '{sourceId}' not found."
                    }));
                }
            }

            // Has user rights for the service
            var isOwnOrganization = ((VmOpenApiServiceVersionBase)currentVersion).Security == null ? false : ((VmOpenApiServiceVersionBase)currentVersion).Security.IsOwnOrganization;

            if (UserRole() != UserRoleEnum.Eeva && !isOwnOrganization)
            {
                return(NotFound(new VmError()
                {
                    ErrorMessage = $"User has no rights to update or create this entity!"
                }));
            }

            request.CurrentPublishingStatus = currentVersion.PublishingStatus;
            // Get the available languages from current version
            // Check if user has added new language versions. New available languages and data need to be validated (required fields need to exist in request).
            var newLanguages = request.AvailableLanguages.Where(i => !currentVersion.AvailableLanguages.Contains(i)).ToList();

            // Check the general description data. If current version is attached into general description, service type cannot be updated for service.
            // Except if deleteStatutoryServiceGeneralDescriptionId is true (general description will be removed from the service).
            if (currentVersion.StatutoryServiceGeneralDescriptionId.IsAssigned() && !request.DeleteStatutoryServiceGeneralDescriptionId)
            {
                request.Type = null;
            }

            ServiceValidator service = new ServiceValidator(request, generalDescriptionService, codeService, fintoService, commonService, channelService, newLanguages, UserRole());

            service.Validate(this.ModelState);

            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            return(Ok(serviceService.SaveService(request, Settings.AllowAnonymous, versionNumber, attachProposedChannels, sourceId)));
        }