89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using LaDOSE.Business.Interface;
|
|
using LaDOSE.Entity;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LaDOSE.Business.Provider.SheetsProvider
|
|
{
|
|
/// <summary>
|
|
/// Writes the export to the log instead of to Google, and reports success.
|
|
///
|
|
/// This exists so the whole feature — button, ordering, cumulative prefixes, payload shape —
|
|
/// can be exercised end to end before any Google account, key or sharing exists. Set
|
|
/// GoogleSheets:Writer to "Logging" and read the API log. It is also the safe way to try a
|
|
/// selection without touching the real spreadsheet.
|
|
/// </summary>
|
|
public class LoggingSheetsWriter : ISheetsWriter
|
|
{
|
|
private readonly ILogger<LoggingSheetsWriter> _logger;
|
|
|
|
public LoggingSheetsWriter(ILogger<LoggingSheetsWriter> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public string Name => "Logging";
|
|
|
|
public bool IsConfigured => true;
|
|
|
|
public Task<SheetExportResult> WriteTablesAsync(SheetExportRequest request, CancellationToken ct = default)
|
|
{
|
|
var tabs = request.Tabs ?? new List<SheetTable>();
|
|
|
|
_logger.LogInformation(
|
|
"Sheets export (Logging writer): spreadsheet {SpreadsheetId}, {TabCount} tabs",
|
|
request.SpreadsheetId, tabs.Count);
|
|
|
|
foreach (var tab in tabs)
|
|
{
|
|
var header = tab.Header ?? new List<string>();
|
|
var rows = tab.Rows ?? new List<SheetRow>();
|
|
|
|
_logger.LogInformation(" [{Name}] event {EventId}: {RowCount} rows x {ColumnCount} columns | {Header}",
|
|
tab.Name, tab.EventId, rows.Count, header.Count, string.Join(" | ", header));
|
|
|
|
foreach (var row in rows)
|
|
{
|
|
_logger.LogInformation(" {Player} | {Points} | {Total}",
|
|
row.Player, string.Join(" | ", row.Points ?? new List<int>()), row.Total);
|
|
}
|
|
|
|
foreach (var line in tab.Footer ?? new List<string>())
|
|
{
|
|
_logger.LogInformation(" -- {Line}", line);
|
|
}
|
|
}
|
|
|
|
return Task.FromResult(new SheetExportResult
|
|
{
|
|
SpreadsheetId = request.SpreadsheetId,
|
|
SpreadsheetUrl = SheetUrl(request.SpreadsheetId),
|
|
Writer = Name,
|
|
Tabs = tabs.Select(tab => new SheetTabResult
|
|
{
|
|
RequestedName = tab.Name,
|
|
Name = tab.Name,
|
|
Rows = (tab.Rows?.Count ?? 0) + 1,
|
|
Columns = tab.Header?.Count ?? 0,
|
|
// Nothing was inspected, so this is a claim rather than an observation.
|
|
Created = true
|
|
}).ToList(),
|
|
Warnings = new List<string>
|
|
{
|
|
"Writer is 'Logging': nothing was written to Google. The payload is in the API log."
|
|
}
|
|
});
|
|
}
|
|
|
|
internal static string SheetUrl(string spreadsheetId)
|
|
{
|
|
return string.IsNullOrWhiteSpace(spreadsheetId)
|
|
? null
|
|
: $"https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit";
|
|
}
|
|
}
|
|
}
|