Exemplo n.º 1
0
        // Deletes every content item and reference from exhibit with given guid.
        public void DeleteExhibit(Guid id)
        {
            var exhibitsIDs = GetChildContentItemsIds(id); // list of ids of content items

            // delete content items
            while (exhibitsIDs.Count != 0)
            {
                var e = ContentItems.Find(exhibitsIDs.First());
                ContentItems.Remove(e);
                exhibitsIDs.RemoveAt(0);
            }

            Exhibit deleteExhibit = Exhibits.Find(id);

            Exhibits.Remove(deleteExhibit);
        }
        /// <inheritdoc />
        public override void Set(IEnumerable <ContentItem> contentItems)
        {
            bool anyset = false;

            foreach (var item in contentItems)
            {
                ValidateKey(item.Key);
                ValidateLocation(item.Location);

                var contentItem = ContentItems.Find(item.Location, item.Key);

                if (contentItem == null)
                {
                    contentItem = ContentItems.Add(new ContentItem
                    {
                        Content         = item.Content,
                        Key             = item.Key,
                        Location        = item.Location,
                        Id              = GetId(item.Location, item.Key),
                        OriginalContent = item.OriginalContent
                    });

                    anyset = true;
                    _cache.AddItem(contentItem); //Add item to current cache
                }
                else
                {
                    if (contentItem.Content != item.Content)
                    {
                        contentItem.Content         = item.Content;
                        contentItem.OriginalContent = item.OriginalContent;

                        anyset = true;
                        _cache.UpdateItem(contentItem); //Update item in current cache
                    }
                }
            }

            if (anyset)
            {
                _dataContext.SaveChanges();
            }
        }
        /// <inheritdoc />
        public override ContentItem GetOrCreate(string location, string key, string defaultContent)
        {
            ValidateKey(key);
            ValidateLocation(location);
            defaultContent = defaultContent ?? "";

            //Search the cache in memory to reduce DB calls
            var cachedContentItem = ContentItemsCached.Find(x => x.Location.Equals(location) &&
                                                            x.Key.Equals(key));

            if (cachedContentItem == null)
            {
                var newContentItem = ContentItems.Add(new ContentItem
                {
                    Content         = defaultContent,
                    Key             = key,
                    Location        = location,
                    Id              = GetId(location, key),
                    OriginalContent = defaultContent
                });

                _dataContext.SaveChanges();

                _cache.AddItem(newContentItem); //Update the cache as well
            }

            if (cachedContentItem.OriginalContent != defaultContent)
            {
                var contentItem = ContentItems.Find(location, key);

                contentItem.OriginalContent = defaultContent;
                _dataContext.SaveChanges();

                _cache.UpdateItemContent(contentItem); //Update the cache as well
            }

            return(cachedContentItem);
        }
        /// <inheritdoc />
        public override void SetById(IEnumerable <KeyValuePair <string, string> > idContentPairs)
        {
            foreach (var item in idContentPairs)
            {
                ValidateId(item.Key);

                string id = item.Key;
                string location, key;
                ParseId(id, out location, out key);
                ValidateLocation(location);
                ValidateKey(key);

                var contentItem = AddIds(ContentItems.Find(location, key));

                if (contentItem != null)
                {
                    contentItem.Content = item.Value;

                    _cache.UpdateItemContent(contentItem); //Update the cached value
                }
            }

            _dataContext.SaveChanges();
        }