Rename salesbook
This commit is contained in:
15
salesbook.Maui/Core/Services/FormFactor.cs
Normal file
15
salesbook.Maui/Core/Services/FormFactor.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using salesbook.Shared.Core.Interface;
|
||||
|
||||
namespace salesbook.Maui.Core.Services;
|
||||
|
||||
public class FormFactor : IFormFactor
|
||||
{
|
||||
public string GetFormFactor()
|
||||
{
|
||||
return DeviceInfo.Idiom.ToString();
|
||||
}
|
||||
public string GetPlatform()
|
||||
{
|
||||
return DeviceInfo.Platform.ToString() + " - " + DeviceInfo.VersionString;
|
||||
}
|
||||
}
|
||||
117
salesbook.Maui/Core/Services/LocalDbService.cs
Normal file
117
salesbook.Maui/Core/Services/LocalDbService.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
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<AnagClie>();
|
||||
_connection.CreateTableAsync<JtbComt>();
|
||||
_connection.CreateTableAsync<PtbPros>();
|
||||
_connection.CreateTableAsync<PtbProsRif>();
|
||||
_connection.CreateTableAsync<StbActivity>();
|
||||
_connection.CreateTableAsync<VtbCliePersRif>();
|
||||
_connection.CreateTableAsync<VtbDest>();
|
||||
_connection.CreateTableAsync<StbActivityResult>();
|
||||
_connection.CreateTableAsync<StbActivityType>();
|
||||
_connection.CreateTableAsync<StbUser>();
|
||||
}
|
||||
|
||||
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 stb_user;");
|
||||
|
||||
await _connection.CreateTableAsync<StbActivityResult>();
|
||||
await _connection.CreateTableAsync<StbActivityType>();
|
||||
await _connection.CreateTableAsync<StbUser>();
|
||||
}
|
||||
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<AnagClie>();
|
||||
await _connection.CreateTableAsync<JtbComt>();
|
||||
await _connection.CreateTableAsync<PtbPros>();
|
||||
await _connection.CreateTableAsync<PtbProsRif>();
|
||||
await _connection.CreateTableAsync<StbActivity>();
|
||||
await _connection.CreateTableAsync<VtbCliePersRif>();
|
||||
await _connection.CreateTableAsync<VtbDest>();
|
||||
|
||||
Console.WriteLine("Database resettato con successo.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore durante il reset del database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task InsertAll<T>(List<T> entityList) =>
|
||||
_connection.InsertAllAsync(entityList, typeof(T));
|
||||
|
||||
public async Task Insert<T>(List<T> 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<T>(List<T> entityList)
|
||||
{
|
||||
foreach (var entity in entityList)
|
||||
{
|
||||
var result = await _connection.UpdateAsync(entity);
|
||||
if (result == 0)
|
||||
{
|
||||
await _connection.InsertAsync(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task<List<T>> Get<T>(Expression<Func<T, bool>>? whereCond = null) where T : new() =>
|
||||
whereCond is null
|
||||
? _connection.Table<T>().ToListAsync()
|
||||
: _connection.Table<T>().Where(whereCond).ToListAsync();
|
||||
|
||||
public List<T> Get<T>(string sql) where T : new() => _connection.QueryAsync<T>(sql).Result;
|
||||
|
||||
public async Task Delete<T>(T entity) =>
|
||||
await _connection.DeleteAsync(entity);
|
||||
}
|
||||
87
salesbook.Maui/Core/Services/ManageDataService.cs
Normal file
87
salesbook.Maui/Core/Services/ManageDataService.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using AutoMapper;
|
||||
using System.Linq.Expressions;
|
||||
using salesbook.Shared.Core.Dto;
|
||||
using salesbook.Shared.Core.Entity;
|
||||
using salesbook.Shared.Core.Helpers.Enum;
|
||||
using salesbook.Shared.Core.Interface;
|
||||
|
||||
namespace salesbook.Maui.Core.Services;
|
||||
|
||||
public class ManageDataService(LocalDbService localDb, IMapper mapper) : IManageDataService
|
||||
{
|
||||
public Task<List<T>> GetTable<T>(Expression<Func<T, bool>>? whereCond = null) where T : new() =>
|
||||
localDb.Get(whereCond);
|
||||
|
||||
public async Task<List<ActivityDTO>> GetActivity(Expression<Func<StbActivity, bool>>? whereCond = null)
|
||||
{
|
||||
var activities = await localDb.Get(whereCond);
|
||||
|
||||
var codJcomList = activities
|
||||
.Select(x => x.CodJcom)
|
||||
.Where(x => !string.IsNullOrEmpty(x))
|
||||
.Distinct().ToList();
|
||||
|
||||
var jtbComtList = await localDb.Get<JtbComt>(x => codJcomList.Contains(x.CodJcom));
|
||||
var commesseDict = jtbComtList.ToDictionary(x => x.CodJcom, x => x.Descrizione);
|
||||
|
||||
var codAnagList = activities
|
||||
.Select(x => x.CodAnag)
|
||||
.Where(x => !string.IsNullOrEmpty(x))
|
||||
.Distinct().ToList();
|
||||
var clientList = await localDb.Get<AnagClie>(x => codAnagList.Contains(x.CodAnag));
|
||||
var distinctClient = clientList.ToDictionary(x => x.CodAnag, x => x.RagSoc);
|
||||
|
||||
var prospectList = await localDb.Get<PtbPros>(x => codAnagList.Contains(x.CodPpro));
|
||||
var distinctProspect = prospectList.ToDictionary(x => x.CodPpro, x => x.RagSoc);
|
||||
|
||||
var returnDto = activities
|
||||
.Select(activity =>
|
||||
{
|
||||
var dto = mapper.Map<ActivityDTO>(activity);
|
||||
|
||||
if (activity.CodJcom != null)
|
||||
{
|
||||
dto.Category = ActivityCategoryEnum.Commessa;
|
||||
}
|
||||
else
|
||||
{
|
||||
dto.Category = activity.CodAnag != null ? ActivityCategoryEnum.Interna : ActivityCategoryEnum.Memo;
|
||||
}
|
||||
|
||||
if (dto.Category == ActivityCategoryEnum.Interna && activity.CodAnag != null)
|
||||
{
|
||||
string? ragSoc;
|
||||
|
||||
if (distinctClient.TryGetValue(activity.CodAnag, out ragSoc) ||
|
||||
distinctProspect.TryGetValue(activity.CodAnag, out ragSoc))
|
||||
{
|
||||
dto.Cliente = ragSoc;
|
||||
}
|
||||
}
|
||||
|
||||
dto.Commessa = activity.CodJcom != null && commesseDict.TryGetValue(activity.CodJcom, out var descr)
|
||||
? descr
|
||||
: null;
|
||||
return dto;
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return returnDto;
|
||||
}
|
||||
|
||||
public Task InsertOrUpdate<T>(T objectToSave) =>
|
||||
localDb.InsertOrUpdate<T>([objectToSave]);
|
||||
|
||||
public Task Delete<T>(T objectToDelete) =>
|
||||
localDb.Delete(objectToDelete);
|
||||
|
||||
public async Task DeleteActivity(ActivityDTO activity)
|
||||
{
|
||||
await localDb.Delete(
|
||||
(await GetTable<StbActivity>(x => x.ActivityId.Equals(activity.ActivityId))).Last()
|
||||
);
|
||||
}
|
||||
|
||||
public Task ClearDb() =>
|
||||
localDb.ResetDb();
|
||||
}
|
||||
12
salesbook.Maui/Core/Services/NetworkService.cs
Normal file
12
salesbook.Maui/Core/Services/NetworkService.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using salesbook.Shared.Core.Interface;
|
||||
|
||||
namespace salesbook.Maui.Core.Services;
|
||||
|
||||
public class NetworkService : INetworkService
|
||||
{
|
||||
public bool IsNetworkAvailable()
|
||||
{
|
||||
return Connectivity.Current.NetworkAccess == NetworkAccess.Internet;
|
||||
}
|
||||
|
||||
}
|
||||
86
salesbook.Maui/Core/Services/SyncDbService.cs
Normal file
86
salesbook.Maui/Core/Services/SyncDbService.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using salesbook.Shared.Core.Helpers;
|
||||
using salesbook.Shared.Core.Interface;
|
||||
|
||||
namespace salesbook.Maui.Core.Services;
|
||||
|
||||
public class SyncDbService(IIntegryApiService integryApiService, LocalDbService localDb) : ISyncDbService
|
||||
{
|
||||
public async Task GetAndSaveActivity(string? dateFilter)
|
||||
{
|
||||
var allActivity = await integryApiService.RetrieveActivity(dateFilter);
|
||||
|
||||
if (!allActivity.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(allActivity!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(allActivity!);
|
||||
}
|
||||
|
||||
public async Task GetAndSaveCommesse(string? dateFilter)
|
||||
{
|
||||
var allCommesse = await integryApiService.RetrieveAllCommesse(dateFilter);
|
||||
|
||||
if (!allCommesse.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(allCommesse!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(allCommesse!);
|
||||
}
|
||||
|
||||
public async Task GetAndSaveProspect(string? dateFilter)
|
||||
{
|
||||
var taskSyncResponseDto = await integryApiService.RetrieveProspect(dateFilter);
|
||||
|
||||
if (!taskSyncResponseDto.PtbPros.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(taskSyncResponseDto.PtbPros!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(taskSyncResponseDto.PtbPros!);
|
||||
|
||||
if (!taskSyncResponseDto.PtbProsRif.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(taskSyncResponseDto.PtbProsRif!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(taskSyncResponseDto.PtbProsRif!);
|
||||
}
|
||||
|
||||
public async Task GetAndSaveClienti(string? dateFilter)
|
||||
{
|
||||
var taskSyncResponseDto = await integryApiService.RetrieveAnagClie(dateFilter);
|
||||
|
||||
if (!taskSyncResponseDto.AnagClie.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(taskSyncResponseDto.AnagClie!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(taskSyncResponseDto.AnagClie!);
|
||||
|
||||
if (!taskSyncResponseDto.VtbDest.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(taskSyncResponseDto.VtbDest!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(taskSyncResponseDto.VtbDest!);
|
||||
|
||||
if (!taskSyncResponseDto.VtbCliePersRif.IsNullOrEmpty())
|
||||
if (dateFilter is null)
|
||||
await localDb.InsertAll(taskSyncResponseDto.VtbCliePersRif!);
|
||||
else
|
||||
await localDb.InsertOrUpdate(taskSyncResponseDto.VtbCliePersRif!);
|
||||
}
|
||||
|
||||
public async Task GetAndSaveSettings(string? dateFilter)
|
||||
{
|
||||
if (dateFilter is not null)
|
||||
await localDb.ResetSettingsDb();
|
||||
|
||||
var settingsResponse = await integryApiService.RetrieveSettings();
|
||||
|
||||
if (!settingsResponse.ActivityResults.IsNullOrEmpty())
|
||||
await localDb.InsertAll(settingsResponse.ActivityResults!);
|
||||
|
||||
if (!settingsResponse.ActivityTypes.IsNullOrEmpty())
|
||||
await localDb.InsertAll(settingsResponse.ActivityTypes!);
|
||||
|
||||
if (!settingsResponse.StbUsers.IsNullOrEmpty())
|
||||
await localDb.InsertAll(settingsResponse.StbUsers!);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user