using SQLite; using System.Linq.Expressions; using salesbook.Shared.Core.Entity; namespace salesbook.Maui.Core.Services; public class LocalDbService { private const string DB_NAME = "task_db.db3"; private readonly SQLiteAsyncConnection _connection; public LocalDbService() { _connection = new SQLiteAsyncConnection(Path.Combine(FileSystem.AppDataDirectory, DB_NAME)); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); _connection.CreateTableAsync(); } public async Task ResetSettingsDb() { try { await _connection.ExecuteAsync("DROP TABLE IF EXISTS stb_activity_result;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS stb_activity_type;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS srl_activity_type_user;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS stb_user;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS vtb_tipi;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS nazioni;"); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); } catch (Exception ex) { Console.WriteLine($"Errore durante il reset del database(settings): {ex.Message}"); throw; } } public async Task ResetDb() { await ResetSettingsDb(); try { await _connection.ExecuteAsync("DROP TABLE IF EXISTS anag_clie;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS jtb_comt;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS ptb_pros;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS ptb_pros_rif;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS stb_activity;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS vtb_clie_pers_rif;"); await _connection.ExecuteAsync("DROP TABLE IF EXISTS vtb_dest;"); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); await _connection.CreateTableAsync(); Console.WriteLine("Database resettato con successo."); } catch (Exception ex) { Console.WriteLine($"Errore durante il reset del database: {ex.Message}"); throw; } } public Task InsertAll(List entityList) => _connection.InsertAllAsync(entityList, typeof(T)); public async Task Insert(List entityList) { foreach (var entity in entityList) { try { await _connection.InsertAsync(entity, typeof(T)); } catch (Exception ex) { Console.WriteLine($"Errore db su {entity}: {ex.Message}"); } } } public async Task InsertOrUpdate(List entityList) { foreach (var entity in entityList) { var result = await _connection.UpdateAsync(entity); if (result == 0) { await _connection.InsertAsync(entity); } } } public Task> Get(Expression>? whereCond = null) where T : new() => whereCond is null ? _connection.Table().ToListAsync() : _connection.Table().Where(whereCond).ToListAsync(); public List Get(string sql) where T : new() => _connection.QueryAsync(sql).Result; public async Task Delete(T entity) => await _connection.DeleteAsync(entity); }