Esempio n. 1
0
        public async Task CreateBlogPostAsync(CreateBlogPostInputModel input, string userId, string imagePath)
        {
            var blogPost = new BlogPost
            {
                Title           = input.Title,
                BlogText        = input.BlogText,
                CreatedByUserId = userId,
            };

            Directory.CreateDirectory($"{imagePath}/blogposts/");
            foreach (var image in input.Images)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');
                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Невалиден формат на снимка: {extension}");
                }

                var dbImage = new Image
                {
                    BlogPostId = blogPost.Id,
                    Extension  = extension,
                };
                blogPost.Images.Add(dbImage);

                var physicalPath = $"{imagePath}/blogposts/{dbImage.Id}.{extension}";
                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.blogPostsRepository.AddAsync(blogPost);

            await this.blogPostsRepository.SaveChangesAsync();
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(CreateBlogPostInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.blogPostsService.CreateBlogPostAsync(input, user.Id, $"{this.environment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            return(this.RedirectToAction("All"));
        }
Esempio n. 3
0
        public IActionResult Create()
        {
            var viewModel = new CreateBlogPostInputModel();

            return(this.View(viewModel));
        }