public string ValidateCompany(string companyJson) { var newCompany = JsonConvert.DeserializeObject <CompanyEntity>(companyJson); var companyValidation = CompanyValidator.Validate(newCompany, Repository.Repository.GetAllCompanies()); return(JsonConvert.SerializeObject(companyValidation)); }
public ActionResult UpdateCompany(CompanyModel data) { if (data == null) { return(Jsend(JsendResult <List <ValidationFailure> > .Error("Company data can't be null"))); } var validator = new CompanyValidator(); ValidationResult validateResult = validator.Validate(data); if (validateResult.IsValid) { try { var result = RequestHelper.MakePostWebRequest <CompanyModel, Jsend <CompanyModel> >( $"{apiDomain}/api/Company", data, "PUT"); return(Jsend(result)); } catch (WebException ex) { Console.WriteLine(ex); return(Jsend(JsendResult.Error("Add Company occured error"))); } } List <ValidationFailure> failures = validateResult.Errors.ToList(); return(Jsend(JsendResult <List <ValidationFailure> > .Fail(failures))); }
static void Main(string[] args) { //定义待校验的实体 Company company = new Company(); company.ID = 1; company.CompanyName = "测试企业名称"; company.CompanyCode = "9988065487"; company.Email = "*****@*****.**"; company.Start = DateTime.Now.AddDays(-1); company.End = DateTime.Now; //定义不正确的信息,查看校验效果 company.TaxNumber = "3102055546874545"; company.SocialNumber = "3102055546874545"; //定义校验器 CompanyValidator validator = new CompanyValidator(); var validateResult = validator.Validate(company); //检查校验结果 if (validateResult.IsValid) { Console.WriteLine("校验通过"); } else { foreach (var error in validateResult.Errors) { Console.WriteLine(error.ErrorMessage); } } Console.Read(); }
public string CreateCompany([FromBody] JObject companyJObject) { var message = string.Empty; if (!CompanyValidator.Validate(companyJObject, out message)) { return(message); } string isin = companyJObject["Isin"].ToObject <string>(); // return if Isin already exists if (_companyService.GetCompanyByIsin(isin) != null) { return("ISIN already exists"); } string name = companyJObject["Name"].ToObject <string>(); string stock = companyJObject["StockTicker"].ToObject <string>(); string exchange = companyJObject["Exchange"].ToObject <string>(); string website = companyJObject["Website"].ToObject <string>() ?? string.Empty; var company = new Company(isin, name, stock, exchange, website); _companyService.Create(company); return($"{company.Isin}: Successfully created"); }
public Jsend <List <ValidationFailure> > InsertUpdateCompany(CompanyModel data) { if (data == null) { return(JsendResult <List <ValidationFailure> > .Error("Company data can't be null")); } var validator = new CompanyValidator(); ValidationResult validateResult = validator.Validate(data); if (validateResult.IsValid) { try { var result = _uow.CompanyTRepository.FindByID(data.CompanyID); if (result == null) { _uow.CompanyTRepository.Add( new CompanyT { CompanyName = data.CompanyName, CompanyCode = data.CompanyCode, TaxID = data.TaxID, Phone = data.Phone, Owner = data.Owner, WebsiteURL = data.WebsiteURL, Address = data.Address }); } else { _uow.CompanyTRepository.Update( new CompanyT { CompanyID = data.CompanyID, CompanyName = data.CompanyName, CompanyCode = data.CompanyCode, TaxID = data.TaxID, Phone = data.Phone, Owner = data.Owner, WebsiteURL = data.WebsiteURL, Address = data.Address }); } _uow.Commit(); } catch (SqlException ex) { _logger.Error(ex); return(JsendResult <List <ValidationFailure> > .Error("Queay data occured error")); } return(JsendResult <List <ValidationFailure> > .Success()); } List <ValidationFailure> failures = validateResult.Errors.ToList(); return(JsendResult <List <ValidationFailure> > .Fail(failures)); }
public void ShouldFailValidationIfExchangeNullOrEmptyOrWhiteSpace() { var validator = new CompanyValidator(); var company = GetCompanyToValidate(); company.Exchange = null; var result = validator.Validate(company); Assert.False(result.IsValid); company.Exchange = ""; result = validator.Validate(company); Assert.False(result.IsValid); company.Exchange = " "; result = validator.Validate(company); Assert.False(result.IsValid); }
public void ShouldPassValidation() { var validator = new CompanyValidator(); var company = GetCompanyToValidate(); var result = validator.Validate(company); Assert.True(result.IsValid); }
public void ShouldFailValidationIfWebsiteIsNotValidUrl() { var validator = new CompanyValidator(); var company = GetCompanyToValidate(); company.Website = "invalid"; var result = validator.Validate(company); Assert.False(result.IsValid); }
public void ShouldFailValidationIfInvalidIsin() { var validator = new CompanyValidator(); var company = GetCompanyToValidate(); company.Isin = "invalid"; var result = validator.Validate(company); Assert.False(result.IsValid); }
public Jsend <List <ValidationFailure> > AddCompany(CompanyModel data) { if (data == null) { return(JsendResult <List <ValidationFailure> > .Error("Company data can't be null")); } var checkNameUnique = _uow.CompanyTRepository.FindByName(data.CompanyName); if (checkNameUnique != null) { return(JsendResult <List <ValidationFailure> > .Error("CompanyName has already had")); } var validator = new CompanyValidator(); ValidationResult validateResult = validator.Validate(data); if (validateResult.IsValid) { try { _uow.CompanyTRepository.Add( new CompanyT { CompanyName = data.CompanyName, CompanyCode = data.CompanyCode, TaxID = data.TaxID, Phone = data.Phone, Owner = data.Owner, WebsiteURL = data.WebsiteURL, Address = data.Address }); _uow.Commit(); } catch (SqlException ex) { _logger.Error(ex); return(JsendResult <List <ValidationFailure> > .Error("Insert data error")); } return(JsendResult <List <ValidationFailure> > .Success()); } List <ValidationFailure> failures = validateResult.Errors.ToList(); return(JsendResult <List <ValidationFailure> > .Fail(failures)); }
public ActionResult Create(CreateCompanyVM model) { var validator = new CompanyValidator(companyRepository); var result = validator.Validate(model.Company); if (result.IsValid) { var comp = companyRepository.InsertCompany(model.Company); companyRepository.SaveChanges(); return(RedirectToAction("Show", "Company", new { comp.Id })); } else { model.Errors = result.Errors.ToDictionary(e => e.PropertyName, e => string.Format(e.ErrorMessage, e.FormattedMessagePlaceholderValues["PropertyName"])); return(View(model)); } }
public void Test_Validate_returnsFlase_IfNullObjectWasPassed() { // Arrange string message; JObject CompanyObject = new JObject(); CompanyObject.Add("Isin", "R1234"); CompanyObject.Add("Name", "Res=nault inc"); CompanyObject.Add("StockTicker", null); CompanyObject.Add("Exchange", "cars"); CompanyObject.Add("Website", "www.renault.com"); //Act var isValid = CompanyValidator.Validate(CompanyObject, out message); //Assert Assert.IsFalse(isValid, nameof(isValid)); Assert.AreEqual(CompanyValidator.IsinErrorMessage, message, nameof(message)); }
internal Result SuspendCompany(string externalId, SuspendCustomerParams data) { var loggerManager = new LoggerManager(); var operationGuid = Guid.NewGuid().ToString(); try { loggerManager.InsertLogoRecord(nameof(SuspendCompany), nameof(LogLevel.Info), null, data.TransactionId, JsonConvert.SerializeObject(data)); var orderDemandManager = new OrderDemandManager(); var suspendCompany = new SuspendCompanyData { ExternalId = externalId, OrderDemandGuid = operationGuid }; var validator = new CompanyValidator(); var valResults = validator.Validate(suspendCompany); var validationSucceeded = valResults.IsValid; if (!validationSucceeded) { var failures = valResults.Errors; var message = failures.Aggregate(string.Empty, (current, failure) => current + (failure.ErrorMessage + "<br />")); return(new Result { IsCompleted = false, Success = false, Message = message }); } orderDemandManager.SaveOrderDemand(null, operationGuid, 0, (int)ProvisionType.SuspendCustomer, (int)OrderDemandStates.Created, (int)OrderDemandType.Integrated, JsonConvert.SerializeObject(suspendCompany), data.TransactionId); return(new Result { IsCompleted = false, Success = true }); } catch (Exception ex) { loggerManager.InsertLogoRecord(nameof(SuspendCompany), nameof(LogLevel.Error), ex.Message + " " + ex.StackTrace, operationGuid, JsonConvert.SerializeObject(data)); return(new Result { IsCompleted = true, Success = false, Message = ex.Message }); } }
public async Task <Result> InsertCompany(Company model) { var validator = new CompanyValidator(true); var result = validator.Validate(model).GetResult(); if (!result.Success) { return(result); } var entity = MapCompanyModelToEntity(model); await _context.Company.AddAsync(entity); await _context.SaveChangesAsync(); model.Id = entity.Id; result.Tag = model; return(result); }
public IActionResult Update(string Isin, Company companyIn) { var message = string.Empty; if (!CompanyValidator.Validate(JObject.FromObject(companyIn), out message)) { return(BadRequest(message)); } var company = _companyService.GetCompanyByIsin(Isin); if (company == null) { return(NotFound()); } _companyService.Update(Isin, companyIn); return(Accepted()); }
public IActionResult Edit(CompanyDto company) { var validator = new CompanyValidator(companyRepository); var result = validator.Validate(company); if (result.IsValid) { companyRepository.UpdateCompany(company); companyRepository.SaveChanges(); return(RedirectToAction("Show", "Company", new { company.Id })); } else { var errors = result.Errors.ToDictionary(e => e.PropertyName, e => string.Format(e.ErrorMessage, e.FormattedMessagePlaceholderValues["PropertyName"])); EditCompanyVM model = new EditCompanyVM() { Company = company, Errors = errors }; return(View(model)); } }
public async Task <Result> UpdateCompany(Company model) { var validator = new CompanyValidator(false); var result = validator.Validate(model).GetResult(); if (!result.Success) { return(result); } var entity = await _context.Company.FindAsync(model.Id); if (entity == null) { return(new Result()); } entity = MapCompanyModelToEntity(model, entity); await _context.SaveChangesAsync(); return(result); }