Exemplo n.º 1
0
        private void SaveCategories(int?postID, IFormCollection form)
        {
            var currentCategories = _categoryService.GetCategories(postID ?? 0);// Categories currently assigned to the Post

            if (form["selCategories"] != "")
            {
                string[] newCategories = form["selCategories"].ToString().Split(',');// new associations

                // add new associations
                foreach (var newCategory in newCategories)
                {
                    // check if we already have this association
                    var currentCategory = currentCategories.Where(p => p.CategoryId == newCategory.ToInt()).FirstOrDefault();
                    if (currentCategory != null)
                    {
                        currentCategories.Remove(currentCategory);
                        continue;
                    }

                    var relationship = new SlickCMS.Data.Entities.Relationship
                    {
                        PostId     = postID ?? 0,
                        CategoryId = newCategory.ToInt(),
                    };
                    _context.Set <Data.Entities.Relationship>().Add(relationship);
                }

                // remove old associations
                foreach (var currentCategory in currentCategories)
                {
                    var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.CategoryId == currentCategory.CategoryId && r.PostId == postID);
                    _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship);
                }
            }
            else
            {
                foreach (var currentCategory in currentCategories)
                {
                    var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.CategoryId == currentCategory.CategoryId && r.PostId == postID);
                    _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship);
                }
            }

            _context.SaveChanges();
        }
Exemplo n.º 2
0
        private void SaveTags(int?postID, IFormCollection form)
        {
            //    replace " " with "-", so "#star wars" becomes "#star-wars"
            //    all tags begin with #, but strip # out when saving to DB

            var currentTags = _tagService.GetTags(postID ?? 0);// Tags currently assigned to the post

            if (form["txtTags"] != "")
            {
                string[] newTags = form["txtTags"].ToString().Split('#');

                foreach (var newTag in newTags)
                {
                    if (newTag.Length == 0)
                    {
                        continue;
                    }

                    string formattedTag = newTag.Trim().ToLower().Replace(" ", "-");

                    // check if we already have this association
                    var currentTag = currentTags.Where(p => p.Name == formattedTag).FirstOrDefault();
                    if (currentTag != null)
                    {
                        currentTags.Remove(currentTag);
                        continue;
                    }

                    // check if the tag already exists
                    var tag = _tagService.Get(p => p.Name == formattedTag);
                    if (tag == null)
                    {
                        // add a new tag
                        tag = new Data.Entities.Tag
                        {
                            Name = formattedTag,
                        };
                        _context.Set <Data.Entities.Tag>().Add(tag);
                        _context.SaveChanges();
                    }

                    // save the association
                    var relationship = new SlickCMS.Data.Entities.Relationship
                    {
                        PostId = postID ?? 0,
                        TagId  = tag.TagId,
                    };
                    _context.Set <Data.Entities.Relationship>().Add(relationship);
                }

                // remove old associations
                foreach (var currentTag in currentTags)
                {
                    var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.CategoryId == currentTag.TagId && r.PostId == postID);
                    _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship);
                }
            }
            else
            {
                foreach (var currentTag in currentTags)
                {
                    var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.TagId == currentTag.TagId && r.PostId == postID);
                    _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship);
                }
            }

            _context.SaveChanges();
        }