public async Task <IActionResult> Create(PostCreateInputModel input) { var post = AutoMapperConfig.MapperInstance.Map <Post>(input); if (!this.ModelState.IsValid) { return(this.View(input)); } ApplicationUser user = await this.userManager.GetUserAsync(this.User); int postId = await this.postsService.CreateAsync(input.Title, input.Content, input.CategoryId, user.Id); this.TempData["InfoMessage"] = "Forum post created!"; return(this.RedirectToAction(nameof(this.ById), new { id = postId })); }
public async Task <IActionResult> Create(PostCreateInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var postId = await this.postsService .CreateAsync(input.Title, input.Content, input.CategoryId, userId); this.TempData["InfoMessage"] = "Forum Post Created"; return(this.RedirectToAction(nameof(this.ById), new { id = postId })); }
public IActionResult Create(int?id) { try { var categoriesList = this.categories.GetAll <CategoryDropDownViewModel>(); var viewModel = new PostCreateInputModel(); viewModel.Categories = categoriesList; if (id != null) { viewModel.Id = (int)id; } return(this.View(viewModel)); } catch (Exception) { // To Do send message to my email for example return(this.RedirectToAction("HandleError", "Home")); } }
public async Task <IActionResult> Create(PostCreateInputModel input) { if (!await this.categoryService.IsCategoryExists(input.CategoryId)) { this.ModelState.AddModelError("Invalid Category", "Invalid Category"); } if (!this.ModelState.IsValid) { input.Categories = this.categoryService.GetAll <CategoryDropDownViewModel>(); return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); var postId = await this.postService.CreateAsync(input.Title, input.Content, input.CategoryId, user.Id); this.TempData["InfoMessage"] = "Forum post Created!"; return(this.RedirectToAction(nameof(this.ById), new { id = postId })); }
public async Task <string> CreateAsync(PostCreateInputModel input, string userId, string imagePath) { var post = new Post { CategoryId = input.CategoryId, Content = input.Content, Title = input.Title, UserId = userId, }; Directory.CreateDirectory($"{imagePath}/posts/"); if (input.Images != null) { foreach (var image in input.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new ArgumentException($"Invalid image extension {extension}"); } var dbImage = new Image { UserId = userId, Post = post, Extension = extension, }; post.Images.Add(dbImage); var physicalPath = $"{imagePath}/posts/{dbImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } } await this.postsRepository.AddAsync(post); await this.postsRepository.SaveChangesAsync(); return(post.Id); }
public async Task <IActionResult> Create(PostCreateInputModel input) { var id = string.Empty; var user = await this.userManager.GetUserAsync(this.User); if (!this.ModelState.IsValid || input.Content == null) { return(this.RedirectToAction(nameof(this.Create))); } try { id = await this.postsService.CreateAsync(input, user.Id, $"{this.environment.WebRootPath}/images"); } catch (Exception ex) { this.ModelState.AddModelError(string.Empty, ex.Message); return(this.RedirectToAction(nameof(this.Create))); } this.TempData["InfoMessage"] = "Forum post created!"; return(this.RedirectToAction(nameof(this.ById), new { id })); }
public async Task <IActionResult> Create(PostCreateInputModel input) { var post = AutoMapperConfig.MapperInstance.Map <PostCreateInputModel>(input); if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); var files = input.Files; var postId = await this.postsService.CreateAsync(input.Title, input.Content, input.CategoryId, user.Id); if (files != null) { var urlOfProducts = await this.postsService.UploadAsync(this.cloudinary, files); await this.postsService.AddImageInBase(urlOfProducts, postId); } return(this.RedirectToAction(nameof(this.ById), new { id = postId })); }