public async Task <IActionResult> EditArticle(ArticleEditViewModel model, IFormFile pic) { var article = _articleService.GetById(model.Id); if (ModelState.IsValid) { var titleConv = new TitleConverter(); var urlConv = new UrlConverter(); if (pic != null) { var extension = Path.GetExtension(pic.FileName); var name = urlConv.StringReplace(model.Title) + extension; var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/article-cover/" + name); using var stream = new FileStream(path, FileMode.Create); await pic.CopyToAsync(stream); model.Picture = name; article.Picture = model.Picture; } article.Title = titleConv.TitleToPascalCase(model.Title); article.Content = model.Content.Replace(" ", " "); article.Url = urlConv.StringReplace(model.Title); article.CategoryId = model.CategoryId; _articleService.Update(article); return(RedirectToAction("Index", "Article", new { area = "Admin" })); } return(View(model)); }
public async Task <IActionResult> AddArticle(ArticleAddViewModel model, IFormFile pic) { if (ModelState.IsValid) { var titleConv = new TitleConverter(); var urlConv = new UrlConverter(); if (pic != null) { var extension = Path.GetExtension(pic.FileName); var name = urlConv.StringReplace(model.Title) + extension; var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/article-cover/" + name); using var stream = new FileStream(path, FileMode.Create); await pic.CopyToAsync(stream); model.Picture = name; } var article = new Article() { Title = titleConv.TitleToPascalCase(model.Title), Content = model.Content.Replace(" ", " "), CategoryId = model.CategoryId, Url = urlConv.StringReplace(model.Title), Picture = model.Picture }; if (_context.Articles.Any(x => x.Title == model.Title)) { ViewBag.TitleExist = titleConv.TitleToPascalCase(model.Title); return(View(model)); } _articleService.Create(article); return(RedirectToAction("Index", "Article", new { area = "Admin" })); } return(View(model)); }
public IActionResult EditCategory(CategoryEditViewModel model) { if (ModelState.IsValid) { var convertor = new UrlConverter(); var titleConvertor = new TitleConverter(); var category = _categoryService.GetById(model.Id); category.Name = titleConvertor.TitleToPascalCase(model.Name); category.Url = convertor.StringReplace(model.Name); _categoryService.Update(category); return(RedirectToAction("Index", "Category", new { area = "Member" })); } return(View(model)); }
public IActionResult AddCategory(CategoryAddViewModel model) { if (ModelState.IsValid) { var urlConvertor = new UrlConverter(); var titleConverter = new TitleConverter(); var category = new Category() { Name = titleConverter.TitleToPascalCase(model.Name), Url = urlConvertor.StringReplace(model.Name.ToLower()) }; if (_context.Categories.Any(x => x.Name == model.Name)) { ViewBag.CategoryExsit = titleConverter.TitleToPascalCase(model.Name); return(View(model)); } _categoryService.Create(category); return(RedirectToAction("Index", "Category", new { area = "Member" })); } return(View(model)); }