2024-11-20 01:45:48 +01:00
|
|
|
|
using System.IO;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using SQLite;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using Wino.Core.Domain.Entities.Calendar;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
using Wino.Core.Domain.Entities.Mail;
|
|
|
|
|
|
using Wino.Core.Domain.Entities.Shared;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
namespace Wino.Services;
|
|
|
|
|
|
|
|
|
|
|
|
public interface IDatabaseService : IInitializeAsync
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
|
SQLiteAsyncConnection Connection { get; }
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public class DatabaseService : IDatabaseService
|
|
|
|
|
|
{
|
|
|
|
|
|
private const string DatabaseName = "Wino180.db";
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
private bool _isInitialized = false;
|
|
|
|
|
|
private readonly IApplicationConfiguration _folderConfiguration;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public SQLiteAsyncConnection Connection { get; private set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public DatabaseService(IApplicationConfiguration folderConfiguration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_folderConfiguration = folderConfiguration;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isInitialized)
|
|
|
|
|
|
return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var publisherCacheFolder = _folderConfiguration.PublisherSharedFolderPath;
|
|
|
|
|
|
var databaseFileName = Path.Combine(publisherCacheFolder, DatabaseName);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
Connection = new SQLiteAsyncConnection(databaseFileName);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
await CreateTablesAsync();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
_isInitialized = true;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
private async Task CreateTablesAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Connection.CreateTablesAsync(CreateFlags.None,
|
|
|
|
|
|
typeof(MailCopy),
|
|
|
|
|
|
typeof(MailItemFolder),
|
|
|
|
|
|
typeof(MailAccount),
|
|
|
|
|
|
typeof(AccountContact),
|
|
|
|
|
|
typeof(CustomServerInformation),
|
|
|
|
|
|
typeof(AccountSignature),
|
|
|
|
|
|
typeof(MergedInbox),
|
|
|
|
|
|
typeof(MailAccountPreferences),
|
|
|
|
|
|
typeof(MailAccountAlias),
|
|
|
|
|
|
typeof(AccountCalendar),
|
|
|
|
|
|
typeof(CalendarEventAttendee),
|
|
|
|
|
|
typeof(CalendarItem),
|
2025-06-21 01:40:25 +02:00
|
|
|
|
typeof(Reminder),
|
|
|
|
|
|
typeof(Thumbnail)
|
2025-02-16 11:54:23 +01:00
|
|
|
|
);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|