public IActionResult Edit(int id, PostDataModel postDataModel, IFormFile _formFile)
        {
            PostDataModel post = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts).FirstOrDefault(bp => bp.PostId == id);

            if (ModelState.IsValid)
            {
                if (post != null)
                {
                    post.Title          = postDataModel.Title;
                    post.Preview        = postDataModel.Preview;
                    post.Content        = postDataModel.Content;
                    post.LastChangeDate = DateTime.Now;
                    post.Link           = postDataModel.Link;
                    post.Tags           = postDataModel.Tags;

                    if (_formFile != null)
                    {
                        post.Cover = FileToByteArray(_formFile);
                    }
                }

                this.applicationContext.Posts.Update(post);
                this.applicationContext.SaveChanges();

                return(RedirectToAction("View", "Blog", new { id = post.Blog.BlogId }));
            }

            postDataModel.Title   = post.Title;
            postDataModel.Content = post.Content;
            postDataModel.Cover   = post.Cover;

            return(View(postDataModel));
        }
Exemple #2
0
        public void AutoIncludeScriptsFor_GivenViewContextAndBundleResolverWithActionScript_ReturnsStringForControllerScript()
        {
            //---------------Set up test pack-------------------
            var ctx = new ViewContext {
                Controller = Substitute.For <Controller>()
            };
            var valueProvider  = Substitute.For <IValueProvider>();
            var controllerName = RandomValueGen.GetRandomString();
            var actionName     = RandomValueGen.GetRandomString();

            valueProvider.GetValue("Controller").Returns(new ValueProviderResult(controllerName, controllerName, CultureInfo.InvariantCulture));
            valueProvider.GetValue("Action").Returns(new ValueProviderResult(actionName, actionName, CultureInfo.InvariantCulture));

            ctx.Controller.ValueProvider = valueProvider;

            var bundleResolver = Substitute.For <IBundleResolver>();
            var script         = RandomValueGen.GetRandomString() + ".js";

            bundleResolver.GetBundleContents(AutoInclude.BundleBase + controllerName + "/" + actionName).Returns(new[] { script });

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = AutoInclude.AutoIncludeScriptsFor(ctx, bundleResolver, (name) => new HtmlString(
                                                               $"<script src=\"{script}\"></script>"));

            //---------------Test Result -----------------------
            var parts = result.ToHtmlString().Split('\n');

            Assert.AreEqual(1, parts.Length);
            StringAssert.Contains(script, parts[0]);
        }
Exemple #3
0
        public void AutoIncludeScriptsFor_GivenViewContextAndBundleResolverWithNoMatchingBundles_ReturnsEmptyHTMLString()
        {
            //---------------Set up test pack-------------------
            var ctx = new ViewContext {
                Controller = Substitute.For <Controller>()
            };
            var valueProvider  = Substitute.For <IValueProvider>();
            var controllerName = RandomValueGen.GetRandomString();
            var actionName     = RandomValueGen.GetRandomString();

            valueProvider.GetValue("Controller").Returns(new ValueProviderResult(controllerName, controllerName, CultureInfo.InvariantCulture));
            valueProvider.GetValue("Action").Returns(new ValueProviderResult(actionName, actionName, CultureInfo.InvariantCulture));

            ctx.Controller.ValueProvider = valueProvider;

            var bundleResolver = Substitute.For <IBundleResolver>();

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, bundleResolver.GetBundleContents(AutoInclude.BundleBase + controllerName).Count());

            //---------------Execute Test ----------------------
            var result = AutoInclude.AutoIncludeScriptsFor(ctx, bundleResolver);

            //---------------Test Result -----------------------
            Assert.AreEqual("", result.ToHtmlString());
        }
        public IActionResult Index()
        {
            SearchViewModel search = new SearchViewModel
            {
                Blogs = AutoInclude.IncludeAll <BlogDataModel>(this.applicationContext.Blogs),
                Posts = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts)
            };

            return(View(search));
        }
        public IActionResult Edit(int id)
        {
            PostDataModel target = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts).FirstOrDefault(bp => bp.PostId == id);

            if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(target.Author.Id) || this.User.IsInRole("Administrator")))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }

            return(View(target));
        }
Exemple #6
0
        public async Task <IActionResult> Delete(int id)
        {
            CommentDataModel commentDataModel = AutoInclude.IncludeAll <CommentDataModel>(this.applicationContext.Comments).FirstOrDefault(c => c.CommentId == id);

            if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(commentDataModel.Author.Id) || this.User.IsInRole("Administrator")))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }

            this.applicationContext.Comments.Remove(commentDataModel);
            await this.applicationContext.SaveChangesAsync();

            return(RedirectToAction("Details", "Post", new { id = commentDataModel.Post.PostId }));
        }
Exemple #7
0
        public async Task <IActionResult> Edit(int id, CommentDataModel comment)
        {
            if (ModelState.IsValid)
            {
                CommentDataModel commentDataModel = AutoInclude.IncludeAll <CommentDataModel>(this.applicationContext.Comments).FirstOrDefault(c => c.CommentId == id);
                commentDataModel.Content = comment.Content;

                this.applicationContext.Update(commentDataModel);
                await this.applicationContext.SaveChangesAsync();

                return(RedirectToAction("Details", "Post", new { id = commentDataModel.PostId }));
            }

            return(View(comment));
        }
        public IActionResult Pin(int id)
        {
            var post = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts).FirstOrDefault(p => p.PostId == id);

            if (post.Pinned)
            {
                post.Pinned = false;
            }
            else
            {
                post.Pinned = true;
            }

            this.applicationContext.Posts.Update(post);
            this.applicationContext.SaveChanges();

            return(RedirectToAction("View", "Blog", new { id = post.Blog.BlogId }));
        }
Exemple #9
0
        public void AutoIncludeScriptsFor_GivenViewContextAndBundleResolverWithControllerAndActionScripts_ReturnsStringForControllerScript()
        {
            //---------------Set up test pack-------------------
            var ctx = new ViewContext {
                Controller = Substitute.For <Controller>()
            };
            var valueProvider  = Substitute.For <IValueProvider>();
            var controllerName = RandomValueGen.GetRandomString();
            var actionName     = RandomValueGen.GetRandomString();

            valueProvider.GetValue("Controller").Returns(new ValueProviderResult(controllerName, controllerName, CultureInfo.InvariantCulture));
            valueProvider.GetValue("Action").Returns(new ValueProviderResult(actionName, actionName, CultureInfo.InvariantCulture));

            ctx.Controller.ValueProvider = valueProvider;

            var    bundleResolver = Substitute.For <IBundleResolver>();
            string c1             = RandomValueGen.GetRandomString() + ".js",
                   c2             = RandomValueGen.GetRandomString() + ".js",
                   a1             = RandomValueGen.GetRandomString() + ".js",
                   a2             = RandomValueGen.GetRandomString() + ".js";

            bundleResolver.GetBundleContents(AutoInclude.BundleBase + controllerName).Returns(new[] { c1, c2 });
            bundleResolver.GetBundleContents(AutoInclude.BundleBase + controllerName + "/" + actionName).Returns(new[] { a1, a2 });

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = AutoInclude.AutoIncludeScriptsFor(ctx, bundleResolver, (names) =>
            {
                return(new HtmlString(string.Join("\n", bundleResolver
                                                  .GetBundleContents(names[0])
                                                  .Select(script => $"<script src=\"{script}\"></script>"))));
            });

            //---------------Test Result -----------------------
            var parts = result.ToHtmlString().Split('\n');

            Assert.AreEqual(4, parts.Length);
            Assert.IsTrue(new[] { c1, c2, a1, a2 }.All(script => parts.Any(p => p.Contains(script))));
        }
        public async Task <IActionResult> Archiv(int id)
        {
            PostDataModel target = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts).FirstOrDefault(bp => bp.PostId == id);

            if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(target.Author.Id) || this.User.IsInRole("Administrator")))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }

            if (target.Archieved == false)
            {
                target.Archieved = true;
            }
            else
            {
                target.Archieved = false;
            }

            this.applicationContext.Posts.Update(target);
            await this.applicationContext.SaveChangesAsync();

            return(RedirectToAction("Details", "Post", new { id }));
        }
        public async Task <IActionResult> Delete(int id)
        {
            PostDataModel   target = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts).FirstOrDefault(bp => bp.PostId == id);
            ApplicationUser author = this.applicationContext.Users.FirstOrDefault(u => u.Id == target.Author.Id);

            if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(target.Author.Id) || this.User.IsInRole("Administrator")))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }

            author.Posts.Remove(target);

            this.applicationContext.Comments.RemoveRange(target.Comments);

            if (target != null)
            {
                this.applicationContext.Users.Update(author);
                this.applicationContext.Posts.Remove(target);
            }

            await this.applicationContext.SaveChangesAsync();

            return(RedirectToAction("View", "Blog", new { id = target.Blog.BlogId }));
        }
        public IActionResult Search(string searchString)
        {
            if (string.IsNullOrWhiteSpace(searchString))
            {
                return(RedirectToAction("Index", "Home"));
            }

            var posts = AutoInclude.IncludeAll <PostDataModel>(this.applicationContext.Posts);
            var blogs = AutoInclude.IncludeAll <BlogDataModel>(this.applicationContext.Blogs);
            var users = this.applicationContext.Users
                        .Include(x => x.Posts)
                        .ThenInclude(x => x.Comments)
                        .Include(x => x.Posts)
                        .ThenInclude(x => x.Author)
                        .Include(x => x.Posts)
                        .ThenInclude(x => x.Blog)
                        .Include(x => x.Comments).ToList();

            List <PostDataModel> searchedPost = new List <PostDataModel>();

            foreach (PostDataModel post in posts)
            {
                if (post.Title.ToLower().Contains(searchString.ToLower()) || post.Preview.ToLower().Contains(searchString.ToLower()) || post.Tags.ToLower().Contains(searchString.ToLower()))
                {
                    if (!searchedPost.Contains(post))
                    {
                        searchedPost.Add(post);
                    }
                }
            }

            List <BlogDataModel> searchedBlogs = new List <BlogDataModel>();

            foreach (BlogDataModel blog in blogs)
            {
                if (blog.Name.ToLower().Contains(searchString.ToLower()) || blog.Description.ToLower().Contains(searchString.ToLower()))
                {
                    if (!searchedBlogs.Contains(blog))
                    {
                        searchedBlogs.Add(blog);
                    }
                }
            }

            List <ApplicationUser> searchedUsers = new List <ApplicationUser>();

            foreach (ApplicationUser user in users)
            {
                if (user.UserName.ToLower().Contains(searchString.ToLower()))
                {
                    if (!searchedUsers.Contains(user))
                    {
                        searchedUsers.Add(user);
                    }
                }
            }

            SearchViewModel result = new SearchViewModel
            {
                Posts = searchedPost,
                Blogs = searchedBlogs,
                Users = searchedUsers
            };

            return(View(result));
        }