Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

58 add debt mgt #62

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions FlowHub.DataAccess/IRepositories/IDataAccessRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface IDataAccessRepo
{
LiteDatabaseAsync GetDb();
void DeleteDB();
}
5 changes: 2 additions & 3 deletions FlowHub.DataAccess/IRepositories/IExpendituresRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ public interface IExpendituresRepository
// Task<List<ExpendituresModel>> GetAllExpendituresAsync();
event Action OfflineExpendituresListChanged;
Task<List<ExpendituresModel>> GetAllExpendituresAsync();
// List<ExpendituresModel> OnlineExpendituresList { get; set; }
// List<ExpendituresModel> OnlineExpendituresList { get; set; }

List<ExpendituresModel> OfflineExpendituresList { get; set; }
// Task<List<ExpendituresModel>> GetAllExpFromOnlineAsync(string UserId);
// Task<List<ExpendituresModel>> GetAllExpFromOnlineAsync(string UserId);
Task<bool> AddExpenditureAsync(ExpendituresModel expenditure);
Task<bool> UpdateExpenditureAsync(ExpendituresModel expenditure);
Task<bool> DeleteExpenditureAsync(ExpendituresModel expenditure);

Task SynchronizeExpendituresAsync();

Task DropExpendituresCollection();

}
2 changes: 1 addition & 1 deletion FlowHub.DataAccess/IRepositories/IIncomeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface IIncomeRepository
{
event Action OfflineIncomesListChanged;
Task<List<IncomeModel>> GetAllIncomesAsync();
List<IncomeModel> OfflineIncomesList { get; set ; }
List<IncomeModel> OfflineIncomesList { get; set; }
Task<bool> AddIncomeAsync(IncomeModel income);
Task<bool> DeleteIncomeAsync(IncomeModel incomeId);
Task<bool> UpdateIncomeAsync(IncomeModel income);
Expand Down
2 changes: 1 addition & 1 deletion FlowHub.DataAccess/IRepositories/IUsersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface IUsersRepository
event Action OfflineUserDataChanged;
public UsersModel OfflineUser { get; set; }
public UsersModel OnlineUser { get; set; }
// Task<UsersModel> GetUserAsync();
// Task<UsersModel> GetUserAsync();
Task<UsersModel> GetUserAsync(string UserEmail, string UserPassword);
Task<UsersModel> GetUserAsync(string UserId);
Task<UsersModel> GetUserOnlineAsync(UsersModel user);
Expand Down
22 changes: 21 additions & 1 deletion FlowHub.DataAccess/Platforms/Android/DataAccessRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
// All the code in this file is only included on Android.
public class DataAccessRepo : IDataAccessRepo
{
LiteDatabaseAsync db ;
LiteDatabaseAsync db;

public LiteDatabaseAsync GetDb() //this function returns the path where the db file is saved
{
string path;

path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SavingTracker.db");
// string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;

db = new LiteDatabaseAsync(path);

return db;
}
public void DeleteDB()
{
string path;

path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SavingTracker.db");
if (File.Exists(path))
{
try
{
File.Delete(path);
Debug.WriteLine("File deleted");
}
catch (IOException e)
{
Debug.WriteLine(e.Message);
}
}
}
}
6 changes: 6 additions & 0 deletions FlowHub.DataAccess/Platforms/Windows/DataAccessRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
public class DataAccessRepo : IDataAccessRepo
{
LiteDatabaseAsync db;

public void DeleteDB()
{
throw new NotImplementedException();
}

public LiteDatabaseAsync GetDb() //this function returns the path where the db file is saved
{
string path;
Expand Down
35 changes: 19 additions & 16 deletions FlowHub.DataAccess/Repositories/DebtRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DebtRepository : IDebtRepository
private readonly IOnlineCredentialsRepository onlineRepository;
private readonly IUsersRepository usersRepo;

public List<DebtModel> OfflineDebtList { get ; set ; }
public List<DebtModel> OfflineDebtList { get; set; }
public List<DebtModel> OnlineDebtList { get; set; }

public event Action OfflineDebtListChanged;
Expand All @@ -22,7 +22,7 @@ public DebtRepository(IDataAccessRepo dataAccess, IOnlineCredentialsRepository o
{
this.dataAccess = dataAccess;
this.onlineRepository = onlineRepository;
this.usersRepo = userRepo;
usersRepo = userRepo;
}

async Task<LiteDatabaseAsync> OpenDB()
Expand All @@ -49,6 +49,7 @@ public async Task<List<DebtModel>> GetAllDebtAsync()
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.UpdateDateTime)
.ToListAsync();
OfflineDebtList ??= Enumerable.Empty<DebtModel>().ToList();
return OfflineDebtList;
}
}
Expand Down Expand Up @@ -99,7 +100,7 @@ async Task LoadOnlineDB()
Debug.WriteLine($"Exception Message {ex.Message}");
}
}
var filtersDebt= Builders<DebtModel>.Filter.Eq("UserId", usersRepo.OnlineUser.Id) &
var filtersDebt = Builders<DebtModel>.Filter.Eq("UserId", usersRepo.OnlineUser.Id) &
Builders<DebtModel>.Filter.Eq("Currency", usersRepo.OfflineUser.UserCurrency);

AllDebtsOnline ??= DBOnline.GetCollection<DebtModel>(DebtsCollectionName);
Expand Down Expand Up @@ -130,15 +131,15 @@ public async Task SynchronizeDebtsAsync()
Dictionary<string, DebtModel> OnlineDebtDict = OnlineDebtList.ToDictionary(x => x.Id, x => x);
Dictionary<string, DebtModel> OfflineDebtDict = OfflineDebtList.ToDictionary(x => x.Id, x => x);

foreach(var itemID in OfflineDebtDict.Keys.Intersect(OnlineDebtDict.Keys))
foreach (var itemID in OfflineDebtDict.Keys.Intersect(OnlineDebtDict.Keys))
{
var offlineItem = OfflineDebtDict[itemID];
var onlineItem = OnlineDebtDict[itemID];
if (offlineItem.UpdateDateTime > onlineItem.UpdateDateTime)
if (offlineItem.UpdateDateTime.ToUniversalTime() > onlineItem.UpdateDateTime.ToUniversalTime())
{
await UpdateDebtOnlineAsync(offlineItem);
}
else if (offlineItem.UpdateDateTime < onlineItem.UpdateDateTime)
else if (offlineItem.UpdateDateTime.ToUniversalTime() < onlineItem.UpdateDateTime.ToUniversalTime())
{
await UpdateDebtAsync(onlineItem);
}
Expand All @@ -162,11 +163,12 @@ public async Task SynchronizeDebtsAsync()
}
catch (Exception ex)
{
Debug.WriteLine("Sync exception" + ex.Message);
Debug.WriteLine("Debts Sync exception" + ex.Message);
}
}
public async Task<bool> AddDebtAsync(DebtModel debt)
{
debt.UpdateDateTime = DateTime.UtcNow;
try
{
using (db = await OpenDB())
Expand Down Expand Up @@ -201,9 +203,11 @@ public async Task<bool> AddDebtAsync(DebtModel debt)
}
public async Task<bool> UpdateDebtAsync(DebtModel debt)
{
debt.UpdateDateTime = DateTime.UtcNow;

try
{
using(db = await OpenDB())
using (db = await OpenDB())
{
if (await AllDebts.UpdateAsync(debt))
{
Expand Down Expand Up @@ -236,10 +240,11 @@ public async Task<bool> UpdateDebtAsync(DebtModel debt)
}
public async Task<bool> DeleteDebtAsync(DebtModel debt)
{
debt.UpdateDateTime = DateTime.UtcNow;
debt.IsDeleted = true;
try
{
using(db = await OpenDB())
using (db = await OpenDB())
{
if (await AllDebts.UpdateAsync(debt))
{
Expand Down Expand Up @@ -284,13 +289,11 @@ async Task DeleteDebtOnlineAsync(DebtModel debtItem)
{
await AllDebtsOnline.ReplaceOneAsync(debt => debt.Id == debtItem.Id, debtItem);
}
public Task DropDebtCollection()
{
throw new NotImplementedException();
}

public Task<bool> SynchronizeDebtAsync(string userEmail, string userPassword)
public async Task DropDebtCollection()
{
throw new NotImplementedException();
await OpenDB();
await db.DropCollectionAsync(DebtsCollectionName);
db.Dispose();
Debug.WriteLine("debts Collection dropped!");
}
}
Loading