-
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.
Merge pull request #50 from davewalker5/MC-293-Equiment-Export
MC-293 Implement Equipment Export
- Loading branch information
Showing
41 changed files
with
843 additions
and
124 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM mcr.microsoft.com/dotnet/core/aspnet:latest | ||
COPY musiccatalogue.api-1.24.0.0 /opt/musiccatalogue.api-1.24.0.0 | ||
WORKDIR /opt/musiccatalogue.api-1.24.0.0/bin | ||
COPY musiccatalogue.api-1.25.0.0 /opt/musiccatalogue.api-1.25.0.0 | ||
WORKDIR /opt/musiccatalogue.api-1.25.0.0/bin | ||
ENTRYPOINT [ "./MusicCatalogue.Api" ] |
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
15 changes: 15 additions & 0 deletions
15
src/MusicCatalogue.Api/Entities/EquipmentExportWorkItem.cs
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,15 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Api.Entities | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class EquipmentExportWorkItem : BackgroundWorkItem | ||
{ | ||
public string FileName { get; set; } = ""; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{base.ToString()}, FileName = {FileName}"; | ||
} | ||
} | ||
} |
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
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,46 @@ | ||
using Microsoft.Extensions.Options; | ||
using MusicCatalogue.Api.Entities; | ||
using MusicCatalogue.Api.Interfaces; | ||
using MusicCatalogue.Entities.Config; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Api.Services | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class EquipmentExportService : BackgroundQueueProcessor<EquipmentExportWorkItem> | ||
{ | ||
private readonly MusicApplicationSettings _settings; | ||
public EquipmentExportService( | ||
ILogger<BackgroundQueueProcessor<EquipmentExportWorkItem>> logger, | ||
IBackgroundQueue<EquipmentExportWorkItem> queue, | ||
IServiceScopeFactory serviceScopeFactory, | ||
IOptions<MusicApplicationSettings> settings) | ||
: base(logger, queue, serviceScopeFactory) | ||
{ | ||
_settings = settings.Value; | ||
} | ||
|
||
/// <summary> | ||
/// Export the equipment register | ||
/// </summary> | ||
/// <param name="item"></param> | ||
/// <param name="factory"></param> | ||
/// <returns></returns> | ||
protected override async Task ProcessWorkItem(EquipmentExportWorkItem item, IMusicCatalogueFactory factory) | ||
{ | ||
MessageLogger.LogInformation("Retrieving equipment records for export"); | ||
|
||
// Use the file extension to determine which exporter to use | ||
var extension = Path.GetExtension(item.FileName).ToLower(); | ||
IEquipmentExporter? exporter = extension == ".xlsx" ? factory.EquipmentXlsxExporter : factory.EquipmentCsvExporter; | ||
|
||
// Construct the full path to the export file | ||
var filePath = Path.Combine(_settings.CatalogueExportPath, item.FileName); | ||
|
||
// Export the equipment register | ||
await exporter.Export(filePath); | ||
MessageLogger.LogInformation("Equipment register export completed"); | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/MusicCatalogue.Entities/DataExchange/EquipmentDataExchangeEventArgs.cs
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,12 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Entities.DataExchange | ||
{ | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class EquipmentDataExchangeEventArgs : EventArgs | ||
{ | ||
public long RecordCount { get; set; } | ||
public FlattenedEquipment? Equipment { get; set; } | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
src/MusicCatalogue.Entities/DataExchange/FlattenedEquipment.cs
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,134 @@ | ||
using MusicCatalogue.Entities.Exceptions; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
|
||
namespace MusicCatalogue.Entities.DataExchange | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class FlattenedEquipment | ||
{ | ||
protected const string DateTimeFormat = "dd/MM/yyyy"; | ||
|
||
public const int DescriptionField = 0; | ||
public const int ModelField = 1; | ||
private const int SerialNumberField = 2; | ||
private const int EquipmentTypeField = 3; | ||
private const int ManufacturerField = 4; | ||
private const int WishlistItemField = 5; | ||
private const int PurchasedField = 6; | ||
private const int PriceField = 7; | ||
private const int RetailerField = 8; | ||
private const int NumberOfFields = 9; | ||
|
||
public string Description { get; set; } = ""; | ||
public string? Model { get; set; } | ||
public string? SerialNumber { get; set; } | ||
public string EquipmentTypeName { get; set; } = ""; | ||
public string ManufacturerName { get; set; } = ""; | ||
public bool? IsWishListItem { get; set; } | ||
public DateTime? Purchased { get; set; } | ||
public decimal? Price { get; set; } | ||
public string? RetailerName { get; set; } | ||
|
||
/// <summary> | ||
/// Purchase date formatted per the DateTimeFormat | ||
/// </summary> | ||
public string FormattedPurchaseDate | ||
{ | ||
get | ||
{ | ||
return Purchased != null ? (Purchased ?? DateTime.Now).ToString(DateTimeFormat) : ""; | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Create a representation of the flattened equipment record in CSV format | ||
/// </summary> | ||
/// <returns></returns> | ||
public string ToCsv() | ||
{ | ||
var wishListString = (IsWishListItem ?? false).ToString(); | ||
var purchasedDateString = Purchased != null ? (Purchased ?? DateTime.Now).ToString(DateTimeFormat) : ""; | ||
var priceString = Price != null ? Price.ToString() : ""; | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
AppendField(builder, Description); | ||
AppendField(builder, Model ?? ""); | ||
AppendField(builder, SerialNumber ?? ""); | ||
AppendField(builder, EquipmentTypeName); | ||
AppendField(builder, ManufacturerName); | ||
AppendField(builder, wishListString); | ||
AppendField(builder, purchasedDateString); | ||
AppendField(builder, priceString); | ||
AppendField(builder, RetailerName ?? ""); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Create a flattened equipment record from a CSV string | ||
/// </summary> | ||
/// <param name="fields"></param> | ||
/// <returns></returns> | ||
public static FlattenedEquipment FromCsv(IList<string> fields) | ||
{ | ||
// Check we have the required number of fields | ||
if ((fields == null) || (fields.Count != NumberOfFields)) | ||
{ | ||
throw new InvalidRecordFormatException("Incorrect number of CSV fields"); | ||
} | ||
|
||
// Get the model and serial number, both of which may be NULL | ||
string? model = !string.IsNullOrEmpty(fields[ModelField]) ? fields[ModelField] : null; | ||
string? serialNumber = !string.IsNullOrEmpty(fields[SerialNumberField]) ? fields[SerialNumberField] : null; | ||
|
||
// Determine the purchase date | ||
DateTime? purchasedDate = null; | ||
if (!string.IsNullOrEmpty(fields[PurchasedField])) | ||
{ | ||
purchasedDate = DateTime.ParseExact(fields[PurchasedField], DateTimeFormat, null); | ||
} | ||
|
||
// Determine the price | ||
decimal? price = !string.IsNullOrEmpty(fields[PriceField]) ? decimal.Parse(fields[PriceField]) : null; | ||
|
||
// Create a new "flattened" record containing equipment details | ||
return new FlattenedEquipment | ||
{ | ||
Description = fields[DescriptionField], | ||
Model = fields[ModelField], | ||
SerialNumber = fields[SerialNumberField], | ||
EquipmentTypeName = fields[EquipmentTypeField], | ||
ManufacturerName = fields[ManufacturerField], | ||
IsWishListItem = bool.Parse(fields[WishlistItemField]), | ||
Purchased = purchasedDate, | ||
Price = price, | ||
RetailerName = fields[RetailerField] | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Append a value to a string builder holding a representation of a flattened equpiment record in CSV format | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="value"></param> | ||
private static void AppendField(StringBuilder builder, object? value) | ||
{ | ||
// Add a separator if there are already fields in the line under construction | ||
if (builder.Length > 0) | ||
{ | ||
builder.Append(','); | ||
} | ||
|
||
// Convert the value to string and see if it contains the delimiter | ||
var stringValue = (value?.ToString() ?? "").Replace('"', '\''); | ||
var containsDelimiter = !string.IsNullOrEmpty(stringValue) && stringValue.Contains(','); | ||
|
||
// Add the value to the builder, quoting it if needed | ||
if (containsDelimiter) builder.Append('"'); | ||
builder.Append(stringValue); | ||
if (containsDelimiter) builder.Append('"'); | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace MusicCatalogue.Entities.Database | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public abstract class EquipmentBase | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
|
||
[ForeignKey("EquipmentType.Id")] | ||
public int EquipmentTypeId { get; set; } | ||
|
||
[ForeignKey("Manufacturer.Id")] | ||
public int ManufacturerId { get; set; } | ||
|
||
[Required] | ||
public string Description { get; set; } = ""; | ||
|
||
public string? Model { get; set; } | ||
|
||
public string? SerialNumber { get; set; } | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/MusicCatalogue.Entities/Interfaces/IEquipmentExporter.cs
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 IEquipmentExporter | ||
{ | ||
event EventHandler<EquipmentDataExchangeEventArgs>? EquipmentExport; | ||
|
||
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
Oops, something went wrong.