Ejemplo n.º 1
0
        public async Task AddNewBlogAsync(BlogInputViewModel viewModel, string userId)
        {
            Category category;

            if (viewModel.SelectCategory == "new")
            {
                category = await this.categoriesService.AddNewCategoryAsync(viewModel.NewCategory);
            }
            else
            {
                category = await this.categoriesService.AddNewCategoryAsync(viewModel.SelectCategory);
            }

            var blog = new Blog
            {
                BulgarianTitle       = viewModel.BulgarianTitle,
                BulgarianDescription = viewModel.BulgarianDescription,
                UserId   = userId,
                Category = category,
            };

            foreach (var image in viewModel.Images)
            {
                var currImage = await this.imagesService.AddNewImageAsync(image, $"{this.environment.ContentRootPath}/wwwroot/images/blog/");

                blog.Images.Add(currImage);
            }

            await this.blogRepository.AddAsync(blog);

            await this.blogRepository.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public IActionResult Add()
        {
            var categories = this.categoriesService.GetAllTitles();
            var viewModel  = new BlogInputViewModel
            {
                Categories = categories,
            };

            return(this.View(viewModel));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Add(BlogInputViewModel viewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            await this.blogsService.AddNewBlogAsync(viewModel, this.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            return(this.Redirect("~/Blog"));
        }
Ejemplo n.º 4
0
        public async Task EditAsync(string blogId, BlogInputViewModel blog)
        {
            var blogToEdit = await this.blogRepository
                             .All()
                             .FirstOrDefaultAsync(b => b.Id == blogId);

            blogToEdit.Title   = blog.Title;
            blogToEdit.Content = blog.Content;

            await this.blogRepository.SaveChangesAsync();
        }
Ejemplo n.º 5
0
        public async Task CreateAsync(BlogInputViewModel blog)
        {
            var author = this.userRepository.AllAsNoTracking().FirstOrDefault(u => u.Id == blog.AuthorId);

            await this.blogRepository.AddAsync(new Blog
            {
                AuthorId = author.Id,
                Content  = blog.Content,
                Title    = blog.Title,
            });

            await this.blogRepository.SaveChangesAsync();
        }