示例#1
0
        public async Task <string> Init()
        {
            await _blogRepository.RemoveAllPosts();

            await _blogRepository.EnsureIndex();

            await _blogRepository.AddPost(new BlogPostModel
            {
                Body = "Test Post 1",
                User = "******"
            });

            await _blogRepository.AddPost(new BlogPostModel
            {
                Body = "Test Post 2",
                User = "******"
            });

            await _blogRepository.AddPost(new BlogPostModel
            {
                Body = "Test Post 3",
                User = "******"
            });

            await _blogRepository.AddPost(new BlogPostModel
            {
                Body = "Test Post 4",
                User = "******"
            });

            return("Done");
        }
示例#2
0
        public ContentResult AddPost(Post post)
        {
            string json;

            if (ModelState.IsValid)
            {
                var id = _blogRepository.AddPost(post);

                json = JsonConvert.SerializeObject(new
                {
                    id      = id,
                    success = true,
                    message = "Post added successfully."
                });
            }
            else
            {
                json = JsonConvert.SerializeObject(new
                {
                    id      = 0,
                    success = false,
                    message = "Failed to add the post."
                });
            }

            return(Content(json, "application/json"));
        }
示例#3
0
        public ActionResult AddPost(Post post)
        {
            string Json;

            ModelState.Clear();
            if (TryValidateModel(post))
            {
                var id = _blogrepository.AddPost(post);
                Json = JsonConvert.SerializeObject(new
                {
                    id      = id,
                    success = true,
                    message = "Post added successfully!"
                });
            }
            else
            {
                Json = JsonConvert.SerializeObject(new
                {
                    id      = 0,
                    success = false,
                    message = "Failed to add Post!"
                });
            }
            return(Content(Json, "application/json"));
        }
示例#4
0
        public async Task <IActionResult> Create(PostsVm posts, string[] TagPosts)
        {
            if (TagPosts != null)
            {
                posts.TagPosts = new List <TagPosts>();
                foreach (var tag in TagPosts)
                {
                    var tagToAdd = new TagPosts {
                        TagId = Convert.ToInt32(tag), PostId = posts.PostId
                    };
                    posts.TagPosts.Add(tagToAdd);
                }
            }

            if (ModelState.IsValid)
            {
                Posts    model = _mapper.Map <Posts>(posts);
                DateTime date  = DateTime.Now;
                model.DateCreated = date;
                _context.AddPost(model);
                _context.Commit();
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.GetCategoryList(), "CategoryId", "Title", posts.CategoryId);
            ViewData["Tags"]       = new MultiSelectList(_context.GetTagList().AsEnumerable(), "TagId", "Title", posts.TagPosts.Select(e => e.TagId).AsEnumerable());
            return(View(posts));
        }
示例#5
0
        public ContentResult AddPost(Post post)
        {
            string json;

            ModelState.Clear();
            post.PostedOn = DateTime.Now;
            if (TryValidateModel(post))
            {
                var id = _blogRepository.AddPost(post);

                json = JsonConvert.SerializeObject(new
                {
                    id      = post.Id,
                    success = true,
                    message = "Post added successfully."
                });
            }
            else
            {
                json = JsonConvert.SerializeObject(new
                {
                    id      = 0,
                    success = false,
                    message = "Failed to add the post."
                });
            }


            return(Content(json, "application/json"));
        }
示例#6
0
        public ActionResult Create([ModelBinder(typeof(CreateOrEditPostCustomDataBinder))] CreateOrEditPostModel model)
        {
            model.Post.UrlSlug = Slug.GenerateSlug(model.Post.Title);

            _postRepository.Add(model.Post);
            _blogRepository.AddPost(model.Blog.Id, model.Post);
            return(RedirectToAction("Details", "Blog"));
        }
示例#7
0
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                _repository.AddPost(post);
                return(RedirectToAction("Index"));
            }

            return(View(post));
        }
示例#8
0
        public Task Post([FromBody] BlogPostCreateModel model)
        {
            var userId    = User.Identity.Name;
            var postModel = new BlogPostModel
            {
                User = userId,
                Body = model.Body,
                Tags = model.Tags
            };

            return(_blogRepository.AddPost(postModel));
        }
示例#9
0
        public IActionResult Create(long blogID, [FromBody] Post post)
        {
            Post p = repository.AddPost(blogID, post);

            if (p == null)
            {
                return(BadRequest());
            }

            //post.BlogID = null;
            return(Created($"{p.ID}", post));
        }
示例#10
0
        public async Task <ActionResult> Edit(PostViewModel postvm)
        {
            ViewBag.Message = "Wanna create a post ?";

            var post = new Post
            {
                Title        = postvm.Title,
                Id           = postvm.Id,
                Body         = postvm.Body,
                LastModified = DateTime.Now,
                Category     = postvm.Category,
                //Author = postvm.Author,
                Tags        = postvm.Tags,
                Published   = postvm.Published,
                Description = postvm.Description
            };

            if (postvm.Image == null)
            {
                post.Image = postvm.CurrentImage;
            }
            else
            {
                if (!string.IsNullOrEmpty(postvm.CurrentImage))
                {
                    _fileManager.RemoveImage(postvm.CurrentImage);
                }
                post.Image = await _fileManager.SaveImage(postvm.Image);
            }
            if (post.Id > 0)
            {
                _repo.UpdatePost(post);
            }
            else
            {
                post.Created = DateTime.Now;
                _repo.AddPost(post);
            }



            if (await _repo.SaveChangesAsync())
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(post));
            }
        }
示例#11
0
        public ActionResult Create(CreatePostViewModel p)
        {
            if (_accessValidator.IsRedactor() || _accessValidator.IsAdministration())
            {
                if (ModelState.IsValid)
                {
                    _postRepository.AddPost(p);
                    return(RedirectToAction("Index", "Home"));
                }
                ModelState.AddModelError("", "All fields, except image, should be filled!");
                return(View(p));
            }

            return(RedirectToAction("Unauthorized", "Error"));
        }
        public async Task <IActionResult> Edit(Post post)
        {
            if (post.Id > 0)
            {
                repo.UpdatePost(post);
            }
            else
            {
                repo.AddPost(post);
            }


            if (await repo.SaveChangesAsync())
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(post));
            }
        }
        public ActionResult Create([ModelBinder(typeof(CreateOrEditPostCustomDataBinder))] CreateOrEditPostModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Post.Title))
            {
                ModelState.AddModelError("Post.Title", "You must specify a title.");
            }

            if (string.IsNullOrWhiteSpace(model.Post.Content))
            {
                ModelState.AddModelError("Post.Content", "You must specify a content.");
            }

            if (!ModelState.IsValid)
            {
                return(View());
            }
            model.Post.UrlSlug = Slug.GenerateSlug(model.Post.Title);

            _postRepository.Add(model.Post);
            _blogRepository.AddPost(model.Blog.Id, model.Post);
            return(RedirectToAction("Details", "Blog"));
        }
示例#14
0
 public async Task <Post> AddPost(Post post)
 {
     post.CreatedAt = DateTime.UtcNow;
     return(await _blogRepository.AddPost(post));
 }
示例#15
0
        public async Task <IActionResult> OnPostAsync()
        {
            CategoryOptions = new List <string>();

            foreach (var category in _blogRepo.GetCategories(false))
            {
                CategoryOptions.Add(category.CategoryName);
            }
            if (ModelState.IsValid)
            {
                if (Input.Id == 0)
                {
                    var user = await _userManager.GetUserAsync(User);

                    var blogPost = new BlogPost
                    {
                        Title      = Input.Title,
                        Excerpt    = Input.Excerpt,
                        Content    = Input.Content,
                        AuthorId   = user.Id,
                        Categories = Input.Categories,
                        HasImage   = false,

                        DateCreated = DateTime.Now,
                        LastUpdated = DateTime.Now,
                        IsPublished = false,
                        IsFeatured  = false,
                        //TODO: UniqueId
                        Slug          = Input.Title.ToLowerInvariant().Replace(' ', '-'),
                        AllowComments = Input.AllowComments
                    };


                    _blogRepo.AddPost(blogPost);
                    if (Input.Image != null)
                    {
                        if (Input.Image.Length > 0)
                        {
                            using (var ms = new MemoryStream())
                            {
                                Input.Image.CopyTo(ms);
                                var  fileBytes  = ms.ToArray();
                                bool imageSaved = await _mediaStorageService.SaveBlogPostImage(fileBytes, Input.Image.FileName, blogPost.Slug, blogPost.PostId);

                                blogPost.HasImage = imageSaved;
                                _blogRepo.UpdatePost(blogPost);
                            }
                        }
                    }
                    return(RedirectToPage("./EditBlogPost", new { id = blogPost.PostId }));
                }
                else
                {
                    var blogPost = _blogRepo.GetPost(Input.Id);
                    blogPost.Content       = Input.Content;
                    blogPost.Excerpt       = Input.Excerpt;
                    blogPost.Categories    = Input.Categories;
                    blogPost.LastUpdated   = DateTime.Now;
                    blogPost.AllowComments = Input.AllowComments;
                    if (Input.Image != null)
                    {
                        if (Input.Image.Length > 0)
                        {
                            using (var ms = new MemoryStream())
                            {
                                Input.Image.CopyTo(ms);
                                var  fileBytes  = ms.ToArray();
                                bool imageSaved = await _mediaStorageService.SaveBlogPostImage(fileBytes, Input.Image.FileName, blogPost.Slug, blogPost.PostId);

                                blogPost.HasImage = imageSaved;
                            }
                        }
                    }

                    _blogRepo.UpdatePost(blogPost);
                }
            }

            return(Page());
        }
示例#16
0
        public async Task <IActionResult> CreatePost(PostCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Image == null)
                {
                    _clientNotification.AddToastNotification("You Have to Upload An Image!", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-full-width",
                        PreventDuplicates = true
                    });
                    return(View(model));
                }

                if (model.Image.Length > _options.MaxBytes)
                {
                    _clientNotification.AddToastNotification("Image File size Exceeded", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-full-width",
                        PreventDuplicates = true
                    });
                    return(View(model));
                }

                if (!_options.IsSupported(model.Image.FileName))
                {
                    _clientNotification.AddToastNotification("Invalid File Type", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-full-width",
                        PreventDuplicates = true
                    });
                }
                else
                {
                    var user = await _userManager.GetUserAsync(User);

                    var post = new Post
                    {
                        Id          = Guid.NewGuid().ToString().Replace("-", string.Empty).ToLowerInvariant(),
                        Author      = user.Name + " " + user.Surname,
                        Title       = model.Title,
                        Description = model.Description,
                        Slug        = _blogRepository.CreateSlug(model.Title),
                        ImageUrl    = _fileManager.SaveImage(model.Image),
                        CategoryId  = model.Tag,
                        Body        = model.Body,
                        UserId      = user.Id
                    };
                    _blogRepository.AddPost(post);
                    await _blogRepository.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }

            _clientNotification.AddToastNotification("You Have Errors", NotificationHelper.NotificationType.error, new ToastNotificationOption
            {
                NewestOnTop       = true,
                CloseButton       = true,
                PositionClass     = "toast-top-full-width",
                PreventDuplicates = true
            });
            return(View(model));
        }
示例#17
0
 public async Task AddPost(AddPostRequest request)
 {
     var post = _mapper.Map <AddPostRequest, Post>(request);
     await _repository.AddPost(post);
 }