private void Check_ArticleTitle_Valid(int titleLength) { string title = GetStringWithDesiredLength(titleLength); var validator = new ArticleValidator(); string[] errors; Assert.True(validator.Validate(Article.Create(new ArticleState { Title = title }), out errors)); }
private void Check_ArticleText_Valid(int textLength) { string text = GetStringWithDesiredLength(textLength); var validator = new ArticleValidator(); string[] errors; Assert.True(validator.Validate(Article.Create(new ArticleState { Text = text }), out errors)); }
public object Put(Guid id, [FromBody] ArticleModificationData value) { Article article; try { article = _articleService.GetByID(id); DataTransfer.ModifyArticle(article, value); var validationResults = new ArticleValidator().Validate(article); var errorString = JsonConvert.SerializeObject(validationResults.Errors); var jsonResult = Json(errorString); if (!validationResults.IsValid) { jsonResult.StatusCode = 400; } try { _articleService.Update(article); jsonResult.Value += JsonConvert.SerializeObject(article); } catch (Exception ex) { return(jsonResult); } return(jsonResult); } catch (Exception ex) { return(new StatusCodeResult(404)); } }
public object Post([FromBody] ArticleInsertionData data) { var article = new Article(); DataTransfer.ModifyArticle(article, data); var validationResults = new ArticleValidator().Validate(article); var errorString = JsonConvert.SerializeObject(validationResults.Errors); var jsonResult = Json(errorString); if (!validationResults.IsValid) { jsonResult.StatusCode = 400; } try { _articleService.Insert(article); jsonResult.Value += JsonConvert.SerializeObject(article); } catch (Exception ex) { return(jsonResult); } return(jsonResult); }
public async Task <Entities.Concrete.Article> AddArticle(ArticleAddForDto articleAddForDto) { var article = _mapper.Map <Article.Entities.Concrete.Article>(articleAddForDto); var articleValidator = new ArticleValidator(); var resutltValidator = articleValidator.Validate(article); if (resutltValidator.Errors.Count > 0) { throw new ValidationException(resutltValidator.Errors); } var addedArticle = await _articleDal.AddAsync(article); return(addedArticle); }
public ActionResult Create([Bind(Include = "Id, Name, Description, Price")]Article article) { ArticleValidator validator = new ArticleValidator(); ValidationResult result = validator.Validate(article); if (result.IsValid) { _articleService.InsertArticle(article); _articleService.Save(); return RedirectToAction("Index"); } else { foreach (ValidationFailure failer in result.Errors) { ModelState.AddModelError(failer.PropertyName, failer.ErrorMessage); } return View(article); } }
static void Main(string[] args) { UserRepo userRepo = new UserRepo(); ArticleRepo articleRepo = new ArticleRepo(); AuthorRepo authorRepo = new AuthorRepo(); UserValidator userValidator = new UserValidator(); ArticleValidator articleValidator = new ArticleValidator(); AuthorValidator authorValidator = new AuthorValidator(); userRepo.SetValidator(userValidator); articleRepo.SetValidator(articleValidator); authorRepo.SetValidator(authorValidator); Manager manager = new Manager(userRepo, authorRepo, articleRepo); ConsoleUI console = new ConsoleUI(manager); console.Run(); }
public ActionResult Edit([Bind(Include = "Id, Name, Description, Price")] Article article) { ArticleValidator validator = new ArticleValidator(); ValidationResult result = validator.Validate(article); if (result.IsValid) { _articleService.UpdateArticle(article); _articleService.Save(); return(RedirectToAction("Index")); } else { foreach (ValidationFailure failer in result.Errors) { ModelState.AddModelError(failer.PropertyName, failer.ErrorMessage); } return(View(article)); } }
public static void EnsureFileContentIsValid( int templateCode, ElementDescriptorType elementDescriptorType, IElementConstraints elementConstraints, Stream inputStream, string fileName) { switch (elementDescriptorType) { case ElementDescriptorType.BitmapImage: BitmapImageValidator.ValidateBitmapImage(templateCode, (BitmapImageElementConstraints)elementConstraints, inputStream); break; case ElementDescriptorType.VectorImage: var fileFormat = DetectFileFormat(fileName); VectorImageValidator.ValidateVectorImage(templateCode, fileFormat, (VectorImageElementConstraints)elementConstraints, inputStream); break; case ElementDescriptorType.Article: ArticleValidator.ValidateArticle(templateCode, inputStream); break; case ElementDescriptorType.CompositeBitmapImage: break; case ElementDescriptorType.ScalableBitmapImage: break; case ElementDescriptorType.PlainText: case ElementDescriptorType.FormattedText: case ElementDescriptorType.FasComment: case ElementDescriptorType.Link: case ElementDescriptorType.Phone: case ElementDescriptorType.VideoLink: case ElementDescriptorType.Color: throw new NotSupportedException($"Specified element descriptor type '{elementDescriptorType}' is non-binary"); default: throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type"); } }
public async Task <Entities.Concrete.Article> UpdateArticle(ArticleUpdateForDto articleUpdateForDto) { var article = await GetArticleById(articleUpdateForDto.Id); article.Title = articleUpdateForDto.Title; article.Content = articleUpdateForDto.Content; article.CategoryID = articleUpdateForDto.CategoryID; article.UpdatedDate = articleUpdateForDto.UpdatedDate; var articleValidator = new ArticleValidator(); var resutltValidator = articleValidator.Validate(article); if (resutltValidator.Errors.Count > 0) { throw new ValidationException(resutltValidator.Errors); } var updatedArticle = _articleDal.Update(article); return(updatedArticle); }
private Article CreateArticle(Document inDocument) { using var documentContext = this.ApiServiceContext.CreateApiDocumentContext(inDocument); var inArticle = documentContext.GetResource <Article>(); var apiArticleResource = inDocument.GetResource(); if (apiArticleResource?.Relationships != null) { // Author foreign key (required) if (apiArticleResource.Relationships.TryGetRelationship("author", out var apiArticleAuthorRelationship)) { if (apiArticleAuthorRelationship.IsToOneRelationship()) { var apiArticleAuthorToOneRelationship = (ToOneRelationship)apiArticleAuthorRelationship; var apiArticleAuthorId = Convert.ToInt64(apiArticleAuthorToOneRelationship.Data.Id); inArticle.AuthorId = apiArticleAuthorId; } } // Blog foreign key (required) if (apiArticleResource.Relationships.TryGetRelationship("blog", out var apiArticleBlogRelationship)) { if (apiArticleBlogRelationship.IsToOneRelationship()) { var apiArticleBlogToOneRelationship = (ToOneRelationship)apiArticleBlogRelationship; var apiArticleBlogId = Convert.ToInt64(apiArticleBlogToOneRelationship.Data.Id); inArticle.BlogId = apiArticleBlogId; } } } var validator = new ArticleValidator(); validator.ValidateAndThrow(inArticle); var outArticle = this.BloggingRepository.CreateArticle(inArticle); return(outArticle); }
private static string EnsureFileContentIsValid( int templateCode, string fileName, ElementDescriptorType elementDescriptorType, IElementConstraints elementConstraints, Stream inputStream) { switch (elementDescriptorType) { case ElementDescriptorType.BitmapImage: return(BitmapImageValidator.ValidateBitmapImage(templateCode, (BitmapImageElementConstraints)elementConstraints, inputStream)); case ElementDescriptorType.VectorImage: { var fileFormat = DetectFileFormat(fileName); VectorImageValidator.ValidateVectorImage(templateCode, fileFormat, (VectorImageElementConstraints)elementConstraints, inputStream); return(ContentTypesMap[fileFormat]); } case ElementDescriptorType.Article: ArticleValidator.ValidateArticle(templateCode, inputStream); return(ContentTypesMap[FileFormat.Chm]); case ElementDescriptorType.PlainText: case ElementDescriptorType.FormattedText: case ElementDescriptorType.FasComment: case ElementDescriptorType.Link: case ElementDescriptorType.Phone: case ElementDescriptorType.VideoLink: case ElementDescriptorType.Color: throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}"); default: throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type"); } }
public ArticleShould() { this.configuration = new ArticleValidator(); }
public static ValidationResult Validate(this Article article) { var result = new ArticleValidator().Validate(article); return(result); }