69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using SQLite;
|
|
using Wino.Core.Domain.Entities.Calendar;
|
|
using Wino.Core.Domain.Entities.Mail;
|
|
using Wino.Core.Domain.Entities.Shared;
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
namespace Wino.Services;
|
|
|
|
public interface IDatabaseService : IInitializeAsync
|
|
{
|
|
SQLiteAsyncConnection Connection { get; }
|
|
}
|
|
|
|
public class DatabaseService : IDatabaseService
|
|
{
|
|
private const string DatabaseName = "Wino180.db";
|
|
|
|
private bool _isInitialized = false;
|
|
private readonly IApplicationConfiguration _folderConfiguration;
|
|
|
|
public SQLiteAsyncConnection Connection { get; private set; }
|
|
|
|
public DatabaseService(IApplicationConfiguration folderConfiguration)
|
|
{
|
|
_folderConfiguration = folderConfiguration;
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
if (_isInitialized)
|
|
return;
|
|
|
|
var publisherCacheFolder = _folderConfiguration.PublisherSharedFolderPath;
|
|
var databaseFileName = Path.Combine(publisherCacheFolder, DatabaseName);
|
|
|
|
Connection = new SQLiteAsyncConnection(databaseFileName);
|
|
|
|
await CreateTablesAsync();
|
|
|
|
_isInitialized = true;
|
|
}
|
|
|
|
private async Task CreateTablesAsync()
|
|
{
|
|
await Task.WhenAll(
|
|
Connection.CreateTableAsync<MailCopy>(),
|
|
Connection.CreateTableAsync<MailItemFolder>(),
|
|
Connection.CreateTableAsync<MailAccount>(),
|
|
Connection.CreateTableAsync<AccountContact>(),
|
|
Connection.CreateTableAsync<CustomServerInformation>(),
|
|
Connection.CreateTableAsync<AccountSignature>(),
|
|
Connection.CreateTableAsync<MergedInbox>(),
|
|
Connection.CreateTableAsync<MailAccountPreferences>(),
|
|
Connection.CreateTableAsync<MailAccountAlias>(),
|
|
Connection.CreateTableAsync<Thumbnail>(),
|
|
Connection.CreateTableAsync<KeyboardShortcut>(),
|
|
Connection.CreateTableAsync<AccountCalendar>(),
|
|
Connection.CreateTableAsync<CalendarEventAttendee>(),
|
|
Connection.CreateTableAsync<CalendarItem>(),
|
|
Connection.CreateTableAsync<CalendarAttachment>(),
|
|
Connection.CreateTableAsync<Reminder>(),
|
|
Connection.CreateTableAsync<MailInvitationCalendarMapping>()
|
|
);
|
|
}
|
|
|
|
}
|