From 5c01526347c1686d4a8062e2cb00dafaa003b093 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Fri, 1 Oct 2021 09:38:05 +0200 Subject: [PATCH] Improved the mapping a bit to avoid redundant code --- .../Helpers/RedirectsBackOfficeHelper.cs | 121 +++++++----------- 1 file changed, 43 insertions(+), 78 deletions(-) diff --git a/src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs b/src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs index eae37b6..e97e8d1 100644 --- a/src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs +++ b/src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs @@ -120,52 +120,13 @@ public virtual Dictionary GetServerVariables() { //} public virtual object Map(RedirectsSearchResult result) { - - List items = new List(); Dictionary rootNodeLookup = new Dictionary(); Dictionary contentLookup = new Dictionary(); Dictionary mediaLookup = new Dictionary(); - foreach (var redirect in result.Items) { - - RedirectRootNodeModel rootNode = null; - if (redirect.RootKey != Guid.Empty) { - - if (!rootNodeLookup.TryGetValue(redirect.RootKey, out rootNode)) { - - if (!contentLookup.TryGetValue(redirect.RootKey, out IContent content)) { - content = _contentService.GetById(redirect.RootKey); - if (content != null) contentLookup.Add(content.Key, content); - } - var domains = content == null ? null :_domainService.GetAssignedDomains(content.Id, false).Select(x => x.DomainName).ToArray(); - rootNode = new RedirectRootNodeModel(redirect, content, domains); - - rootNodeLookup.Add(rootNode.Key, rootNode); - - } - } - - RedirectDestinationModel destination; - if (redirect.Destination.Type == RedirectDestinationType.Content) { - if (!contentLookup.TryGetValue(redirect.Destination.Key, out IContent content)) { - content = _contentService.GetById(redirect.Destination.Key); - if (content != null) contentLookup.Add(content.Key, content); - } - destination = new RedirectDestinationModel(redirect, content); - } else if (redirect.Destination.Type == RedirectDestinationType.Media) { - if (!mediaLookup.TryGetValue(redirect.Destination.Key, out IMedia media)) { - media = _mediaService.GetById(redirect.Destination.Key); - if (media != null) mediaLookup.Add(media.Key, media); - } - destination = new RedirectDestinationModel(redirect, media); - } else { - destination = new RedirectDestinationModel(redirect); - } - - items.Add(new RedirectModel(redirect, rootNode, destination)); - - } + IEnumerable items = result.Items + .Select(redirect => Map(redirect, rootNodeLookup, contentLookup, mediaLookup)); return new { result.Pagination, @@ -174,55 +135,59 @@ public virtual object Map(RedirectsSearchResult result) { } + public virtual RedirectModel Map(Redirect redirect) { + Dictionary rootNodeLookup = new(); + Dictionary contentLookup = new(); + Dictionary mediaLookup = new(); + return Map(redirect, rootNodeLookup, contentLookup, mediaLookup); + } + public virtual IEnumerable Map(IEnumerable redirects) { + Dictionary rootNodeLookup = new(); + Dictionary contentLookup = new(); + Dictionary mediaLookup = new(); + return redirects.Select(redirect => Map(redirect, rootNodeLookup, contentLookup, mediaLookup)); + } - List items = new List(); + private RedirectModel Map(Redirect redirect, Dictionary rootNodeLookup, + Dictionary contentLookup, Dictionary mediaLookup) + { - Dictionary rootNodeLookup = new Dictionary(); - Dictionary contentLookup = new Dictionary(); - Dictionary mediaLookup = new Dictionary(); + RedirectRootNodeModel rootNode = null; + if (redirect.RootKey != Guid.Empty) { - foreach (var redirect in redirects) { + if (!rootNodeLookup.TryGetValue(redirect.RootKey, out rootNode)) { - RedirectRootNodeModel rootNode = null; - if (redirect.RootKey != Guid.Empty) { - - if (!rootNodeLookup.TryGetValue(redirect.RootKey, out rootNode)) { - - if (!contentLookup.TryGetValue(redirect.RootKey, out IContent content)) { - content = _contentService.GetById(redirect.RootKey); - if (content != null) contentLookup.Add(content.Key, content); - } - var domains = content == null ? null :_domainService.GetAssignedDomains(content.Id, false).Select(x => x.DomainName).ToArray(); - rootNode = new RedirectRootNodeModel(redirect, content, domains); + if (!contentLookup.TryGetValue(redirect.RootKey, out IContent content)) { + content = _contentService.GetById(redirect.RootKey); + if (content != null) contentLookup.Add(content.Key, content); + } + var domains = content == null ? null :_domainService.GetAssignedDomains(content.Id, false).Select(x => x.DomainName).ToArray(); + rootNode = new RedirectRootNodeModel(redirect, content, domains); - rootNodeLookup.Add(rootNode.Key, rootNode); + rootNodeLookup.Add(rootNode.Key, rootNode); - } } + } - RedirectDestinationModel destination; - if (redirect.Destination.Type == RedirectDestinationType.Content) { - if (!contentLookup.TryGetValue(redirect.Destination.Key, out IContent content)) { - content = _contentService.GetById(redirect.Destination.Key); - if (content != null) contentLookup.Add(content.Key, content); - } - destination = new RedirectDestinationModel(redirect, content); - } else if (redirect.Destination.Type == RedirectDestinationType.Media) { - if (!mediaLookup.TryGetValue(redirect.Destination.Key, out IMedia media)) { - media = _mediaService.GetById(redirect.Destination.Key); - if (media != null) mediaLookup.Add(media.Key, media); - } - destination = new RedirectDestinationModel(redirect, media); - } else { - destination = new RedirectDestinationModel(redirect); + RedirectDestinationModel destination; + if (redirect.Destination.Type == RedirectDestinationType.Content) { + if (!contentLookup.TryGetValue(redirect.Destination.Key, out IContent content)) { + content = _contentService.GetById(redirect.Destination.Key); + if (content != null) contentLookup.Add(content.Key, content); } - - items.Add(new RedirectModel(redirect, rootNode, destination)); - + destination = new RedirectDestinationModel(redirect, content); + } else if (redirect.Destination.Type == RedirectDestinationType.Media) { + if (!mediaLookup.TryGetValue(redirect.Destination.Key, out IMedia media)) { + media = _mediaService.GetById(redirect.Destination.Key); + if (media != null) mediaLookup.Add(media.Key, media); + } + destination = new RedirectDestinationModel(redirect, media); + } else { + destination = new RedirectDestinationModel(redirect); } - return items; + return new RedirectModel(redirect, rootNode, destination); }