-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce unique Rules (Old Urls) #62
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,31 @@ public class DynamicDataStoreRepository : RedirectRuleRepository | |
private readonly DynamicDataStoreFactory _dynamicDataStoreFactory; | ||
private DynamicDataStore DynamicDataStore => _dynamicDataStoreFactory.CreateStore(typeof(RedirectRule)); | ||
|
||
private IQueryable<RedirectRule> GetAllByOldPattern(string oldPattern) | ||
{ | ||
return DynamicDataStore.Items<RedirectRule>().Where(x => x.OldPattern == oldPattern); | ||
} | ||
|
||
private IQueryable<RedirectRule> RemoveDuplicates(RedirectRule redirectRule) | ||
{ | ||
var oldPatternRedirectRules = | ||
GetAllByOldPattern(redirectRule.OldPattern) | ||
.Where(x => x.RedirectRuleType != RedirectRuleType.Regex) | ||
.OrderByDescending(x => x.CreatedOn); | ||
|
||
IQueryable<RedirectRule> duplicates = null; | ||
|
||
if (oldPatternRedirectRules.Count() > 1) | ||
{ | ||
duplicates = oldPatternRedirectRules.Skip(1); | ||
|
||
foreach (var duplicate in duplicates) | ||
{ | ||
Delete(duplicate.Id.ExternalId); | ||
} | ||
} | ||
return duplicates; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this method returns |
||
} | ||
public DynamicDataStoreRepository(DynamicDataStoreFactory dynamicDataStoreFactory) | ||
{ | ||
_dynamicDataStoreFactory = dynamicDataStoreFactory; | ||
|
@@ -27,8 +51,10 @@ public override RedirectRule GetById(Guid id) | |
} | ||
|
||
public override RedirectRule Add(RedirectRule redirectRule) | ||
{ | ||
{ | ||
DynamicDataStore.Save(redirectRule); | ||
RemoveDuplicates(redirectRule); | ||
|
||
return redirectRule; | ||
} | ||
|
||
|
@@ -46,6 +72,24 @@ public override RedirectRule Update(RedirectRule redirectRule) | |
return redirectRuleToUpdate; | ||
} | ||
|
||
public override bool RemoveAllDuplicates() | ||
{ | ||
var redirectRules = GetAll(); | ||
|
||
try | ||
{ | ||
foreach (var redirectRule in redirectRules) | ||
{ | ||
RemoveDuplicates(redirectRule); | ||
} | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel that at least what we should do here is to log error through ILogger - I know errors are ignored in every other method (ie Delete, ClearAll) but maybe we should fix that? What do you think @m-jedynak |
||
} | ||
} | ||
|
||
public override bool Delete(Guid id) | ||
{ | ||
try | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not fluent with EpiServer store concept but I wonder if this is possible to have separate methods in controller (I believe it needs to be supported by JS side of EpiServer store): Delete(GuidId), ClearAll() and RemoveAllDuplicates() ?