Skip to content

Commit

Permalink
fix: multi-site proper feed loader. added support for querystring
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed May 31, 2022
1 parent f2ab739 commit fe2d482
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>2.2.0</Version>
<PackageVersion>2.2.0</PackageVersion>
<Version>2.2.1</Version>
<PackageVersion>2.2.1</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed.Csv</PackageId>
<Title>Geta Optimizely Csv Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>2.2.0</Version>
<PackageVersion>2.2.0</PackageVersion>
<Version>2.2.1</Version>
<PackageVersion>2.2.1</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed.Google</PackageId>
<Title>Geta Optimizely Google Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>2.2.0</Version>
<PackageVersion>2.2.0</PackageVersion>
<Version>2.2.1</Version>
<PackageVersion>2.2.1</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed</PackageId>
<Title>Geta Optimizely Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion src/Geta.Optimizely.ProductFeed/ProductFeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IActionResult Get()
return NotFound("Feed not found");
}

var descriptor = _feedRepository.FindDescriptorByUrl(siteHost.PathAndQuery.TrimStart('/'));
var descriptor = _feedRepository.FindDescriptorByUri(siteHost);

return Content(Encoding.UTF8.GetString(feedInfo.FeedBytes), descriptor.MimeType);
}
Expand Down
27 changes: 16 additions & 11 deletions src/Geta.Optimizely.ProductFeed/Repositories/FeedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ public FeedRepository(FeedApplicationDbContext applicationDbContext, IEnumerable
_descriptors = descriptors;
}

public FeedEntity GetLatestFeed(Uri siteHost)
public FeedEntity GetLatestFeed(Uri siteUri)
{
if (siteHost == null)
if (siteUri == null)
{
throw new ArgumentNullException(nameof(siteHost));
throw new ArgumentNullException(nameof(siteUri));
}

// we need to do client-side eval because string.Equals with comparison is not supported
var feedContent = _applicationDbContext
.FeedData
.Where(f => f.Link.Contains(siteHost.AbsolutePath))
.OrderByDescending(f => f.CreatedUtc)
.FirstOrDefault();
.ToList();

return feedContent;
return feedContent.FirstOrDefault(f => f.Link.Equals(GetAbsoluteUrlWithoutQuery(siteUri).AbsoluteUri.TrimEnd('/'),
StringComparison.InvariantCultureIgnoreCase));
}

public void Save(ICollection<FeedEntity> feedData)
Expand All @@ -44,9 +44,11 @@ public void Save(ICollection<FeedEntity> feedData)
return;
}

var feeds = _applicationDbContext.FeedData.ToList();

foreach (var data in feedData)
{
var found = _applicationDbContext.FeedData.FirstOrDefault(f => f.Link.Contains(data.Link));
var found = feeds.FirstOrDefault(f => f.Link.Equals(data.Link, StringComparison.InvariantCultureIgnoreCase));

if (found != null)
{
Expand All @@ -63,11 +65,14 @@ public void Save(ICollection<FeedEntity> feedData)
}
}

public FeedDescriptor FindDescriptorByUrl(string pathAndQuery)
public FeedDescriptor FindDescriptorByUri(Uri siteUri)
{
var path = pathAndQuery.TrimStart('/');
var path = GetAbsoluteUrlWithoutQuery(siteUri).AbsolutePath.Trim('/');

return _descriptors.FirstOrDefault(d => d.FileName.TrimStart('/').Equals(path, StringComparison.InvariantCultureIgnoreCase));
return _descriptors.FirstOrDefault(d => d.FileName.Trim('/').Equals(path, StringComparison.InvariantCultureIgnoreCase));
}

private Uri GetAbsoluteUrlWithoutQuery(Uri siteUri)
=> new UriBuilder(siteUri) { Query = string.Empty }.Uri;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Geta.Optimizely.ProductFeed.Repositories
{
public interface IFeedRepository
{
FeedEntity GetLatestFeed(Uri siteHost);
FeedEntity GetLatestFeed(Uri siteUri);

void Save(ICollection<FeedEntity> feedData);

FeedDescriptor FindDescriptorByUrl(string pathAndQuery);
FeedDescriptor FindDescriptorByUri(Uri siteUri);
}
}

0 comments on commit fe2d482

Please sign in to comment.