Skip to content

Commit

Permalink
converted namespace to file scoped
Browse files Browse the repository at this point in the history
  • Loading branch information
Valdis Iljuconoks committed Nov 18, 2024
1 parent e862a62 commit b97ff34
Show file tree
Hide file tree
Showing 37 changed files with 858 additions and 897 deletions.
29 changes: 14 additions & 15 deletions src/Geta.Optimizely.ProductFeed.Csv/CsvFeedDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
using System;
using Geta.Optimizely.ProductFeed.Configuration;

namespace Geta.Optimizely.ProductFeed.Csv
namespace Geta.Optimizely.ProductFeed.Csv;

public class CsvFeedDescriptor<TEntity> : FeedDescriptor
{
public class CsvFeedDescriptor<TEntity> : FeedDescriptor
{
public CsvFeedDescriptor() : base("csv", "csv-feed", "text/plain") { }
public CsvFeedDescriptor() : base("csv", "csv-feed", "text/plain") { }

public Type CsvEntityType { get; set; }
public Type CsvEntityType { get; set; }

public CsvFeedDescriptor<TEntity> SetConverter<TConverter>() where TConverter : IProductFeedConverter<TEntity>
{
Converter = typeof(TConverter);
return this;
}
public CsvFeedDescriptor<TEntity> SetConverter<TConverter>() where TConverter : IProductFeedConverter<TEntity>
{
Converter = typeof(TConverter);
return this;
}

public CsvFeedDescriptor<TEntity> SetFilter<TFilter>() where TFilter : IProductFeedFilter<TEntity>
{
Filter = typeof(TFilter);
return this;
}
public CsvFeedDescriptor<TEntity> SetFilter<TFilter>() where TFilter : IProductFeedFilter<TEntity>
{
Filter = typeof(TFilter);
return this;
}
}
73 changes: 36 additions & 37 deletions src/Geta.Optimizely.ProductFeed.Csv/CsvFeedExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,43 @@
using EPiServer.Web;
using Geta.Optimizely.ProductFeed.Models;

namespace Geta.Optimizely.ProductFeed.Csv
namespace Geta.Optimizely.ProductFeed.Csv;

public class CsvFeedExporter<TEntity> : AbstractFeedContentExporter<TEntity>
{
public class CsvFeedExporter<TEntity> : AbstractFeedContentExporter<TEntity>
private readonly CsvFeedDescriptor<TEntity> _descriptor;
private CsvWriter _writer;

public CsvFeedExporter(CsvFeedDescriptor<TEntity> descriptor)
{
_descriptor = descriptor;
}

public override void BeginExport(HostDefinition host, CancellationToken cancellationToken)
{
base.BeginExport(host, cancellationToken);
_writer = new CsvWriter(new StreamWriter(_buffer), new CsvConfiguration(CultureInfo.InvariantCulture));
_writer.WriteHeader(_descriptor.CsvEntityType);
_writer.NextRecord();
}

public override object ConvertEntry(TEntity entity, HostDefinition host, CancellationToken cancellationToken)
{
private readonly CsvFeedDescriptor<TEntity> _descriptor;
private CsvWriter _writer;

public CsvFeedExporter(CsvFeedDescriptor<TEntity> descriptor)
{
_descriptor = descriptor;
}

public override void BeginExport(HostDefinition host, CancellationToken cancellationToken)
{
base.BeginExport(host, cancellationToken);
_writer = new CsvWriter(new StreamWriter(_buffer), new CsvConfiguration(CultureInfo.InvariantCulture));
_writer.WriteHeader(_descriptor.CsvEntityType);
_writer.NextRecord();
}

public override object ConvertEntry(TEntity entity, HostDefinition host, CancellationToken cancellationToken)
{
return Converter.Convert(entity, host);
}

public override byte[] SerializeEntry(object value, CancellationToken cancellationToken)
{
_writer.WriteRecord(value);
_writer.NextRecord();

return Array.Empty<byte>();
}

public override ICollection<FeedEntity> FinishExport(HostDefinition host, CancellationToken cancellationToken)
{
_writer.Flush();

return base.FinishExport(host, cancellationToken);
}
return Converter.Convert(entity, host);
}

public override byte[] SerializeEntry(object value, CancellationToken cancellationToken)
{
_writer.WriteRecord(value);
_writer.NextRecord();

return Array.Empty<byte>();
}

public override ICollection<FeedEntity> FinishExport(HostDefinition host, CancellationToken cancellationToken)
{
_writer.Flush();

return base.FinishExport(host, cancellationToken);
}
}
33 changes: 16 additions & 17 deletions src/Geta.Optimizely.ProductFeed.Csv/ProductFeedOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
using EPiServer.Commerce.Catalog.ContentTypes;
using Geta.Optimizely.ProductFeed.Configuration;

namespace Geta.Optimizely.ProductFeed.Csv
namespace Geta.Optimizely.ProductFeed.Csv;

public static class ProductFeedOptionsExtensions
{
public static class ProductFeedOptionsExtensions
public static ProductFeedOptions<TEntity> AddCsvExport<TEntity>(
this ProductFeedOptions<TEntity> options,
Action<CsvFeedDescriptor<TEntity>> setupAction)
{
public static ProductFeedOptions<TEntity> AddCsvExport<TEntity>(
this ProductFeedOptions<TEntity> options,
Action<CsvFeedDescriptor<TEntity>> setupAction)
{
var descriptor = new CsvFeedDescriptor<TEntity>();
var descriptor = new CsvFeedDescriptor<TEntity>();

descriptor.SetExporter<CsvFeedExporter<TEntity>, TEntity>();
descriptor.SetSiteUrlBuilder<DefaultSiteUrlBuilder>();
descriptor.SetExporter<CsvFeedExporter<TEntity>, TEntity>();
descriptor.SetSiteUrlBuilder<DefaultSiteUrlBuilder>();

setupAction(descriptor);
setupAction(descriptor);

if (descriptor.CsvEntityType == null)
{
throw new ArgumentException("CsvEntityType is not set");
}
if (descriptor.CsvEntityType == null)
{
throw new ArgumentException("CsvEntityType is not set");
}

options.Add(descriptor);
options.Add(descriptor);

return options;
}
return options;
}
}
15 changes: 7 additions & 8 deletions src/Geta.Optimizely.ProductFeed.Google/EncodedStringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
using System.IO;
using System.Text;

namespace Geta.Optimizely.ProductFeed.Google
namespace Geta.Optimizely.ProductFeed.Google;

public sealed class EncodedStringWriter : StringWriter
{
public sealed class EncodedStringWriter : StringWriter
public EncodedStringWriter(Encoding encoding)
{
public EncodedStringWriter(Encoding encoding)
{
Encoding = encoding;
}

public override Encoding Encoding { get; }
Encoding = encoding;
}

public override Encoding Encoding { get; }
}
27 changes: 13 additions & 14 deletions src/Geta.Optimizely.ProductFeed.Google/GoogleFeedDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@

using Geta.Optimizely.ProductFeed.Configuration;

namespace Geta.Optimizely.ProductFeed.Google
namespace Geta.Optimizely.ProductFeed.Google;

public class GoogleFeedDescriptor<TEntity> : FeedDescriptor
{
public class GoogleFeedDescriptor<TEntity> : FeedDescriptor
{
public GoogleFeedDescriptor() : base("google", "/googleproductfeed", "application/xml") { }
public GoogleFeedDescriptor() : base("google", "/googleproductfeed", "application/xml") { }

public GoogleFeedDescriptor<TEntity> SetConverter<TConverter>() where TConverter : IProductFeedConverter<TEntity>
{
Converter = typeof(TConverter);
return this;
}
public GoogleFeedDescriptor<TEntity> SetConverter<TConverter>() where TConverter : IProductFeedConverter<TEntity>
{
Converter = typeof(TConverter);
return this;
}

public GoogleFeedDescriptor<TEntity> SetFilter<TFilter>() where TFilter : IProductFeedFilter<TEntity>
{
Filter = typeof(TFilter);
return this;
}
public GoogleFeedDescriptor<TEntity> SetFilter<TFilter>() where TFilter : IProductFeedFilter<TEntity>
{
Filter = typeof(TFilter);
return this;
}
}
71 changes: 35 additions & 36 deletions src/Geta.Optimizely.ProductFeed.Google/GoogleFeedExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,51 @@
using Geta.Optimizely.ProductFeed.Google.Models;
using Geta.Optimizely.ProductFeed.Models;

namespace Geta.Optimizely.ProductFeed.Google
namespace Geta.Optimizely.ProductFeed.Google;

public class GoogleFeedExporter<TEntity> : AbstractFeedContentExporter<TEntity>
{
public class GoogleFeedExporter<TEntity> : AbstractFeedContentExporter<TEntity>
private readonly List<Entry> _entries = new();

public override void BeginExport(HostDefinition host, CancellationToken cancellationToken)
{
private readonly List<Entry> _entries = new();
_entries.Clear();
base.BeginExport(host, cancellationToken);
}

public override void BeginExport(HostDefinition host, CancellationToken cancellationToken)
{
_entries.Clear();
base.BeginExport(host, cancellationToken);
}
public override ICollection<FeedEntity> FinishExport(HostDefinition host, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

public override ICollection<FeedEntity> FinishExport(HostDefinition host, CancellationToken cancellationToken)
var f = new Feed
{
cancellationToken.ThrowIfCancellationRequested();
Updated = DateTime.UtcNow,
Title = "Google Product Feed",
Link = host?.Url.ToString().TrimEnd('/') + '/' + Descriptor.FileName.TrimStart('/'),
Entries = _entries.Where(e => e != null).ToList()
};

var f = new Feed
return new[]
{
new FeedEntity
{
Updated = DateTime.UtcNow,
Title = "Google Product Feed",
Link = host?.Url.ToString().TrimEnd('/') + '/' + Descriptor.FileName.TrimStart('/'),
Entries = _entries.Where(e => e != null).ToList()
};
CreatedUtc = f.Updated, Link = f.Link, FeedBytes = ObjectXmlSerializer.Serialize(f, typeof(Feed))
}
};
}

return new[]
{
new FeedEntity
{
CreatedUtc = f.Updated, Link = f.Link, FeedBytes = ObjectXmlSerializer.Serialize(f, typeof(Feed))
}
};
}

public override object ConvertEntry(TEntity entity, HostDefinition host, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var entry = (Entry)Converter.Convert(entity, host);
public override object ConvertEntry(TEntity entity, HostDefinition host, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var entry = (Entry)Converter.Convert(entity, host);

_entries.Add(entry);
_entries.Add(entry);

return null;
}
return null;
}

public override byte[] SerializeEntry(object value, CancellationToken cancellationToken)
{
return Array.Empty<byte>();
}
public override byte[] SerializeEntry(object value, CancellationToken cancellationToken)
{
return Array.Empty<byte>();
}
}
Loading

0 comments on commit b97ff34

Please sign in to comment.