Пример #1
0
        public async Task <ValidationResult> Add(CreateBlogViewModel createBlogViewModel)
        {
            CreateBlogCommand createBlogCommand =
                _mapper.Map <CreateBlogCommand>(createBlogViewModel);

            return(await _mediator.Send(createBlogCommand));
        }
        public async Task <IActionResult> CreateBlog(CreateBlogViewModel createBlogViewModel)
        {
            if (ModelState.IsValid)
            {
                List <Tag> tags = new List <Tag>();
                tags.Add(new Tag {
                    Name = createBlogViewModel.tagName
                });
                Blog newBlog = new Blog()
                {
                    Description = createBlogViewModel.blogDescription,
                    Title       = createBlogViewModel.blogTitle,
                    Tags        = tags
                };



                ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

                newBlog.User = user;
                db.Blogs.Add(newBlog);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(createBlogViewModel));
        }
Пример #3
0
        public async Task <IActionResult> CreateBlog(CreateBlogViewModel model)
        {
            if (ModelState.IsValid)
            {
                Blog blog = new Blog
                {
                    UserId           = model.UserId,
                    Title            = model.Title,
                    BriefDescription = model.BriefDescription,
                    Topic            = model.Topic,
                    BlogContent      = model.BlogContent,
                    Date             = DateTime.Now,
                    Approved         = false
                };

                // Add to Database
                _blogRepository.Add(blog);

                //Show all blog from user
                var user = await userManager.FindByIdAsync(model.UserId);

                // Message
                TempData["Alert"]      = $"Blog with title \"{blog.Title}\" awaits approval from admin";
                TempData["AlertClass"] = "alert-warning";
                return(RedirectToAction("Author", "Profile", new { userName = user.UserName }));
            }

            return(View(model));
        }
Пример #4
0
        public virtual ActionResult Edit(CreateBlogViewModel model)
        {
            Blog blog     = _blogService.GetBlog(model.Nickname);
            var  modelOut = new CreateBlogViewModel(blog);

            return(View(modelOut));
        }
Пример #5
0
 protected override async Task OnInitializedAsync()
 {
     Blog = new CreateBlogViewModel
     {
         Name        = "",
         Description = ""
     };
 }
Пример #6
0
        public async Task <IActionResult> ConfirmCreateBlog(CreateBlogViewModel blog)
        {
            var blogDto = Mapper.Map <BlogDto>(blog);

            blogDto.User = User.CreateUserDto();
            await _blogManager.CreateBlog(blogDto);

            return(RedirectToAction("Index", "Home"));
        }
Пример #7
0
 public virtual ActionResult Update(CreateBlogViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(View("Edit", model));
     }
     _blogService.UpdateBlog(model.Nickname, model.ApproveComments, model.CommentsEnabled, model.Description,
                             model.Title);
     return(RedirectToRoute(new { controller = "Dashboard", action = "Index" }));
 }
        public IActionResult Create(CreateBlogViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model"));
            }

            var blog = _blogService.Create(vm.ToModel());

            return(Ok(BlogViewModel.From(blog)));
        }
Пример #9
0
        public async Task <BlogViewModel> CreateAsync(CreateBlogViewModel vm)
        {
            var response = await _httpClient.PostAsJsonAsync($"{_urlBase}/Create", vm);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            return(await response.Content.ReadFromJsonAsync <BlogViewModel>());
        }
Пример #10
0
        public ActionResult Create()
        {
            Blogs blog = new Blogs();

            CreateBlogViewModel viewModel = new CreateBlogViewModel
                                            (
                blog,
                UnitOfWork_.CategoryRepo.GetCategories()
                                            );

            return(View(viewModel));
        }
Пример #11
0
        public async Task <bool> Create(CreateBlogViewModel blog)
        {
            var model = Blog.Create(blog.Title, blog.UserId, blog.FirstName,
                                    blog.LastName,
                                    blog.Post, blog.UrlPath);


            blogRepository.Add(model);
            var result = await uow.SaveChangesAsync();

            return(result);
        }
Пример #12
0
        public ActionResult Create(CreateBlogViewModel model)
        {
            string currentUserId = User.Identity.GetUserId();

            if (_service.CreateBlog(model, currentUserId))
            {
                return(RedirectToAction("Index", "AdminPanel"));
            }

            ViewBag.BlogCategoryId = new SelectList(db.BlogCategories, "Id", "Name");
            return(View(model));
        }
Пример #13
0
        public ActionResult Edit(int id)
        {
            Blogs blog = UnitOfWork_.BlogRepo.GetBlogById(id);

            CreateBlogViewModel viewModel = new CreateBlogViewModel
                                            (
                blog,
                UnitOfWork_.CategoryRepo.GetCategories()
                                            );

            return(View(viewModel));
        }
Пример #14
0
        public async Task CreateBlogs_withUser()
        {
            var newRecord = new CreateBlogViewModel()
            {
                Title   = "a title",
                Content = "a content",
                Active  = true
            };

            // Arrange
            var blogService = new BlogSupervisor(_mockHttpContextAccessor.Object, _10Records_blogRepository.Object, _1User_userRepository.Object);

            // Assert
            Assert.DoesNotThrowAsync(async() => await blogService.CreateBlog(newRecord));
        }
Пример #15
0
        public IActionResult Create(CreateBlogViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Blog blog = new Blog()
                {
                    Title       = viewModel.Title,
                    Description = viewModel.Description
                };
                _blogService.Create(blog, User);

                return(RedirectToAction("Index"));
            }

            return(View(viewModel));
        }
Пример #16
0
        public async Task <ActionResult> Create([FromBody] CreateBlogViewModel blog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _blogRepository.Create(new Blog
                                         (
                                             blog.Id,
                                             blog.Name,
                                             blog.CreatedOn,
                                             new Author(blog.Author.FirstName, blog.Author.LastName)
                                         ));

            return(Created(string.Empty, blog));
        }
Пример #17
0
        public async Task CreateBlogs_whithoutUser_shouldthrowexception()
        {
            var newRecord = new CreateBlogViewModel()
            {
                Title   = "a title",
                Content = "a content",
                Active  = true
            };

            _mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            // Arrange
            var blogService = new BlogSupervisor(_mockHttpContextAccessor.Object, _10Records_blogRepository.Object, _1User_userRepository.Object);

            // Assert

            Assert.ThrowsAsync <Exception>(async() => await blogService.CreateBlog(newRecord));
        }
Пример #18
0
 public IActionResult Create(CreateBlogViewModel model)
 {
     if (ModelState.IsValid && UserId != null)
     {
         repository.Add(new Models.Blog
         {
             UserId     = UserId,
             CategoryId = model.CategoryId,
             Title      = model.Title,
             Body       = model.Body,
             CreatedAt  = DateTime.Now
         });
         TempData["msg"] = "Blog created successfully";
         return(RedirectToAction(nameof(MyPage)));
     }
     return(View(model));
 }
Пример #19
0
        public virtual ActionResult Create(CreateBlogViewModel model)
        {
            if (RedirectIfInvalidUser())
            {
                return(RedirectToAction("New", "Session"));
            }

            if (!ModelState.IsValid)
            {
                return(View("New", model));
            }
            var user = HttpContext.User as UserViewModel;

            _blogService.CreateBlog(model.Title, model.Description, model.ApproveComments, model.CommentsEnabled,
                                    model.Nickname, user.Id);
            return(RedirectToRoute(new { controller = "Dashboard", action = "Index" }));
        }
Пример #20
0
 public IActionResult Edit(CreateBlogViewModel model)
 {
     if (ModelState.IsValid)
     {
         if (repository.Blog(model.BlogId) != null)
         {
             repository.Save(new Models.Blog
             {
                 BlogId     = model.BlogId,
                 CategoryId = model.CategoryId,
                 Title      = model.Title,
                 Body       = model.Body
             });
             TempData["msg"] = "Blog Updated successfully";
             return(RedirectToAction(nameof(MyPage)));
         }
     }
     return(View(model));
 }
Пример #21
0
        public async Task <ActionResult> Create([Bind(Include = "Title,Content")] CreateBlogViewModel blog, string[] selectedTags)
        {
            try
            {
                Blog newBlog = new Blog();

                if (selectedTags != null)
                {
                    if (selectedTags.Length > MAX_BLOG_TAGS)
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                    }

                    newBlog.Tags = new List <Tag>();
                    foreach (var tag in selectedTags)
                    {
                        var taggtoAdd = db.Tags.Find(int.Parse(tag));
                        newBlog.Tags.Add(taggtoAdd);
                    }
                }

                if (ModelState.IsValid)
                {
                    newBlog.Title    = blog.Title;
                    newBlog.Content  = blog.Content;
                    newBlog.UserID   = User.Identity.GetUserId();
                    newBlog.PostDate = DateTime.Now;
                    db.Blogs.Add(newBlog);

                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }catch (Exception)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }


            return(View(blog));
        }
Пример #22
0
        public async Task <Blog> BlogMapping(CreateBlogViewModel createBlogViewModel, ClaimsPrincipal claimsPrincipal)
        {
            Blog blog = createBlogViewModel.Blog;

            blog.Author = await _userManager.GetUserAsync(claimsPrincipal);

            blog.CreatedOn = DateTime.Now;

            blog = await _blogService.CreateBlog(blog);

            string webRootPath = _webHostEnvironment.WebRootPath;
            string pathToImage = $@"{webRootPath}\UserFiles\Blogs\{blog.Id}\BlogImage.jpg";

            EnsureFolder(pathToImage);
            using (var fileStream = new FileStream(pathToImage, FileMode.Create))
            {
                await createBlogViewModel.BlogImage.CopyToAsync(fileStream);
            }

            return(blog);
        }
Пример #23
0
        public ActionResult Edit(int id, FormCollection collection)
        {
            Blogs blog = UnitOfWork_.BlogRepo.GetBlogById(id);

            try
            {
                if (blog != null && TryUpdateModel(blog, "Blog"))
                {
                    blog.PostedBy   = User.Identity.Name;
                    blog.DatePosted = DateTime.Now;

                    UnitOfWork_.BlogRepo.Save();

                    return(RedirectToAction("Index"));
                }

                else
                {
                    CreateBlogViewModel viewModel = new CreateBlogViewModel
                                                    (
                        blog,
                        UnitOfWork_.CategoryRepo.GetCategories()
                                                    );

                    return(View(viewModel));
                }
            }
            catch
            {
                CreateBlogViewModel viewModel = new CreateBlogViewModel
                                                (
                    blog,
                    UnitOfWork_.CategoryRepo.GetCategories()
                                                );

                return(View(viewModel));
            }
        }
Пример #24
0
        public ActionResult Create(Blogs blog)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    blog.PostedBy   = User.Identity.Name;
                    blog.DatePosted = DateTime.Now;

                    UnitOfWork_.BlogRepo.AddBlog(blog);
                    UnitOfWork_.BlogRepo.Save();

                    return(RedirectToAction("Index"));
                }
                else
                {
                    CreateBlogViewModel viewModel = new CreateBlogViewModel
                                                    (
                        blog,
                        UnitOfWork_.CategoryRepo.GetCategories()
                                                    );

                    return(View(viewModel));
                }
            }
            catch
            {
                CreateBlogViewModel viewModel = new CreateBlogViewModel
                                                (
                    blog,
                    UnitOfWork_.CategoryRepo.GetCategories()
                                                );

                return(View(viewModel));
            }
        }
Пример #25
0
        public bool CreateBlog(CreateBlogViewModel viewModel, string userId)
        {
            if (viewModel.UrlName.ToLower() != ServerTools.GenerateUrlFriendlyString(viewModel.UrlName))
            {
                _modelState.AddModelError("UrlName", "Url name may only consist of letters, numbers and dashes.");
            }

            if (!_modelState.IsValid ||
                (viewModel.MiniatureUrl != null && !ServerTools.Paths.TempFolderContains(viewModel.MiniatureUrl)))
            {
                return(false);
            }



            Models.Blog blog = Mapper.Map <Models.Blog>(viewModel);
            blog.UserId = userId;

            string blogFolderPath = CreateBlogFolder(blog.UrlName);

            if (viewModel.MiniatureUrl != null)
            {
                bool   invalidImage         = false;
                string tempImgPath          = Path.Combine(ServerTools.Paths.TempFolder, viewModel.MiniatureUrl);
                string imageDestinationPath = "";
                using (var img = Image.FromFile(tempImgPath))
                {
                    if (!ValidateImage(img))
                    {
                        invalidImage = true;
                    }
                    else
                    {
                        imageDestinationPath = ResizeAndSave(img, viewModel.MiniatureUrl, blogFolderPath);
                    }
                }
                File.Delete(tempImgPath);
                if (invalidImage)
                {
                    return(false);
                }
                blog.MiniatureUrl = ServerTools.RelativePath(imageDestinationPath);
            }
            else
            {
                blog.MiniatureUrl = "/MediaData/Default/miniature.jpg";
            }

            var layoutSettings = new LayoutSettings();

            /*_db.LayoutSettings.Add(layoutSettings);*/
            blog.LayoutSettings = layoutSettings;

            _db.Blogs.Add(blog);
            _db.SaveChanges();

            var postCategory = new PostCategory {
                Name = "General", UrlSlug = "general", BlogId = blog.Id
            };

            _db.PostCategories.Add(postCategory);

            var user = _db.Users.Find(userId);

            if (user.CurrentBlogId == null)
            {
                user.CurrentBlogId    = blog.Id;
                _db.Entry(user).State = EntityState.Modified;
            }
            _db.SaveChanges();

            return(true);
        }
Пример #26
0
        public async Task <IActionResult> Post([FromBody] CreateBlogViewModel value)
        {
            await _blogSupervisor.CreateBlog(value);

            return(new ObjectResult("Ok"));
        }
Пример #27
0
        public async Task <IActionResult> Create(CreateBlogViewModel model)
        {
            await _mappingManager.BlogMapping(model, User);

            return(RedirectToAction(nameof(Index), "Home"));
        }
Пример #28
0
        public async Task <IActionResult> Create([FromBody] CreateBlogViewModel blog)
        {
            await service.Create(blog);

            return(Ok());
        }