LogsController
This commit is contained in:
56
WebAPI/Controllers/LogsController.cs
Normal file
56
WebAPI/Controllers/LogsController.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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
|
||||
{
|
||||
public class LogsController : Controller
|
||||
{
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LogsController(
|
||||
GoogleSheetsHelper _googleSheetsHelper,
|
||||
GoogleDriveHelper _googleDriveHelper,
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
googleDriveHelper = _googleDriveHelper;
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
public void AddEntry(LogEntry entry)
|
||||
{
|
||||
String type;
|
||||
switch (entry.LogType) {
|
||||
case LogType.import:
|
||||
type = "Import";
|
||||
break;
|
||||
default:
|
||||
type = "Other"; // should never happen
|
||||
break;
|
||||
}
|
||||
var response = googleSheetValues.Get(configuration["appLogsFile"], $"{type}!A:A").Execute();
|
||||
var data = response.Values;
|
||||
int 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user