public SupplierService( IUserContext applicationContext, ISupplierRepository supplierRepository, IQueueDispatcher <IMessage> dispatcher) : base(applicationContext, dispatcher) { _supplierRepository = supplierRepository; _supplierValidator = new SupplierValidator(supplierRepository); }
public (bool isValid, IEnumerable <ValidationResult> errors) Validate() { var validator = new SupplierValidator(); 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 async Task <IActionResult> CalculateOrder([FromQuery] OrderModel model) { var validator = new SupplierValidator(); var result = await validator.ValidateAsync(model); if (!result.IsValid) { var error = string.Join(';', result.Errors); throw new Exception(error); } var order = await _service.CalculateOrder(model); return(Ok(order)); }
public ServiceResult Save(SaveSupplierMessage message) { Supplier supplier; CrudOperation operation; if (message.ID.HasValidIdValue()) { supplier = Repository.GetById(message.ID); operation = CrudOperation.Update; if (supplier == null) { return(new ServiceResult().SetInvalid().WithMessage(ValidationResources.Supplier_NotFound)); } Mapper.Map(message, supplier); } else { supplier = Mapper.Map <Supplier>(message); operation = CrudOperation.Create; } var validation = new SupplierValidator().Execute(supplier); if (!validation.IsValid) { return(Mapper.Map <ServiceResult>(validation)); } switch (operation) { case CrudOperation.Create: Repository.Create(supplier); break; case CrudOperation.Update: Repository.Update(supplier); break; } return(supplier.AsServiceResultWithID().SetValid()); }
private async Task ValidateSupplier(Dal.Entities.Supplier supplier) { var supplierValidator = new SupplierValidator(); var validateResult = await supplierValidator.ValidateAsync(supplier); if (validateResult.IsValid) { return; } throw new ServiceException(validateResult.Errors.Select(e => new ErrorMessage() { Code = ErrorCodes.Model_Validation_Error_Code, Meta = new { e.ErrorCode, e.ErrorMessage, e.PropertyName }, Message = e.ErrorMessage }).ToArray()); }
public void SaveData() { SupplierModel currentData = new SupplierModel(); currentData.Name = Name; currentData.Address = Address; currentData.ContactNumber = ContactNumber; SupplierValidator validator = new SupplierValidator(); ValidationResult result = validator.Validate(currentData); if (result.IsValid == false) { string errorMessage = (String.Join(Environment.NewLine + " • ", result.Errors.Select(error => error.ErrorMessage))); universalHelper.MessageDialog("Saving of data failed!", " • " + errorMessage); return; } else { helper.SaveItem(currentData); ClearFields(); } }
public SupplierValidatorTest() { _validator = new SupplierValidator(); }
public SupplierBll(ILog log) { _log = log; _validator = new SupplierValidator(); }
/// <summary> /// James Heim /// Created: 2019/01/24 /// /// Create a Supplier from data provided via the user input. /// </summary> /// <remarks> /// James Heim /// Updated: 2019/01/30 /// Moved created Supplier to a varible the button handler can access. /// </remarks> private void CreateSupplier() { // Load inputs. string supplierName = txtSupplierName.Text.Trim(); string streetAddress = txtStreetAddress.Text.Trim(); string city = txtCity.Text.Trim(); string state = txtState.Text.Trim(); string zip = txtZip.Text.Trim(); string country = txtCountry.Text.Trim(); string contactFirstName = txtContactFirstName.Text.Trim(); string contactLastName = txtContactLastName.Text.Trim(); string phoneNumber = txtPhoneNumber.Text.Trim(); string email = txtEmail.Text.Trim(); string description = txtDescription.Text.Trim(); // Check for empty inputs. if (supplierName.Length == 0 || streetAddress.Length == 0 || city.Length == 0 || state.Length == 0 || zip.Length == 0 || country.Length == 0 || contactFirstName.Length == 0 || contactLastName.Length == 0 || phoneNumber.Length == 0 || email.Length == 0) { throw new ApplicationException("Fill out all data."); } // Phone number validation. else if (!Regex.IsMatch(phoneNumber, @"^\(?\d{3}\)?[\s\-]?\d{3}\-?\d{4}$")) { txtPhoneNumber.Select(0, txtPhoneNumber.Text.Length); txtPhoneNumber.Focus(); throw new ApplicationException("Please enter a valid phone number."); } // Email validation. else if (!Regex.IsMatch(txtEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")) { txtEmail.Select(0, txtEmail.Text.Length); txtEmail.Focus(); throw new ApplicationException("Enter a valid email."); } else { SupplierValidator.ValidateEmail(new Supplier() { Email = email }); SupplierValidator.ValidatePhoneNumber(new Supplier() { PhoneNumber = phoneNumber }); // Create the Supplier. try { _newSupplier = new Supplier { Name = supplierName, Address = streetAddress, City = city, State = state, PostalCode = zip, Country = country, ContactFirstName = contactFirstName, ContactLastName = contactLastName, PhoneNumber = phoneNumber, Email = email, Description = description }; } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.InnerException, "Error Creating Supplier"); } } }