Esempio n. 1
0
        public BlogPostCustom GetBlogPost(string slug)
        {
            //find blog by slug value
            var blogPost = con.BlogPosts.Where(x => x.Slug == slug).FirstOrDefault();

            if (blogPost != null)
            {
                List <string> tags = new List <string>();
                //find all tags for the specified blog

                var tagIDs = con.BlogPostsTags.Where(t => t.BlogPostId == blogPost.Id).Select(x => x.TagId).ToList();
                //add tags for api's customized model

                foreach (var tag in tagIDs)
                {
                    var tagy = con.Tags.Find(tag);
                    tags.Add(tagy.TagName);
                }
                var blog = new BlogPostCustom.BPCblog
                {
                    slug        = slug,
                    title       = blogPost.Title,
                    description = blogPost.Description,
                    body        = blogPost.Body,
                    createdAt   = blogPost.CreatedAt,
                    updatedAt   = blogPost.UpdatetAt,
                    tagList     = tags
                };
                var blogModel = new BlogPostCustom();
                blogModel.blogPost = blog;
                return(blogModel);
            }

            return(new BlogPostCustom("Post with this slug doesn't exist"));
        }
Esempio n. 2
0
        public BlogPostCustom UpdateBlogPost(string slug, UpdatePostCustom model)
        {
            //find post by slug
            var findbySlug = con.BlogPosts.Where(x => x.Slug == slug).FirstOrDefault();

            //if post with this slug value doesn't exist or title in the model is not provided
            if (findbySlug == null || model.blogPost.title == null)
            {
                return(new BlogPostCustom("Slug doesn't exist or model title is not provided"));
            }

            var makeSlug = SlugRefactoring.Refactor(model.blogPost.title);

            findbySlug.Title = model.blogPost.title;
            findbySlug.Slug  = makeSlug;

            //if body is provided then change, if not keep its value as it is
            if (model.blogPost.body != null)
            {
                findbySlug.Body = model.blogPost.body;
            }

            //if description is provided then change, if not keep its value as it is
            if (model.blogPost.description != null)
            {
                findbySlug.Description = model.blogPost.description;
            }
            //while updating we need to change object update time also.
            findbySlug.UpdatetAt = DateTime.Now;
            con.SaveChanges();

            var        tags  = new List <string>();
            List <int> tagID = con.BlogPostsTags.Where(t => t.BlogPostId == findbySlug.Id).Select(x => x.TagId).ToList();

            foreach (var tag in tagID)
            {
                var tagy = con.Tags.Find(tag);
                tags.Add(tagy.TagName);
            }

            //returns updated custom json object
            var customBlog = new BlogPostCustom.BPCblog
            {
                slug        = findbySlug.Slug,
                title       = findbySlug.Title,
                body        = findbySlug.Body,
                description = findbySlug.Description,
                createdAt   = findbySlug.CreatedAt,
                updatedAt   = findbySlug.UpdatetAt,
                tagList     = tags
            };

            var blog = new BlogPostCustom();

            blog.blogPost = customBlog;
            return(blog);
        }
Esempio n. 3
0
        public BlogPostCustom AddBlogPost(CreateBlogPostCustom addBlogPost)
        {
            if (addBlogPost != null)
            {
                string makeSlugFromTitle = null;
                makeSlugFromTitle = SlugRefactoring.Refactor(addBlogPost.blogPost.title);

                //if slug already exists in database return an empty object
                if (con.BlogPosts.Where(x => x.Slug == makeSlugFromTitle).Any())
                {
                    return(new BlogPostCustom("Object with this slug already exists"));
                }
                var model = new BlogPost()
                {
                    Slug        = makeSlugFromTitle,
                    Title       = addBlogPost.blogPost.title,
                    Description = addBlogPost.blogPost.description,
                    Body        = addBlogPost.blogPost.body,
                    CreatedAt   = DateTime.Now,
                    UpdatetAt   = DateTime.Now
                };
                con.Add(model);
                con.SaveChanges();

                //if new object has tags
                if (addBlogPost.blogPost.tagList != null)
                {
                    foreach (var tag in addBlogPost.blogPost.tagList)
                    {
                        var newTag = new Tag
                        {
                            TagName = tag
                        };
                        //if tag doesn't exist in database, add a new tag in a tag table
                        if (!con.Tags.Where(x => x.TagName == tag).Any())
                        {
                            con.Add(newTag);
                            con.SaveChanges();
                        }

                        // if exists, then find id and add in many-to-many table (BlogPostTag)
                        else
                        {
                            newTag.Id = con.Tags.Where(x => x.TagName == tag).FirstOrDefault().Id;
                        }
                        var blogtag = new BlogPostTag();
                        blogtag.BlogPostId = model.Id;
                        blogtag.TagId      = newTag.Id;
                        con.Add(blogtag);
                        con.SaveChanges();
                    }
                }

                //custom object for json return
                var bpc = new BlogPostCustom.BPCblog()
                {
                    title       = addBlogPost.blogPost.title,
                    description = addBlogPost.blogPost.description,
                    body        = addBlogPost.blogPost.body,
                    slug        = makeSlugFromTitle,
                    createdAt   = DateTime.Now,
                    updatedAt   = DateTime.Now,
                    tagList     = addBlogPost.blogPost.tagList
                };
                var bpcM = new BlogPostCustom()
                {
                    blogPost = bpc
                };
                return(bpcM);
            }

            return(new BlogPostCustom("Object provided as a parameter is null"));
        }