persist 'MyList' of users
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

pull/52/head
maxime.BATISTA@etu.uca.fr 2 years ago
parent d3f956674a
commit 503246f488

@ -25,7 +25,7 @@ public partial class App : Application, ConnectionObserver, IApp
public void ForceLogin() public void ForceLogin()
{ {
Shell shell = new ConnectAppShell(this, Endpoint.AccountManager, Notifier); Shell shell = new ConnectAppShell(this, Endpoint.AuthService, Notifier);
shell.GoToAsync("//Splash"); shell.GoToAsync("//Splash");
MainPage = shell; MainPage = shell;
} }

@ -8,9 +8,6 @@ namespace LocalEndpoint
internal class AccountRecipesPreferences : IAccountRecipesPreferences internal class AccountRecipesPreferences : IAccountRecipesPreferences
{ {
//Binds a recipe's id to its amount of person stored in the account's weekly list
private readonly Dictionary<Guid, uint> weekly = new Dictionary<Guid, uint>();
private readonly Database db; private readonly Database db;
public AccountRecipesPreferences(Account account, Database db) public AccountRecipesPreferences(Account account, Database db)
{ {
@ -43,10 +40,12 @@ namespace LocalEndpoint
public ImmutableList<(RecipeInfo, uint)> GetWeeklyList() public ImmutableList<(RecipeInfo, uint)> GetWeeklyList()
{ {
var weeklyDict = db.GetRecipeListOf(Account.User.Id);
List<(RecipeInfo, uint)> weekly = new List<(RecipeInfo, uint)>(); List<(RecipeInfo, uint)> weekly = new List<(RecipeInfo, uint)>();
foreach (Recipe recipe in db.ListAllRecipes()) foreach (Recipe recipe in db.ListAllRecipes())
{ {
if (this.weekly.TryGetValue(recipe.Info.Id, out uint personAmmount)) if (weeklyDict.TryGetValue(recipe.Info.Id, out uint personAmmount))
{ {
weekly.Add((recipe.Info, personAmmount)); weekly.Add((recipe.Info, personAmmount));
} }
@ -70,10 +69,11 @@ namespace LocalEndpoint
public bool AddToWeeklyList(RecipeInfo info, uint persAmount) public bool AddToWeeklyList(RecipeInfo info, uint persAmount)
{ {
if (weekly.ContainsKey(info.Id)) var weeklyDict = db.GetRecipeListOf(Account.User.Id);
if (weeklyDict.ContainsKey(info.Id))
return false; return false;
weekly[info.Id] = persAmount; db.InsertInUserList(Account.User.Id, info.Id, persAmount);
return true; return true;
} }

@ -72,6 +72,18 @@ namespace LocalEndpoint.Data
return usersData[user].Rates[recipe]; return usersData[user].Rates[recipe];
} }
public void InsertInUserList(Guid userId, Guid recipeId, uint persAmount)
{
usersData[userId].RecipesList[recipeId] = persAmount;
Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
}
public void RemoveFromUserList(Guid userId, Guid recipeId)
{
usersData[userId].RecipesList.Remove(recipeId);
Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
}
public void InsertRecipe(Recipe recipe) public void InsertRecipe(Recipe recipe)
{ {
@ -81,7 +93,7 @@ namespace LocalEndpoint.Data
public void InsertUser(User user) public void InsertUser(User user)
{ {
usersData[user.Id] = new UserData(user, new Dictionary<Guid, RecipeRate>()); usersData[user.Id] = new UserData(user, new Dictionary<Guid, RecipeRate>(), new Dictionary<Guid, uint>());
Save(USERS_FILENAME, USERS_SERIALIZER, usersData); Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
} }
@ -108,6 +120,11 @@ namespace LocalEndpoint.Data
return usersData[user].Rates.ToImmutableDictionary(); return usersData[user].Rates.ToImmutableDictionary();
} }
public ImmutableDictionary<Guid, uint> GetRecipeListOf(Guid user)
{
return usersData[user].RecipesList.ToImmutableDictionary();
}
private Recipe ConvertRecipeDataToRecipe(RecipeData rd) private Recipe ConvertRecipeDataToRecipe(RecipeData rd)
{ {
var owner = usersData[rd.OwnerID].User; var owner = usersData[rd.OwnerID].User;

@ -15,6 +15,8 @@ namespace LocalEndpoint.Data
public Account? GetAccount(string email, string passwordHash); public Account? GetAccount(string email, string passwordHash);
public void InsertInUserList(Guid userId, Guid recipeId, uint persAmount);
public void RemoveFromUserList(Guid userId, Guid recipeId);
public void InsertAccount(Account account, string passwordHash); public void InsertAccount(Account account, string passwordHash);
public void InsertRecipe(Recipe recipe); public void InsertRecipe(Recipe recipe);
@ -28,5 +30,7 @@ namespace LocalEndpoint.Data
public ImmutableList<Recipe> ListAllRecipes(); public ImmutableList<Recipe> ListAllRecipes();
public ImmutableDictionary<Guid, RecipeRate> ListRatesOf(Guid user); public ImmutableDictionary<Guid, RecipeRate> ListRatesOf(Guid user);
public ImmutableDictionary<Guid, uint> GetRecipeListOf(Guid user);
} }
} }

@ -8,6 +8,7 @@ namespace LocalEndpoint.Data
[DataContract] [DataContract]
internal record UserData( internal record UserData(
[property: DataMember] User User, [property: DataMember] User User,
[property: DataMember] Dictionary<Guid, RecipeRate> Rates [property: DataMember] Dictionary<Guid, RecipeRate> Rates,
[property: DataMember] Dictionary<Guid, uint> RecipesList
); );
} }

@ -27,7 +27,7 @@ namespace LocalEndpoint
authService = new AuthService(db); authService = new AuthService(db);
} }
public IAuthService AccountManager => authService; public IAuthService AuthService => authService;
public IRecipesService RecipesService => recipesService; public IRecipesService RecipesService => recipesService;

@ -4,7 +4,7 @@ namespace Endpoint
{ {
public interface IEndpoint public interface IEndpoint
{ {
public IAuthService AccountManager { get; } public IAuthService AuthService { get; }
public IRecipesService RecipesService { get; } public IRecipesService RecipesService { get; }

@ -1,11 +1,6 @@
using LocalEndpoint; using LocalEndpoint;
using Models; using Models;
using System;
using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Endpoint namespace Endpoint
{ {

Loading…
Cancel
Save