public BaseLayer GetById(long id)
        {
            var context = new OpenMuseumContext();
            var model = context.BaseLayers.Include(x => x.DataLayers).Include(x => x.Regions).First(x => x.Id == id);

            return model;
        }
 public List<Tag> GetByStringIds(string[] selectedTags)
 {
     using (var context = new OpenMuseumContext())
     {
         return context.Tags.Where(x => selectedTags.Contains(x.Id.ToString())).ToList();
     }
 }
        public IEnumerable<Tag> GetAll(out IDisposable context)
        {
            var db = new OpenMuseumContext();
            context = db;

            return db.Tags.Include(x => x.Pages);
        }
        public IEnumerable<Region> GetAll(out IDisposable context)
        {
            var db = new OpenMuseumContext();
            context = db;

            return db.Regions.Include(x => x.BaseLayer);
        }
 public List<DataLayer> GetByStringIds(string[] selectedDataLayers)
 {
     using (var context = new OpenMuseumContext())
     {
         return context.DataLayers.Where(x => selectedDataLayers.Contains(x.Id.ToString())).ToList();
     }
 }
        public IEnumerable<BaseLayer> GetAll(out IDisposable context)
        {
            var db = new OpenMuseumContext();
            context = db;

            return db.BaseLayers.Include(x => x.DataLayers).Include(x => x.Regions);
        }
        public Tag GetById(long id)
        {
            using (var context = new OpenMuseumContext())
            {
                var model = context.Tags.Include(x => x.Pages).First(x => x.Id == id);

                return model;
            }
        }
 public void Delete(long id)
 {
     using (var context = new OpenMuseumContext())
     {
         var model = context.Tags.First(x => x.Id == id);
         context.Entry(model).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
        public Region GetByPage(long pageId)
        {
            using (var context = new OpenMuseumContext())
            {
                var model = context.Regions.Include(x => x.BaseLayer).Include(x => x.Page).FirstOrDefault(x => x.PageId == pageId);

                return model;
            }
        }
        public long Update(Tag model)
        {
            using (var context = new OpenMuseumContext())
            {
                context.Entry(model).State = EntityState.Modified;
                context.SaveChanges();

                return model.Id;
            }
        }
        public long Add(Tag model)
        {
            using (var context = new OpenMuseumContext())
            {
                var result = context.Tags.Add(model);
                context.Entry(result).State = EntityState.Added;
                context.SaveChanges();

                return result.Id;
            }
        }
        public IEnumerable<DataLayer> GetAll(out IDisposable context)
        {
            var db = new OpenMuseumContext();
            context = db;

            return db.DataLayers
                .Include(x => x.BaseLayer)
                .Include(x => x.Children)
                .Include(x => x.Children.Select(y => y.Points))
                .Include(x => x.Children.Select(y => y.Points.Select(z => z.DataLayers)))
                .Include(x => x.Points)
                .Include(x => x.Points.Select(y => y.DataLayers))
                .Include(x => x.Parent);
        }
        public DataLayer GetById(long id)
        {
            var context = new OpenMuseumContext();
            var model = context.DataLayers
                .Include(x => x.BaseLayer)
                .Include(x => x.Children)
                .Include(x => x.Children.Select(y => y.Points))
                .Include(x => x.Children.Select(y => y.Points.Select(z => z.DataLayers)))
                .Include(x => x.Points)
                .Include(x => x.Points.Select(y => y.DataLayers))
                .Include(x => x.Parent)
                .First(x => x.Id == id);

            return model;
        }
        public void Delete(long id)
        {
            using (var context = new OpenMuseumContext())
            {
                var model = context.Pages.Include(x => x.Tags).First(x => x.Id == id);
                var tags = model.Tags.ToList();

                if (model.Tags != null)
                {
                    foreach (var tag in tags)
                    {
                        model.Tags.Remove(tag);
                    }
                }

                var pointsWithPage = context.Points.Where(x => x.PageId == id);
                if (pointsWithPage != null)
                {
                    foreach (var point in pointsWithPage)
                    {
                        point.PageId = null;
                        context.Entry(point).State = EntityState.Modified;
                    }
                }

                var regionsWithPage = context.Regions.Where(x => x.PageId == id);
                if (regionsWithPage != null)
                {
                    foreach (var region in regionsWithPage)
                    {
                        region.PageId = null;
                        context.Entry(region).State = EntityState.Modified;
                    }
                }

                context.Entry(model).State = EntityState.Deleted;
                context.SaveChanges();
            }
        }
        public long Add(Page model)
        {
            using (var context = new OpenMuseumContext())
            {
                if (model.Tags != null)
                {
                    var tags = model.Tags.ToList();
                    model.Tags = new List<Tag>();

                    foreach (var tag in tags)
                    {
                        context.Tags.Attach(tag);
                        model.Tags.Add(tag);
                    }
                }

                var pageResult = context.Pages.Add(model);

                context.Entry(pageResult).State = EntityState.Added;
                context.SaveChanges();

                return pageResult.Id;
            }
        }
        public long Update(Page model)
        {
            using (var context = new OpenMuseumContext())
            {
                var originalPage = context.Pages.Include(a => a.Tags)
                    .SingleOrDefault(a => a.Id == model.Id);

                if (originalPage != null)
                {
                    if (originalPage.Tags != null && model.Tags != null)
                    {
                        var originalTagIds = originalPage.Tags.Select(x => x.Id).ToList();
                        var newTagIds = model.Tags.Select(x => x.Id).ToList();
                        var tagsToAdd = context.Tags.Where(x => newTagIds.Contains(x.Id)).Where(x => !originalTagIds.Contains(x.Id)).ToList();

                        var tagsToRemove = originalPage.Tags.Select(x =>
                        {
                            if (!newTagIds.Contains(x.Id))
                            {
                                return x;
                            }

                            return null;
                        }).Where(x => x != null).ToList();

                        foreach (var tag in tagsToRemove)
                        {
                            originalPage.Tags.Remove(tag);
                        }

                        foreach (var tag in tagsToAdd)
                        {
                            originalPage.Tags.Add(tag);
                        }
                    }

                    if (originalPage.Tags == null && model.Tags != null)
                    {
                        originalPage.Tags = new List<Tag>();
                        foreach (var tag in model.Tags)
                        {
                            context.Entry(tag).State = EntityState.Modified;
                            originalPage.Tags.Add(tag);
                        }
                    }

                    if (originalPage.Tags != null && model.Tags == null)
                    {
                        foreach (var tag in originalPage.Tags.ToList())
                        {
                            originalPage.Tags.Remove(tag);
                        }
                    }

                    context.SaveChanges();
                }

                originalPage.Name = model.Name;
                originalPage.Description = model.Description;
                originalPage.Content = model.Content;
                originalPage.ExternalId = model.ExternalId;

                context.Entry(originalPage).State = EntityState.Modified;
                context.SaveChanges();

                return originalPage.Id;
            }
        }