Example #1
0
        public void Return_EmptyString_If_String_empty()
        {
            // Arrange


            //Act
            var result = Slugefier.GetFriendlyTitle("");


            //Assert
            Assert.IsTrue(result == "");
        }
Example #2
0
        public void Return_Succesfull_Slug()
        {
            // Arrange
            string result = "";

            //Act
            result = Slugefier.GetFriendlyTitle("Test slug for testing");


            //Assert
            Assert.IsTrue(!result.Contains(" "));
        }
Example #3
0
        public async Task <BlogModel> GetBlog(string slug)
        {
            var result = Slugefier.GetFriendlyTitle(slug);

            var exists = db.Blogs.AsNoTracking().Include(x => x.BlogTags).ThenInclude(x => x.Tag).SingleOrDefault(x => x.Slug == slug);

            //if its not found return null
            if (exists == null)
            {
                return(null);
            }

            return(mapper.Map <BlogModel>(exists));
        }
Example #4
0
        public async Task <BlogModel> AddBlog(AddBlogModel model)
        {
            Blog blog = mapper.Map <Blog>(model);

            blog.Slug = Slugefier.GetFriendlyTitle(model.Title);
            List <string> tagNames = model.TagList;

            //Make sure that the title isn't taken already
            var IsTaken = await db.Blogs.AsNoTracking().SingleOrDefaultAsync(x => x.Slug == blog.Slug);

            if (IsTaken != null)
            {
                return(null);
            }


            await db.Blogs.AddAsync(blog);

            await db.SaveChangesAsync();

            List <BlogTag> ValidBlogTags = new List <BlogTag>();
            List <Tag>     Tags          = await db.Tags.AsNoTracking().ToListAsync();

            foreach (var tag in tagNames)
            {
                //make sure each tag exists
                var exsists = Tags.FirstOrDefault(x => x.Name.ToLower().Trim() == tag.ToLower().Trim());

                //If it exists add it otherwise skip it
                if (exsists != null)
                {
                    ValidBlogTags.Add(new BlogTag()
                    {
                        TagId = exsists.Id, BlogSlug = blog.Slug
                    });
                }
            }

            await db.BlogTags.BulkInsertAsync(ValidBlogTags);

            await db.BulkSaveChangesAsync();


            //return the newly added blog with the updated navigation properties/tags
            Blog UpdatedBlog = await db.Blogs.AsNoTracking().Include(x => x.BlogTags).ThenInclude(x => x.Tag).FirstOrDefaultAsync(x => x.Slug == blog.Slug);

            return(mapper.Map <BlogModel>(UpdatedBlog));
        }
Example #5
0
        public async Task <BlogModel> UpdateBlog(UpdateBlogModel model)
        {
            Blog exists = await db.Blogs.AsNoTracking().SingleOrDefaultAsync(x => x.Slug == model.Slug);



            if (exists == null)
            {
                return(null);
            }

            if (model.Body != null)
            {
                exists.Body = model.Body;
            }

            if (model.Description != null)
            {
                exists.Description = model.Description;
            }


            if (model.Title != null)
            {
                //make sure that the new title/slug isn't taken
                var newSlug = Slugefier.GetFriendlyTitle(model.Title);

                var taken = await db.Blogs.AsNoTracking().SingleOrDefaultAsync(x => x.Slug == newSlug);

                if (taken != null)//id its taken return null so we can send BadRequest response
                {
                    return(null);
                }


                exists.Title = model.Title;
                exists.Slug  = Slugefier.GetFriendlyTitle(model.Title);
            }

            exists.UpdatedAt = DateTime.UtcNow;//update edit time
            await db.Blogs.SingleUpdateAsync(exists);

            await db.SaveChangesAsync();

            return(mapper.Map <BlogModel>(exists));
        }
Example #6
0
        public static void SeedData(RubiconContext db)
        {
            //Check if there are any Tags if not generate some
            var Tags = db.Tags.ToList();

            if (Tags.Count == 0)
            {
                List <Tag> tags = new List <Tag>();
                tags.Add(new Tag()
                {
                    Name = "PC"
                });
                tags.Add(new Tag()
                {
                    Name = "MacOS"
                });
                tags.Add(new Tag()
                {
                    Name = "CV"
                });
                tags.Add(new Tag()
                {
                    Name = "Work"
                });
                tags.Add(new Tag()
                {
                    Name = "Android"
                });
                tags.Add(new Tag()
                {
                    Name = "Deep learning"
                });

                db.AddRange(tags);
                db.SaveChanges();
            }


            //Check if there are Blogs if not generate some
            var Blogs = db.Blogs.ToList();

            if (Blogs.Count == 0)
            {
                List <Tag> tags = db.Tags.ToList();

                Blog blog1 = new Blog()
                {
                    Title       = "Why you should hire me!",
                    Description = "A well tough out joke",
                    Body        = "You really should at least chuckle at this",
                    Slug        = Slugefier.GetFriendlyTitle("Why you should hire me!"),
                };

                db.Blogs.Add(blog1); db.SaveChanges();

                List <BlogTag> blogTags1 = new List <BlogTag>();
                blogTags1.Add(new BlogTag()
                {
                    BlogSlug = blog1.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "PC").Id
                });
                blogTags1.Add(new BlogTag()
                {
                    BlogSlug = blog1.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "CV").Id
                });
                blogTags1.Add(new BlogTag()
                {
                    BlogSlug = blog1.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "Work").Id
                });

                db.BlogTags.AddRange(blogTags1); db.SaveChanges();



                Blog blog2 = new Blog()
                {
                    Title       = "I am running out of jokes after one blog",
                    Description = "The truth",
                    Body        = "Kinda sad when you think about it",
                    Slug        = Slugefier.GetFriendlyTitle("I am running out of jokes after one blog"),
                };

                db.Blogs.Add(blog2); db.SaveChanges();

                List <BlogTag> blogTags2 = new List <BlogTag>();
                blogTags2.Add(new BlogTag()
                {
                    BlogSlug = blog2.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "PC").Id
                });
                blogTags2.Add(new BlogTag()
                {
                    BlogSlug = blog2.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "Android").Id
                });
                blogTags2.Add(new BlogTag()
                {
                    BlogSlug = blog2.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "Deep learning").Id
                });
                db.BlogTags.AddRange(blogTags2); db.SaveChanges();



                Blog blog3 = new Blog()
                {
                    Title       = "Only 4 blogs are generated!",
                    Description = "true",
                    Body        = "Small custom size is better than a lot of giberish data",
                    Slug        = Slugefier.GetFriendlyTitle("Only 4 blogs are generated!"),
                };
                db.Blogs.Add(blog3); db.SaveChanges();

                List <BlogTag> blogTags3 = new List <BlogTag>();
                blogTags3.Add(new BlogTag()
                {
                    BlogSlug = blog3.Slug,
                    TagId    = tags.SingleOrDefault(x => x.Name == "PC").Id
                });
                db.BlogTags.AddRange(blogTags3); db.SaveChanges();



                Blog blog4 = new Blog()
                {
                    Title       = "A blog with no tags",
                    Description = "tags are not required ",
                    Body        = "tags are not required ",
                    Slug        = Slugefier.GetFriendlyTitle("A blog with no tags"),
                };
                db.Blogs.Add(blog4); db.SaveChanges();
            }
        }