public void UpdateRelatedLinks(ContentItem item, RelatedLinksViewModel model) { var relatedLinks = item.As<RelatedLinksPart>().Record; var links = relatedLinks.Links; relatedLinks.RelatedLinksGroup = model.RelatedLinksGroup; relatedLinks.LinksJson = model.LinksJson; var linksJson = model.LinksJson; var oldLinks = _relatedLinksRepository.Fetch(r => r.RelatedLinksRecord.Id == relatedLinks.Id).ToList(); var updatedLinks = (List<RelatedLinkRecord>)JsonConvert.DeserializeObject(linksJson, typeof(List<RelatedLinkRecord>)); var deletedLinks = oldLinks.Where(x => !updatedLinks.Select(ui => ui.Id).Contains(x.Id)); foreach (var deletedLink in deletedLinks) { _relatedLinksRepository.Delete(deletedLink); } foreach (var updatedLink in updatedLinks) { // Check if request is coming from a new lanugage translation edit page (/translate/id) OR english edit page (/edit/id) // "!links.Any()" is true if request is coming from (/translate/id) edit page - Create new RelatedLinkRecord irrespective of Id's - Fix for missing English links when creating translations // OR // "updatedLink.Id == 0" is true if request is coming from (/edit/id) edit page as brand new link if (updatedLink.Id == 0 || !links.Any()) { updatedLink.RelatedLinksRecord = relatedLinks; _relatedLinksRepository.Create(updatedLink); } // "updatedLink.Id > 0" is true if request is coming from (/edit/id) admin page as existing links else if (updatedLink.Id > 0) { updatedLink.RelatedLinksRecord = relatedLinks; _relatedLinksRepository.Update(updatedLink); } } //Updating Links in the part manually. //NOTE: Deleting this line will have a delay of 5 mins to update Links on the edit page relatedLinks.Links = _relatedLinksRepository.Fetch(r => r.RelatedLinksRecord.Id == relatedLinks.Id).ToList(); }
public RelatedLinksViewModel BuildEditorViewModel(RelatedLinksPart part, string itemsJson = "") { var ivm = new RelatedLinksViewModel(); ivm.Links = new List<RelatedLinkRecord>(); ivm.RelatedLinksGroup = part.RelatedLinksGroup; ivm.LinksJson = part.LinksJson; foreach (var item in part.Links) { ivm.Links.Add(new RelatedLinkRecord { Id = item.Id, Type = item.Type, Title = item.Title, Url = item.Url, SortOrder = item.SortOrder, }); } ivm.Links = ivm.Links.OrderBy(x => x.SortOrder).ToList(); if (!string.IsNullOrEmpty(itemsJson)) { ivm.LinksJson = itemsJson; } return ivm; }