Ejemplo n.º 1
0
 public string Add(Customer entity)
 {
     result = validator.Validate(entity);
     if (result.IsValid)
     {
         customerDal.Add(entity);
         JObject jsonObject = new JObject();
         jsonObject.Add("Status", "success");
         jsonObject.Add("ErrorMessage", "Kayıt başarıyla oluşturuldu");
         JArray array = new JArray();
         array.Add(jsonObject);
         return(JsonConvert.SerializeObject(array));
     }
     else
     {
         return(JsonConvert.SerializeObject(result.Errors));
     }
 }
        public (bool isValid, IEnumerable <ValidationResult> errors) Validate()
        {
            var validator = new CustomerValidation();
            var result    = validator.Validate(this);

            if (result.IsValid)
            {
                return(true, null);
            }
            return(false, result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })));
        }
        public void Customer_Validate_isNotValid()
        {
            // Arrange
            var expectedIsValid = false;
            var customer        = new CustomerViewModel()
            {
                CustomerID = Guid.NewGuid(),
                Name       = "Tiago",
                Email      = "tiago_tanphotmail.com",
            };

            // Act
            var customerValidation = new CustomerValidation();
            var validationResult   = customerValidation.Validate(customer);

            // Assert
            Assert.Equal(expectedIsValid, validationResult.IsValid);
        }
Ejemplo n.º 4
0
        public HttpResponseMessage AddCustomers(Customer customer)
        {
            CustomerValidation validations = new CustomerValidation();
            var result = validations.Validate(customer);

            if (!result.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            try
            {
                service.AddCustomer(customer);
                return(Request.CreateResponse(HttpStatusCode.Created));
            }
            catch (Exception)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }