Example #1
0
        // GET: Posts/Create
        public ActionResult Create()
        {
            SecurityController.CheckAuth(this);

            var post = new Post() { PostDate = DateTime.Today, Categories = new List<Category>() };
            PopulateAssignedCategories(post);
            return View(post);
        }
Example #2
0
 public JsonPost(Post post)
 {
     PostDate = post.PostDate;
     Heading = post.Heading;
     Body = post.Body;
     foreach (Category c in post.Categories)
     {
         _Categories.Add(c.Name);
     }
 }
Example #3
0
        private void UpdateAssignedCategories(string[] selectedCategories, Post postToUpdate)
        {
            if (selectedCategories == null)
            {
                postToUpdate.Categories = new List<Category>();
                return;
            }

            var selectedCategoriesHS = new HashSet<string>(selectedCategories);
            var postCategories = new HashSet<int>(postToUpdate.Categories.Select(c => c.ID));

            foreach(var category in db.Categories)
            {
                if (selectedCategoriesHS.Contains(category.ID.ToString()))
                {
                    if (!postCategories.Contains(category.ID))
                    {
                        postToUpdate.Categories.Add(category);
                    }
                }
                else
                {
                    if (postCategories.Contains(category.ID))
                    {
                        postToUpdate.Categories.Remove(category);
                    }
                }
            }
        }
Example #4
0
        private void PopulateHashTags(Post post)
        {
            var mHash = Regex.Matches(post.Body + " ", @"(#.*?)\W");
            if (mHash.Count == 0)
            {
                post.HashTags = new List<HashTag>();
                return;
            }

            var newTags = new HashSet<string>();
            foreach (Match m in mHash)
                newTags.Add(m.Groups[1].Value.ToLower());

            HashSet<string> postTags;
            if (post.HashTags == null)
            {
                postTags = new HashSet<string>();
                post.HashTags = new List<HashTag>();
            }
            else
            {
                postTags = new HashSet<string>(post.HashTags.Select(h => h.Name.ToLower()));
            }

            if (newTags.SetEquals(postTags))
                return;

            foreach(string tag in postTags)
            {
                if (!newTags.Contains(tag))
                {
                    post.HashTags.Remove(db.HashTags.Single(h => h.Name == tag));
                }
            }

            foreach(string tag in newTags)
            {
                if (!postTags.Contains(tag))
                {
                    var dbHash = db.HashTags.SingleOrDefault(h => h.Name == tag);
                    if (dbHash == null)
                    {
                        post.HashTags.Add(new HashTag() { Name = tag });
                    }
                    else
                    {
                        post.HashTags.Add(dbHash);
                    }
                }
            }
        }
Example #5
0
        private void PopulateAssignedCategories(Post post)
        {
            var allCategories = db.Categories;
            var postCategories = new HashSet<int>(post.Categories.Select(c => c.ID));
            var viewModel = new List<AssignedCategoryData>();

            foreach (var category in allCategories)
            {
                viewModel.Add(new AssignedCategoryData
                {
                    ID = category.ID,
                    Name = category.Name,
                    Assigned = postCategories.Contains(category.ID)
                });
            }
            ViewBag.Categories = viewModel;
        }
Example #6
0
        public void Import(string json)
        {
            SecurityController.CheckAuth(this);

            List<JsonPost> jPosts = JsonConvert.DeserializeObject<List<JsonPost>>(json);

            foreach (JsonPost jPost in jPosts)
            {
                Post post = new Post { PostDate = jPost.PostDate, Heading = jPost.Heading, Body = jPost.Body };
                if (jPost.Categories.Count() > 0)
                {
                    post.Categories = new List<Category>();
                    foreach (var category in jPost.Categories)
                    {
                        Category categoryToAdd = db.Categories.Single(c => c.Name == category);
                        post.Categories.Add(categoryToAdd);
                    }
                }
                db.Posts.Add(post);
            }
            db.SaveChanges();

            RedirectToAction("Index");
        }