WIP: refactor
This commit is contained in:
@@ -2,72 +2,58 @@ using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
namespace WebAPI.Controllers;
|
||||
|
||||
public class LogsController : Controller
|
||||
{
|
||||
public class LogsController : Controller
|
||||
private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues;
|
||||
private readonly IConfiguration _configuration;
|
||||
public LogsController(
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
private SpreadsheetsResource.ValuesResource? googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LogsController(
|
||||
GoogleSheetsHelper _googleSheetsHelper,
|
||||
GoogleDriveHelper _googleDriveHelper,
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
if (_googleSheetsHelper.Service is not null) {
|
||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
}
|
||||
googleDriveHelper = _googleDriveHelper;
|
||||
configuration = _configuration;
|
||||
if (googleSheetsHelper.Service is not null) {
|
||||
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
}
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public void AddEntry(LogEntry entry)
|
||||
{
|
||||
if (_googleSheetValues is null) {
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
}
|
||||
|
||||
public void AddEntry(LogEntry entry)
|
||||
var type = entry.LogType switch
|
||||
{
|
||||
if (googleSheetValues is null) {
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
}
|
||||
String type;
|
||||
switch (entry.LogType) {
|
||||
case LogType.import:
|
||||
type = "Import";
|
||||
break;
|
||||
case LogType.backup:
|
||||
type = "Backup";
|
||||
break;
|
||||
case LogType.process:
|
||||
type = "Process";
|
||||
break;
|
||||
case LogType.powerBI:
|
||||
type = "PowerBIAccess";
|
||||
break;
|
||||
default:
|
||||
type = "Other"; // should never happen
|
||||
break;
|
||||
}
|
||||
var response = googleSheetValues.Get(configuration["appLogsFile"], $"{type}!A:A").Execute();
|
||||
var data = response.Values;
|
||||
int row = 1;
|
||||
if (data != null) {
|
||||
row = data.Count + 1;
|
||||
}
|
||||
var range = $"{type}!A{row}:D{row}";
|
||||
|
||||
List<object> logRow = new List<object>
|
||||
{
|
||||
entry.CreatedAt.ToString(new CultureInfo("pl-PL")),
|
||||
entry.Type.ToString(),
|
||||
entry.Title!,
|
||||
entry.Message!
|
||||
};
|
||||
|
||||
ValueRange valueRange = new ValueRange() { Values = new IList<object>[] { logRow }};
|
||||
|
||||
var updateRequest = googleSheetValues.Update(valueRange, configuration["appLogsFile"], range);
|
||||
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
||||
updateRequest.Execute();
|
||||
LogType.import => "Import",
|
||||
LogType.backup => "Backup",
|
||||
LogType.process => "Process",
|
||||
LogType.powerBI => "PowerBIAccess",
|
||||
_ => "Other"
|
||||
};
|
||||
var response = _googleSheetValues.Get(_configuration["appLogsFile"], $"{type}!A:A").Execute();
|
||||
var data = response.Values;
|
||||
var row = 1;
|
||||
if (data != null) {
|
||||
row = data.Count + 1;
|
||||
}
|
||||
var range = $"{type}!A{row}:D{row}";
|
||||
|
||||
var logRow = new List<object>
|
||||
{
|
||||
entry.CreatedAt.ToString(new CultureInfo("pl-PL")),
|
||||
entry.Type.ToString(),
|
||||
entry.Title!,
|
||||
entry.Message!
|
||||
};
|
||||
|
||||
var valueRange = new ValueRange { Values = new IList<object>[] { logRow }};
|
||||
|
||||
var updateRequest = _googleSheetValues.Update(valueRange, _configuration["appLogsFile"], range);
|
||||
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
||||
updateRequest.Execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user