WIP: Resolve all code issues

This commit is contained in:
Michał Zieliski
2024-06-18 18:39:02 +02:00
parent f93bb9cd42
commit c2a98e0386
10 changed files with 363 additions and 441 deletions

View File

@@ -1,13 +1,7 @@
using System.Data;
using System.Globalization;
using System.Xml.Serialization;
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using WebAPI.dataParsers;
using WebAPI.Exports;
using WebAPI.Models;
using static Google.Apis.Drive.v3.FilesResource;
@@ -15,26 +9,20 @@ namespace WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AdminController : Controller
{
private readonly AppDbContext db;
private GoogleDriveHelper googleDriveHelper;
private GoogleSheetsHelper googleSheetsHelper;
private readonly IConfiguration configuration;
private readonly LogsController logsController;
private readonly GoogleDriveHelper _googleDriveHelper;
private readonly IConfiguration _configuration;
private readonly LogsController _logsController;
public AdminController(
AppDbContext _db,
GoogleSheetsHelper _googleSheetsHelper,
GoogleDriveHelper _googleDriveHelper,
IConfiguration _configuration)
GoogleDriveHelper googleDriveHelper,
IConfiguration configuration,
LogsController logsController)
{
db = _db;
googleSheetsHelper = _googleSheetsHelper;
googleDriveHelper = _googleDriveHelper;
configuration = _configuration;
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
_googleDriveHelper = googleDriveHelper;
_configuration = configuration;
_logsController = logsController;
}
[HttpGet]
@@ -42,18 +30,19 @@ namespace WebAPI.Controllers
[AllowAnonymous]
public IActionResult BackupDatabase(string apiKey)
{
if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"])
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
{
return Unauthorized();
}
try
{
string databaseName = "diunabi-morska";
string localDatabasePath = $"{configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
var databaseName = "diunabi-morska";
var localDatabasePath = $"{_configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
var formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
var formatName = $"Full Backup of {databaseName}";
var connection = new SqlConnection(configuration.GetConnectionString("SQLDatabase"));
var connection = new SqlConnection(_configuration.GetConnectionString("SQLDatabase"));
var sql = @"BACKUP DATABASE @databaseName
TO DISK = @localDatabasePath
@@ -73,24 +62,26 @@ namespace WebAPI.Controllers
command.ExecuteNonQuery();
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
body.Name = Path.GetFileName(localDatabasePath);
body.Parents = new List<string?> { configuration["GDriveBackupDirectory"] };
body.MimeType = "application/octet-stream";
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(localDatabasePath),
Parents = new List<string?> { _configuration["GDriveBackupDirectory"] },
MimeType = "application/octet-stream"
};
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
if (googleDriveHelper.Service is null)
if (_googleDriveHelper.Service is null)
{
throw new Exception("Google Drive API not initialized");
}
CreateMediaUpload request = googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
var request = _googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
request.Fields = "id";
var response = request.Upload();
logsController.AddEntry(new LogEntry
request.Upload();
_logsController.AddEntry(new LogEntry
{
Title = "Backup success",
Type = LogEntryType.info,
@@ -101,7 +92,7 @@ namespace WebAPI.Controllers
}
catch (Exception e)
{
logsController.AddEntry(new LogEntry
_logsController.AddEntry(new LogEntry
{
Title = "Backup error",
Type = LogEntryType.error,