public async Task UpdateAsyncShouldUpdateCorrectArticleById() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new ApplicationDbContext(optionsBuilder.Options); var articleService = new ArticleService(dbContext, null); var firstArticle = new Article { Title = "testTitle", Id = 1, ImageUrl = "testImageUrl", Content = "testContent", }; var editArticle = new EditArticleInputModel { Content = "editedContent", Title = "editedTitle", }; await dbContext.AddAsync(firstArticle); await dbContext.SaveChangesAsync(); await articleService.UpdateAsync(1, editArticle); var result = dbContext.Articles.FirstOrDefault(); Assert.NotNull(result); Assert.Equal("editedTitle", result.Title); Assert.Equal("editedContent", result.Content); Assert.Equal("testImageUrl", result.ImageUrl); }
public async Task <bool> EditAsync(EditArticleInputModel model) { var article = this._dbContext.Articles .FirstOrDefault(x => x.Id == model.Id); if (article == null) { return(false); } try { article.Title = model.Title; article.TitleEn = model.TitleEn; article.Content = model.Content; article.ContentEn = model.ContentEn; await this._dbContext.SaveChangesAsync(); return(true); } catch { return(false); } }
public async Task <IActionResult> Edit(EditArticleInputModel model) { var user = await this.userManager.GetUserAsync(this.User); if (!await this.blogPostsService.ExistsAsync(model.Id, user.Id)) { return(this.RedirectToAction("NotOwner", "NewsFeed")); } if (!this.ModelState.IsValid) { return(this.View(model)); } var sanitizer = new HtmlSanitizer(); var content = sanitizer.Sanitize(model.Content); var newUpdatedPost = new Article { Id = model.Id, Title = model.Title, Content = content, }; await this.blogPostsService.UpdateAsync(newUpdatedPost); return(this.RedirectToAction("ById", "Articles", new { id = model.Id })); }
public async Task <IActionResult> ChangePicture(EditArticleInputModel model) { if (model.Id == null || model.Picture == null) { return(this.RedirectToAction("Edit", new { id = model.Id })); } var currentPicture = _pictureService.GetArticlePicturesById(model.Id); var entityType = typeof(Article); var articlePictureId = string.Format(GlobalConstants.ArticlePicture, model.Title); //Delete the old picture from cloudinary _pictureService.DeletePicture(entityType, currentPicture.Id); //Delete old picture from DB DeletePictureFromDB(currentPicture); //Add new picture to Cloudinary var pictures = new List <IFormFile>(); pictures.Add(model.Picture); await this._pictureService.UploadPicturesAsync(pictures, entityType, articlePictureId, model.Id); return(Redirect("/")); }
public async Task <IActionResult> Edit(int id) { EditArticleInputModel inputModel = (await this.articleService.GetArticleAsync(id)).To <EditArticleInputModel>(); return(this.View(inputModel)); }
public async Task UpdateAsync(int id, EditArticleInputModel input) { var article = this.articlesRepository.All().FirstOrDefault(x => x.Id == id); article.Name = input.Name; article.Description = input.Description; article.ArticlesCategoryId = input.ArticlesCategoryId; await this.articlesRepository.SaveChangesAsync(); }
public async Task <IActionResult> Edit(int id, EditArticleInputModel article) { if (!this.ModelState.IsValid) { return(this.View()); } await this.articleService.UpdateAsync(id, article); return(this.RedirectToAction(nameof(this.Article), new { id })); }
public async Task <IActionResult> Edit(int id, EditArticleInputModel input) { if (!this.ModelState.IsValid) { input.ArticlesCategories = this.articlesCategoriesService.GetAll <ArticlesCategoryDropDowwViewModel>(); return(this.View(input)); } await this.articlesService.UpdateAsync(id, input); return(this.RedirectToAction(nameof(this.ById), new { id })); }
public IActionResult Edit(int id) { var article = this.articleService.GetDetails(id); var model = new EditArticleInputModel { Id = article.Id, Content = article.Content, Title = article.Title, }; return(this.View(model)); }
public void Edit(EditArticleInputModel model, string imageUploadResult) { var article = this._dbContext.Articles .FirstOrDefault(a => a.Id == model.Id); article.Title = model.Title; article.Content = model.Content; article.Category = model.Category; article.ArticleVersionPicture = imageUploadResult; this._dbContext.SaveChanges(); }
public ActionResult Edit(EditArticleInputModel model) { if (!ModelState.IsValid) { return(View()); } articleService.EditArticle(model.Id, model.Title, model.Content); TempData["success"] = SuccessMessages.ArticleEditSuccess; return(RedirectToAction("All")); }
public async Task <IActionResult> EditArticle(EditArticleInputModel model) { var sanitizer = new HtmlSanitizer(); var content = sanitizer.Sanitize(model.Content); var newUpdatedPost = new Article { Id = model.Id, Title = model.Title, Content = content, }; await this.administratorService.UpdateArticleAsync(newUpdatedPost); return(this.RedirectToAction(nameof(this.Articles))); }
public async Task UpdateAsync(int id, EditArticleInputModel input) { var article = this.db.Articles.FirstOrDefault(x => x.Id == id); article.Content = input.Content; article.Title = input.Title; string imageUrl = article.ImageUrl; if (input.PictureFile != null) { imageUrl = await this.cloudinaryService.UploudAsync(input.PictureFile); } article.ImageUrl = imageUrl; await this.db.SaveChangesAsync(); }
public async Task <IActionResult> Edit(EditArticleInputModel inputModel) { if (!ModelState.IsValid) { return(this.View(inputModel)); } ArticleServiceModel articleServiceModel = inputModel.To <ArticleServiceModel>(); if (inputModel.ImageFormFile != null) { string imageUrl = await this.cloudinaryService .UploadPictureAsync(inputModel.ImageFormFile, inputModel.Title); await this.imageService.EditWithArticleAsync(imageUrl, articleServiceModel.Id); } await this.articleService.EditArticleAsync(articleServiceModel); return(this.RedirectToAction("All", "Articles")); }
public async Task <IActionResult> EditAsync(EditArticleInputModel model) { if (model.Id == null) { TempData[GlobalConstants.TempDataErrorMessageKey] = GlobalConstants.InvalidId; return(Redirect("/")); } var isSuccess = await this._articleService .EditAsync(model); if (isSuccess == true) { TempData[GlobalConstants.TempDataSuccessMessageKey] = GlobalConstants .ArticleMessage .EditArticleSuccess; } else { TempData[GlobalConstants.TempDataErrorMessageKey] = GlobalConstants.WrongMessage; } return(RedirectToAction("GetById", new { id = model.Id })); }