public PageApiViewModel(Page page)
 {
     Id = page.Id;
     Name = page.Name;
     Description = page.Description;
     Content = page.Content;
     Tags = page.Tags?.Select(x => new TagAPIViewModel(x)).ToList();
 }
 public EditPageViewModel(Page page)
 {
     Id = page.Id;
     Name = page.Name;
     Description = page.Description;
     Content = page.Content;
     ExternalId = page.ExternalId;
     if (page.Tags != null)
         Tags = page.Tags.Select(x => new TagViewModel(x)).ToList();
 }
        public PageViewModel(Page page, Point point, Region region)
        {
            Id = page.Id;
            Name = page.Name;
            Description = page.Description;
            Content = page.Content;
            ExternalId = page.ExternalId;

            Region = region;
            Point = point;

            PointId = point?.Id;
            PointName = point?.Name;
            RegionId = region?.Id;
            RegionName = region?.Name;

            if (page.Tags != null)
                Tags = page.Tags.Select(x => new TagViewModel(x)).ToList();
        }
        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 ActionResult Add(PageViewModel model)
        {
            try
            {
                var tagsRepository = new TagsRepository();
                var pointsRepository = new PointsRepository();
                var pagesRepository = new PagesRepository();

                var page = new Page()
                {
                    Name = model.Name,
                    Description = model.Description,
                    Content = model.Content,
                    ExternalId = model.ExternalId
                };

                if (model.SelectedTags != null)
                {
                    var tags = tagsRepository.GetByStringIds(model.SelectedTags);
                    page.Tags = new List<Tag>(tags);
                }
                else
                {
                    page.Tags = null;
                }

                var pageId = pagesRepository.Add(page);
                if (!string.IsNullOrEmpty(model.Point?.Name))
                {
                    model.Point.PageId = pageId;
                    pointsRepository.Add(model.Point);
                }

                if (model.RegionId != null)
                {
                    var regionsRepository = new RegionsRepository();

                    var region = regionsRepository.GetById(model.RegionId.Value);
                    region.Page = null;
                    region.PageId = pageId;

                    regionsRepository.Update(region);
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
        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;
            }
        }