public async Task AddAsync(ArticlesCreate Article) { Article dbArticle = Article; List <Tag> tags = new List <Tag>(Article.Tags); _context.Articles.Add(dbArticle); _context.Tags.AddRange(tags); await _context.SaveChangesAsync(); dbArticle.ImgPath = await WriteFile(Article, dbArticle); _context.Update(dbArticle); await _context.SaveChangesAsync(); foreach (var tag in tags) { dbArticle.Tags.Add(new ArticleTag() { TagId = tag.Id, ArticleId = dbArticle.Id }); } await _context.SaveChangesAsync(); }
private async Task CreateAtDb(ArticlesCreate article) { if (HttpContext.User.IsInRole("Admin")) { article.isPublish = true; } await _repository.AddAsync(article); }
public async Task <IActionResult> Create(ArticlesCreate article) { if (ModelState.IsValid) { await CreateAtDb(article); return(RedirectToAction(nameof(Index))); } return(View(article)); }
public async Task <IActionResult> Create(ArticlesCreate model) { //[Bind("CoverPhotoImg")] 圖片綁定沒加會撈空 ModelState.Remove("CoverPhoto"); // 移除原先圖片必填欄位驗證 if (ModelState.IsValid) { await _blogService.SaveArticle(model); return(RedirectToAction("Index", "Management")); } return(View(model)); }
private async Task <string> WriteFile(ArticlesCreate articleCreate, Article article) { FileInfo fi = new FileInfo(articleCreate.Image.FileName); var newFilename = article.Id + "_" + String.Format("{0:d}", (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension; var webPath = @"\wwwroot\"; var path = Path.Combine("", webPath + @"\ImageFiles\" + newFilename); var pathToSave = @"/ImageFiles/" + newFilename; using (var stream = new FileStream(path, FileMode.Create)) { await articleCreate.Image.CopyToAsync(stream); } return(pathToSave); }
public async ValueTask SaveArticle(ArticlesCreate articlesCreate) { var fileName = await SaveFile(articlesCreate.CoverPhotoImg); var fileUrl = GetUploadUrl() + fileName; articlesCreate.Tags = string.Join(",", articlesCreate.TagsArray); articlesCreate.CoverPhoto = fileUrl; articlesCreate.Id = Guid.NewGuid(); articlesCreate.DayOfWeek = articlesCreate.CreateDate.DayOfWeek; _articlesRepository.Insert(articlesCreate); await InserTagCloudSync(articlesCreate.TagsArray); //await _unitOfWork.SaveChangesAsync(); await SaveAsync(); }