-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a288edd
commit d83c958
Showing
18 changed files
with
371 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ public enum CommandLineOptionType | |
Unknown, | ||
Lookup, | ||
Import, | ||
Export, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using MusicCatalogue.Entities.DataExchange; | ||
|
||
namespace MusicCatalogue.Entities.Interfaces | ||
{ | ||
public interface IExporter | ||
{ | ||
event EventHandler<TrackDataExchangeEventArgs>? TrackExport; | ||
|
||
Task Export(string file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using MusicCatalogue.Entities.DataExchange; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
|
||
namespace MusicCatalogue.Logic.DataExchange | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class CsvExporter : DataExportBase, IExporter | ||
{ | ||
private StreamWriter? _writer = null; | ||
|
||
#pragma warning disable CS8618 | ||
internal CsvExporter(IMusicCatalogueFactory factory) : base(factory) | ||
{ | ||
} | ||
#pragma warning restore CS8618 | ||
|
||
/// <summary> | ||
/// Export the collection to a CSV file | ||
/// </summary> | ||
/// <param name="sightings"></param> | ||
/// <param name="file"></param> | ||
public async Task Export(string file) | ||
{ | ||
// Open the CSV file | ||
using (_writer = new(file, false, Encoding.UTF8)) | ||
{ | ||
// Iterate over the collection, calling the row addition methods | ||
await IterateOverCollection(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Add the headers to the CSV file | ||
/// </summary> | ||
/// <param name="headers"></param> | ||
protected override void AddHeaders(IEnumerable<string> headers) | ||
{ | ||
var csvHeaders = string.Join(",", headers); | ||
_writer!.WriteLine(csvHeaders); | ||
} | ||
|
||
/// <summary> | ||
/// Add a track to the CSV file | ||
/// </summary> | ||
/// <param name="track"></param> | ||
/// <param name="_"></param> | ||
protected override void AddTrack(FlattenedTrack track, int _) | ||
Check warning on line 49 in src/MusicCatalogue.Logic/DataExchange/CsvExporter.cs GitHub Actions / build
|
||
{ | ||
_writer!.WriteLine(track.ToCsv()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using MusicCatalogue.Entities.Interfaces; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Logic.DataExchange | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public abstract class DataExchangeBase | ||
{ | ||
protected readonly IMusicCatalogueFactory _factory; | ||
|
||
protected DataExchangeBase(IMusicCatalogueFactory factory) | ||
{ | ||
_factory = factory; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using MusicCatalogue.Entities.DataExchange; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Logic.DataExchange | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public abstract class DataExportBase : DataExchangeBase | ||
{ | ||
private readonly string[] ColumnHeaders = | ||
{ | ||
"Artist", | ||
"Album", | ||
"Genre", | ||
"Released", | ||
"Cover Url", | ||
"Track Number", | ||
"Track", | ||
"Duration", | ||
}; | ||
|
||
public event EventHandler<TrackDataExchangeEventArgs>? TrackExport; | ||
|
||
protected DataExportBase(IMusicCatalogueFactory factory) : base(factory) | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Method to add headers to the output | ||
/// </summary> | ||
/// <param name="headers"></param> | ||
protected abstract void AddHeaders(IEnumerable<string> headers); | ||
|
||
/// <summary> | ||
/// Method to add a new flattened track to the output | ||
/// </summary> | ||
/// <param name="track"></param> | ||
/// <param name="recordNumber"></param> | ||
protected abstract void AddTrack(FlattenedTrack track, int recordNumber); | ||
|
||
/// <summary> | ||
/// Iterate over the collection calling the methods supplied by the child class to add | ||
/// headers and to add each track to the output | ||
/// </summary> | ||
protected async Task IterateOverCollection() | ||
{ | ||
// Call the method, supplied by the child class, to add the headers to the output | ||
AddHeaders(ColumnHeaders); | ||
|
||
// Initialise the record count | ||
int count = 0; | ||
|
||
// Retrieve a list of artists and their albums then iterate over the artists | ||
// and albums | ||
var artists = await _factory.Artists.ListAsync(x => true); | ||
foreach (var artist in artists.OrderBy(x => x.Name)) | ||
{ | ||
foreach (var album in artist.Albums.OrderBy(x => x.Title)) | ||
{ | ||
// Retrieve the track list for this album and iterate over the tracks | ||
var tracks = await _factory.Tracks.ListAsync(x => x.AlbumId == album.Id); | ||
foreach (var track in tracks.OrderBy(x => x.Number)) | ||
{ | ||
// Construct a flattened track | ||
var flattened = new FlattenedTrack | ||
{ | ||
ArtistName = artist.Name, | ||
AlbumTitle = album.Title, | ||
Genre = album.Genre, | ||
Released = album.Released, | ||
CoverUrl = album.CoverUrl, | ||
TrackNumber = track.Number, | ||
Title = track.Title, | ||
Duration = track.Duration | ||
}; | ||
|
||
// Call the method to add this track to the file | ||
count++; | ||
AddTrack(flattened, count); | ||
|
||
// Raise the track exported event | ||
TrackExport?.Invoke(this, new TrackDataExchangeEventArgs { RecordCount = count, Track = flattened }); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using ClosedXML.Excel; | ||
using MusicCatalogue.Entities.DataExchange; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Logic.DataExchange | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class XlsxExporter : DataExportBase, IExporter | ||
{ | ||
private const string WorksheetName = "Music"; | ||
|
||
private IXLWorksheet? _worksheet = null; | ||
|
||
#pragma warning disable CS8618 | ||
internal XlsxExporter(IMusicCatalogueFactory factory) : base(factory) | ||
{ | ||
} | ||
#pragma warning restore CS8618 | ||
|
||
/// <summary> | ||
/// Export the collection to a CSV file | ||
/// </summary> | ||
/// <param name="sightings"></param> | ||
/// <param name="file"></param> | ||
public async Task Export(string file) | ||
{ | ||
// Create a new Excel Workbook | ||
using (var workbook = new XLWorkbook()) | ||
{ | ||
// Add a worksheet to contain the data | ||
_worksheet = workbook.Worksheets.Add(WorksheetName); | ||
|
||
// Iterate over the collection, calling the row addition methods. This builds the spreadsheet | ||
// in memory | ||
await IterateOverCollection(); | ||
|
||
// Save the workbook to the specified file | ||
workbook.SaveAs(file); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Add the headers to the CSV file | ||
/// </summary> | ||
/// <param name="headers"></param> | ||
protected override void AddHeaders(IEnumerable<string> headers) | ||
{ | ||
var columnNumber = 1; | ||
foreach (var header in headers) | ||
{ | ||
_worksheet!.Cell(1, columnNumber).Value = header; | ||
columnNumber++; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Add a track to the CSV file | ||
/// </summary> | ||
/// <param name="track"></param> | ||
/// <param name="recordCount"></param> | ||
protected override void AddTrack(FlattenedTrack track, int recordCount) | ||
Check warning on line 62 in src/MusicCatalogue.Logic/DataExchange/XlsxExporter.cs GitHub Actions / build
|
||
{ | ||
var row = recordCount + 1; | ||
_worksheet!.Cell(row, 1).Value = track.ArtistName ?? ""; | ||
_worksheet!.Cell(row, 2).Value = track.AlbumTitle ?? ""; | ||
_worksheet!.Cell(row, 3).Value = track.Genre ?? ""; | ||
_worksheet!.Cell(row, 4).Value = track.Released?.ToString() ?? ""; | ||
_worksheet!.Cell(row, 5).Value = track.CoverUrl ?? ""; | ||
_worksheet!.Cell(row, 6).Value = track.TrackNumber?.ToString() ?? ""; | ||
_worksheet!.Cell(row, 7).Value = track.Title ?? ""; | ||
_worksheet!.Cell(row, 8).Value = track.FormattedDuration() ?? ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.