Exemplo n.º 1
0
        public ActionResult Create([Bind(Exclude = "Tags")] Core.Bookmark bookmark, string tags)
        {
            if (ModelState.IsValid)
            {
                this.service.Add(bookmark, tags);
                this.service.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(bookmark));
        }
Exemplo n.º 2
0
        public ActionResult Edit([Bind(Exclude = "Article.URL,Tags")] Core.Bookmark postedBookmark, string tags)
        {
            var bookmark = this.service.GetOne(postedBookmark.Id);

            if (ModelState.IsValid)
            {
                this.service.Update(postedBookmark, tags);
                this.service.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(bookmark));
        }
Exemplo n.º 3
0
        public void Update(Core.Bookmark postedBookmark, string tags)
        {
            var bookmark = this.GetOne(postedBookmark.Id);

            bookmark.Article.Title = postedBookmark.Article.Title;
            bookmark.Description   = postedBookmark.Description;

            var bookmarkTags = string.IsNullOrEmpty(tags) ? null : tags.Split(',').ToList();

            if (bookmarkTags != null)
            {
                foreach (var tag in bookmarkTags.ToList())
                {
                    var currentTagsOfBookmark = this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.ToList();
                    if (!currentTagsOfBookmark.Select(t => t.Text).Contains(tag))
                    {
                        // check if the tag is new or already exists in the database
                        var existingTag = this.dataContext.Tags.SingleOrDefault(t => t.Text == tag);
                        if (existingTag != null)
                        {
                            this.dataContext.Tags.Attach(existingTag);
                            this.dataContext.Entry(existingTag).State = EntityState.Unchanged;
                            bookmark.Tags.Add(existingTag);
                        }
                        else
                        {
                            var newTag = new Tag {
                                Text = tag
                            };
                            this.dataContext.Tags.Attach(newTag);
                            this.dataContext.Entry(newTag).State = EntityState.Added;
                            bookmark.Tags.Add(newTag);
                        }
                    }
                }
            }

            // if there are no tags posted, then delete all tags associated with the bookmark
            if (bookmarkTags == null)
            {
                // find a way to simplify, bookmark.Tags  = null; perhaps or something equivalent?
                // I think the tags are being lazy loaded, accessed by the enumerator
                // load all related tags during load of the bookmark, reasearch how to load related entities ===>  var bookmark = this.dataContext.Bookmarks.Include(t => t.Tags).Single(b => b.Id == postedBookmark.Id);
                var tagsToRemove = new List <Tag>();
                foreach (var tag in bookmark.Tags)
                {
                    tagsToRemove.Add(tag);
                }
                tagsToRemove.ForEach(t => bookmark.Tags.Remove(t));
            }
            else
            {
                // cannot remove on the same collection while iterating through it(), this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.Remove, perhpas put inside a list and then remove range, or assign Bookmark.Tags = new HashSet or List
                // check if I can assign List to an ICollection

                // to do: from the comments above, change to: var tags = this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.Select(t => t.Text).ToList()
                // iterate on that collection, remove  var tagsToRemove, instead do something like => context.tags.remove(tag)
                var tagsToRemove = new List <Tag>();
                foreach (var tag in this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.Select(t => t.Text))
                {
                    if (!bookmarkTags.Contains(tag))
                    {
                        var tagToRemove = this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.SingleOrDefault(t => t.Text == tag);

                        if (tagToRemove != null)
                        {
                            //this.dataContext.Bookmarks.Single(b => b.Id == bookmark.Id).Tags.Remove(tagToRemove);
                            this.dataContext.Tags.Attach(tagToRemove);
                            this.dataContext.Entry(tagToRemove).State = EntityState.Unchanged;
                            tagsToRemove.Add(tagToRemove);
                        }
                    }
                }

                tagsToRemove.ForEach(t => bookmark.Tags.Remove(t));
            }

            this.dataContext.Bookmarks.Attach(bookmark);
            this.dataContext.Entry(bookmark).State = EntityState.Modified;
        }