public bool IsAddModelValid(AddArticleBindingModel model) { if (model.Name.Length < 3 || model.Name.Length > 100) { return(false); } if (model.Price < 0 || model.Price > 15000) { return(false); } if (model.Category == "None") { return(false); } if (CategoriesSemiModels .CategoriesInformation.GetAdditionalInformation(model.Category) != null && model.Type == "None") { return(false); } if (model.Brand == null) { return(false); } if (model.Description.Length < 10 || model.Description.Length > 1000) { return(false); } return(true); }
public AddArticleBindingModel GetArticleBindingModel() { AddArticleBindingModel model = new AddArticleBindingModel(); model.Categories = CategoriesInformation.Categories; return(model); }
public async Task <int> AddArticleAsync(AddArticleBindingModel model) { var checkForDuplicate = this.DbContext .Articles .FirstOrDefault(x => x.Title == model.Title); if (checkForDuplicate != null) { return(ErrorId); } model.Content = Html_String_Utility.EncodeThisPropertyForMe(model.Content); model.HighLightVideoURL = Html_String_Utility.EncodeThisPropertyForMe(model.HighLightVideoURL); model.PhotoURL = Html_String_Utility.EncodeThisPropertyForMe(model.PhotoURL); model.Title = Html_String_Utility.EncodeThisPropertyForMe(model.Title); var article = this.Mapper.Map <Article>(model); var articleCategory = this.DbContext .ArticleCategories .FirstOrDefault(x => x.CategoryName == model.ArticleCategoryName); if (articleCategory == null) { articleCategory = new ArticleCategory() { CategoryName = model.ArticleCategoryName }; await this.DbContext.ArticleCategories.AddAsync(articleCategory); await this.DbContext.SaveChangesAsync(); } article.ArticleCategoryId = articleCategory.Id; if (article.HighLightVideoURL.Contains(CommonConstants.OriginalVideoUrlPart)) { article.HighLightVideoURL = ModifyVideoURL_Embeded.ModifyEmbed(article.HighLightVideoURL); } await this.DbContext.Articles.AddAsync(article); await this.DbContext.SaveChangesAsync(); return(article.Id); }
public bool AddArticle(AddArticleBindingModel model, HttpPostedFileBase file) { if (!validation.IsAddModelValid(model)) { return(false); } if (file != null) { var img = new byte[file.ContentLength]; file.InputStream.Read(img, 0, file.ContentLength); if (!validation.IsImage(img)) { return(false); } model.Image = img; } else { return(false); } if (model.Type == null || model.Type == "") { model.Type = "none"; } model.UploadDate = DateTime.Now; var article = Mapper.Map <ArticleDataModel>(model); article.Discount = 0; context.Articles.Add(article); context.SaveChanges(); return(true); }
public async Task <IActionResult> AddArticle(AddArticleBindingModel model) { if (!this.ModelState.IsValid) { SetErrorMessage(CommonConstants.DangerMessage); return(this.View()); } int generatedId = await this.articleService.AddArticleAsync(model); if (generatedId < 1) { SetErrorMessage(CommonConstants.DuplicateMessage); return(this.View()); } SetSuccessMessage(string.Format(CommonConstants.SuccessMessage, CommonConstants.ArticleSuffix)); return(Redirect(string.Format(RedirectConstants.AdministratorAreaArticleDetailsPage, generatedId))); }
public ActionResult Add(AddArticleBindingModel model, HttpPostedFileBase file) { if (!ModelState.IsValid && file != null) { ViewBag.AccountType = service.UserIsInRole(User); ViewBag.Error = true; return(View(service.GetArticleBindingModel())); } else { bool result = service.AddArticle(model, file); if (result) { return(RedirectToAction("Index", "Home")); } else { ViewBag.AccountType = service.UserIsInRole(User); ViewBag.Error = true; return(View(service.GetArticleBindingModel())); } } }