public async Task <ActionResult> UpdateArticle(UpdateArticleRequest article) { var model = new UpdateArticleViewModel(); if (this.ModelState.IsValid) { await this.blogCommandService.UpdateArticle(article.Id, new UpdateArticleModel { Body = article.Body, Header = article.Header, TeaserText = article.TeaserText, AuthorId = this.User.Identity.GetUserId() }); return(RedirectToAction("MyArticles")); } model.Article = new UpdateArticleDto { Id = article.Id, Body = article.Body, Header = article.Header, TeaserText = article.TeaserText }; return(View(model)); }
public async Task Update(string userId, ArticlePrimaryKey key, UpdateArticleViewModel request) { var article = await _context.Article.FirstOrDefaultAsync(x => x.CreatedYear == key.CreatedYear && x.TitleShrinked == key.TitleShrinked).CAF(); string contentChecksumPreUpdate = ChecksumAlgorithm.ComputeMD5Checksum(article.Content); _mapper.Map(request, article); _articleFactory.SetUpdated(article); await EnsureRequestOfNarrationIfNarratable(article, contentChecksumPreUpdate).CAF(); await using var transaction = await _context.Database.BeginTransactionAsync().CAF(); try { await _context.SaveChangesAsync().CAF(); await transaction.CommitAsync().CAF(); } catch (Exception e) { _logger.LogError(e.Message); throw new FailedOperationException(); } }
public async Task <ActionResult> GetForUpdateArtile(int articleId) { var model = new UpdateArticleViewModel(); model.Article = await this.blogReadService.GetArticleByIdAsync <UpdateArticleDto>(articleId); return(View(model)); }
public ActionResult UpdateArticle(UpdateArticleViewModel article) { if (!ModelState.IsValid) { GetArticlesAndTags(); using (var context = new DataContext()) { article.ArticleImages = context.Articles.Include("ArticleImages").FirstOrDefault(a => a.Id == article.Id).ArticleImages; } return(View(article)); } var articleImages = new List <ArticleImages>(); foreach (var image in article.Images) { if (image != null) { string imagePath = Guid.NewGuid().ToString().Substring(0, 10) + "_" + System.IO.Path.GetFileName(image.FileName); articleImages.Add(new ArticleImages { ImagePath = imagePath }); WebImage img = new WebImage(image.InputStream); img.Resize(1600, 600); img.Save(Server.MapPath("~/Content/images/articles/" + imagePath)); } } using (var context = new DataContext()) { var articleUpdate = context.Articles.Include("ArticleImages").Include("ArticleTags").Include("SimilarArticles").FirstOrDefault(a => a.Id == article.Id); articleUpdate.Title = article.Title; articleUpdate.Slug = article.Title.GenerateSlug(); articleUpdate.Description = article.Description; articleUpdate.Text = article.Text; //if(articleImages != null && articleImages.Count > 0) articleUpdate.ArticleImages.AddRange(articleImages); articleUpdate.ArticleTags = context.ArticleTags.Where(t => article.ArticleTags.Contains(t.Id)).ToList(); if (article.SimilarArticles != null && article.SimilarArticles.Count() > 0) { context.SimilarArticles.RemoveRange(articleUpdate.SimilarArticles); foreach (var similar in article.SimilarArticles) { context.SimilarArticles.Add(new SimilarArticle { Article = articleUpdate, SimilarArticleId = similar }); } } context.SaveChanges(); } return(Redirect("/admin/articles")); }
public async Task <IActionResult> Update(int articleId) { UpdateArticleViewModel model = new UpdateArticleViewModel { Categories = new SelectList((await Mediator.Send(new GetAllCategoriesQuery())).Select(c => new CategoryTitleViewModel(c, c.Title)), "Id", "Title"), ArticleId = articleId }; return(View(model)); }
public async Task <IActionResult> Update(int articleId, UpdateArticleViewModel model) { if (ModelState.IsValid) { var currentUser = await _userManager.GetUserAsync(HttpContext.User); var userRoles = await _userManager.GetRolesAsync(currentUser); string path = null; if (model.TitleImage != null) { FileSaverResult fileSaverResult = await _fileSaver.SaveArticleTitleImage(_appEnvironment.WebRootPath, model.TitleImage); if (fileSaverResult.IsSuccessful) { path = fileSaverResult.Path; } else { ModelState.AddModelError("", "Can't save image"); } } var result = await Mediator.Send(new UpdateArticleCommand { Title = model.Title, IsPublic = model.IsPublic, CategoryId = model.CategoryId, TitleImagePath = path, UserRoles = userRoles, ArticleId = articleId, }); if (result.IsSuccessful) { return(RedirectToAction("Index", "Article")); } else { ModelState.AddModelError("", result.Message); } } var allCategories = await Mediator.Send(new GetAllCategoriesQuery()); if (allCategories.Any()) { model.Categories = new SelectList(allCategories.Select(c => new CategoryTitleViewModel(c, c.Title)), "Id", "Title"); return(View(model)); } else { return(RedirectToAction("ErrorMessage", "Home", new { message = "No categories found" })); } }
public async Task Update(string userId, ArticlePrimaryKey key, UpdateArticleViewModel request) { var article = await(await _client.Article().FindAsync(x => x.Id == key.Id).CAF()) .FirstOrDefaultAsync().CAF(); string contentChecksumPreUpdate = ChecksumAlgorithm.ComputeMD5Checksum(article.Content); _mapper.Map(request, article); _articleFactory.SetUpdated(article); await EnsureRequestOfNarrationIfNarratable(article, contentChecksumPreUpdate).CAF(); await _client.Article().FindOneAndReplaceAsync(x => x.Id == key.Id, article).CAF(); }
public async Task <ActionResult> Edit(UpdateArticleViewModel updateArticleView) { try { await Mediator.Send(updateArticleView); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <IActionResult> UpdateArticle([FromBody] UpdateArticleViewModel article_model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (article_model._id_article < 1) { return(BadRequest()); } if (article_model._id_category < 1) { return(BadRequest()); } var article = await _context.Articles.FirstOrDefaultAsync(k => k._id_article == article_model._id_article); if (article == null) { return(NotFound()); } article._id_category = article_model._id_category; article.code = article_model.code; article.name = article_model.name; article.price = article_model.price; article.stock = article_model.stock; article.description = article_model.description; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(BadRequest()); } return(Ok()); }
public async Task <ActionResult> Edit(UpdateArticleViewModel model) { Article article = await db.Articles.FindAsync(model.Id); if (ModelState.IsValid) { article.CategoryId = model.CategoryId; article.FullText = model.Fulltext; article.Title = model.Title; db.Entry(article).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } SelectList category = new SelectList(db.Categories, "Id", "Title"); ViewBag.Category = category; return(View(model)); }
public async ValueTask <IActionResult> UpdateArticlePost([FromRoute] string articleId, [FromForm] UpdateArticleViewModel viewModel, [FromServices] ITagService tagService) { var key = new ArticlePrimaryKey(articleId); if (!ModelState.IsValid) { return(View("UpdateArticle", viewModel)); } string userId = this.GetViewerUserId(); try { bool authorizedFor = await AuthorizedFor(key, userId).CAF(); if (!authorizedFor) { return(Forbid()); } } catch (ArticleNotFoundException) { _logger.LogInformation("User: {UserId} attempted to update a non-existing article with key: {articleId}", userId, key.Id); return(NotFound()); } await _articleService.Update(userId, key, viewModel).CAF(); await tagService.AddOrUpdateTagsPerArticle(key, viewModel.TagsAsString).CAF(); _logger.LogInformation("Updated an article by user: {UserId} with article key: {articleId}", userId, key.Id); return(RedirectToAction("UpdateArticleView", new { articleId = key.Id })); }
public async Task <ActionResult> Edit(int id) { Article article = await db.Articles.FindAsync(id); if (article == null) { return(RedirectToAction("Index")); } UpdateArticleViewModel model = new UpdateArticleViewModel { Id = article.ArticleId, CategoryId = article.CategoryId, Fulltext = article.FullText, Title = article.Title }; SelectList category = new SelectList(db.Categories, "Id", "Title"); ViewBag.Category = category; return(View(model)); }
public ActionResult UpdateArticle(int id) { GetArticlesAndTags(); using (var context = new DataContext()) { var article = context.Articles.Include("ArticleImages").Include("ArticleTags").Include("SimilarArticles").FirstOrDefault(a => a.Id == id); var articleViewModel = new UpdateArticleViewModel { Id = article.Id, Title = article.Title, Create = article.Create, Description = article.Description, Text = article.Text, ArticleImages = article.ArticleImages, SimilarArticles = article.SimilarArticles.Select(s => s.SimilarArticleId).ToArray(), ArticleTags = article.ArticleTags.Select(t => t.Id).ToArray() }; return(View(articleViewModel)); } }