Skip to content

Commit

Permalink
Improved the mapping a bit to avoid redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Oct 1, 2021
1 parent f3fe60a commit 5c01526
Showing 1 changed file with 43 additions and 78 deletions.
121 changes: 43 additions & 78 deletions src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,52 +120,13 @@ public virtual Dictionary<string, object> GetServerVariables() {
//}

public virtual object Map(RedirectsSearchResult result) {

List<RedirectModel> items = new List<RedirectModel>();

Dictionary<Guid, RedirectRootNodeModel> rootNodeLookup = new Dictionary<Guid, RedirectRootNodeModel>();
Dictionary<Guid, IContent> contentLookup = new Dictionary<Guid, IContent>();
Dictionary<Guid, IMedia> mediaLookup = new Dictionary<Guid, IMedia>();

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<RedirectModel> items = result.Items
.Select(redirect => Map(redirect, rootNodeLookup, contentLookup, mediaLookup));

return new {
result.Pagination,
Expand All @@ -174,55 +135,59 @@ public virtual object Map(RedirectsSearchResult result) {

}

public virtual RedirectModel Map(Redirect redirect) {
Dictionary<Guid, RedirectRootNodeModel> rootNodeLookup = new();
Dictionary<Guid, IContent> contentLookup = new();
Dictionary<Guid, IMedia> mediaLookup = new();
return Map(redirect, rootNodeLookup, contentLookup, mediaLookup);
}

public virtual IEnumerable<RedirectModel> Map(IEnumerable<Redirect> redirects) {
Dictionary<Guid, RedirectRootNodeModel> rootNodeLookup = new();
Dictionary<Guid, IContent> contentLookup = new();
Dictionary<Guid, IMedia> mediaLookup = new();
return redirects.Select(redirect => Map(redirect, rootNodeLookup, contentLookup, mediaLookup));
}

List<RedirectModel> items = new List<RedirectModel>();
private RedirectModel Map(Redirect redirect, Dictionary<Guid, RedirectRootNodeModel> rootNodeLookup,
Dictionary<Guid, IContent> contentLookup, Dictionary<Guid, IMedia> mediaLookup)
{

Dictionary<Guid, RedirectRootNodeModel> rootNodeLookup = new Dictionary<Guid, RedirectRootNodeModel>();
Dictionary<Guid, IContent> contentLookup = new Dictionary<Guid, IContent>();
Dictionary<Guid, IMedia> mediaLookup = new Dictionary<Guid, IMedia>();
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);

}

Expand Down

0 comments on commit 5c01526

Please sign in to comment.