public async Task <string> CreateAsync(BlogPostCreateInputModel input, string userId, string imageFilesDirectory) { // var userEntity = this.usersRepository.AllAsNoTracking() // .FirstOrDefault(x => x.UserName == articleInputModel.UserId); //// take the user and record its id in the article, product, conformity, etc. var entity = new BlogPost { Title = input.Title.Trim(), Author = input.Author.Trim(), Details = input.Details.Trim(), PublishDate = input.PublishDate, ExternalPostUrl = input.ExternalPostUrl, UserId = userId, }; await this.blogPostsRepository.AddAsync(entity); Stream fileStream = await this.UploadFileAsync(input, imageFilesDirectory, entity); await this.blogPostsRepository.SaveChangesAsync(); await this.AddCategoriesAsync(entity, input.Categories); return(entity.Id); }
private async Task <Stream> UploadFileAsync(BlogPostCreateInputModel input, string imageFilesDirectory, BlogPost entity) { // upload to local wwwroot/images dir: Directory.CreateDirectory(imageFilesDirectory); var extension = Path.GetExtension(input.InputFile.FileName).ToLower().TrimStart('.'); entity.ImageFileExtension = extension; var fileName = Path.GetFileName(input.InputFile.FileName).ToLower(); entity.ImageFileName = fileName; var physicalPath = $"{imageFilesDirectory}/{fileName}.{extension}"; Stream fileStream = new FileStream(physicalPath, FileMode.Create); await input.InputFile.CopyToAsync(fileStream); // upload to Azure Blob variant 2: var container = this.blobServiceClient.GetBlobContainerClient(GlobalConstants.AzureStorageBlobContainerNameImages); var blobClient = container.GetBlobClient($"{fileName}.{extension}"); // fileStream pointer must be returned at its 0 byte, because it is at the last byte at the moment: fileStream.Position = 0; await blobClient.UploadAsync(fileStream); entity.ImageRemoteFileUrl = blobClient.Uri.AbsoluteUri; return(fileStream); }
public async Task <ActionResult> Post(BlogPostCreateInputModel input) { // var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; var user = await this.userManager.GetUserAsync(this.User); var inputId = await this.blogPostService.CreateAsync(input, user.Id, this.imageFilesDirectory); var model = await this.blogPostService.GetByIdAsync <BlogPostExportModel>(inputId); return(this.CreatedAtAction(nameof(this.GetById), new { id = model.Id }, model)); }
public async Task <IActionResult> Create(BlogPostCreateInputModel inputModel) { var user = await this.userManager.GetUserAsync(this.User); if (this.ModelState.IsValid) { await this.blogPostService.CreateAsync(inputModel.Title, inputModel.Content, user.Id, inputModel.PreviewContent); return(this.RedirectToAction(nameof(this.Index))); } return(this.View(inputModel)); }
public ActionResult Create(BlogPostCreateInputModel postInputModel) { if (postInputModel != null) { if (this.ModelState.IsValid) { var post = new Post { Title = postInputModel.Title, Content = postInputModel.Content, Author = this.UserProfile, AuthorId = this.UserProfile.Id, }; this.Data.Posts.Add(post); this.Data.SaveChanges(); return this.RedirectToAction("Index"); } } return this.View(postInputModel); }