Exemplo n.º 1
0
        public async Task <IActionResult> Create(CreateBlogInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var id = await this.blogsService.Create(model.Title, model.Content);

            return(this.RedirectToAction("Details", "Blog", new { id = id }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(CreateBlogInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.blogService.CreateAsync(input);

            return(this.RedirectToAction(nameof(this.Index)));
        }
Exemplo n.º 3
0
        public async Task CreateAsync(CreateBlogInputModel input, string userId, string imagePath)
        {
            var blog = new Blog
            {
                Name          = input.Name,
                SubName       = input.SubName,
                Author        = input.Author,
                Category      = input.Category,
                Date          = input.Date,
                Description   = input.Description,
                AddedByUserId = userId,
            };

            var allowedExtensions = new[] { "jpg", "png", "gif" };

            Directory.CreateDirectory($"{imagePath}/blogs");

            foreach (var image in input.BlogImages)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');

                if (!allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid Image Extension {extension}");
                }

                var dbImage = new BlogImage
                {
                    AddedByUserid = userId,
                    Extension     = extension,
                };
                blog.BlogImages.Add(dbImage);

                var phycicalPath = $"{imagePath}/blogs/{dbImage.Id}.{extension}";

                using Stream fileStream = new FileStream(phycicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.blogRepository.AddAsync(blog);

            await this.blogRepository.SaveChangesAsync();
        }
Exemplo n.º 4
0
        public async Task CreateAsync(CreateBlogInputModel input)
        {
            var uploadedImage = this.cloudinaryService.UploadAsync(input.ImageFile);
            var imageUrl      = uploadedImage.Result;

            await this.imageService.CreateImageAsync(imageUrl);

            var image = this.imageService.GetImageByUrl(imageUrl);

            var article = new Article
            {
                Title      = input.Title,
                Image      = image,
                Content    = input.Content,
                AuthorName = input.AuthorName,
            };

            await this.articleRepository.AddAsync(article);

            await this.articleRepository.SaveChangesAsync();
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create(CreateBlogInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

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

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

            return(this.Redirect("/"));
        }
Exemplo n.º 6
0
        public IActionResult Create()
        {
            var viewModel = new CreateBlogInputModel();

            return(this.View(viewModel));
        }