|
|
|
@ -5,6 +5,8 @@ using System.Collections.Immutable;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace LocalEndpoint.Data
|
|
|
|
@ -62,9 +64,11 @@ namespace LocalEndpoint.Data
|
|
|
|
|
Save(ACCOUNTS_FILENAME, ACCOUNTS_SERIALIZER, accountsData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Recipe GetRecipe(Guid id)
|
|
|
|
|
public Recipe? GetRecipe(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return ConvertRecipeDataToRecipe(recipesData[id]);
|
|
|
|
|
if (recipesData.TryGetValue(id, out RecipeData? data))
|
|
|
|
|
return ConvertRecipeDataToRecipe(data);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipeRate GetRecipeRate(Guid user, Guid recipe)
|
|
|
|
@ -72,38 +76,38 @@ namespace LocalEndpoint.Data
|
|
|
|
|
return usersData[user].Rates[recipe];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InsertInUserList(Guid userId, Guid recipeId, uint persAmount)
|
|
|
|
|
public async 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)
|
|
|
|
|
public async void RemoveFromUserList(Guid userId, Guid recipeId)
|
|
|
|
|
{
|
|
|
|
|
usersData[userId].RecipesList.Remove(recipeId);
|
|
|
|
|
Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void InsertRecipe(Recipe recipe)
|
|
|
|
|
public async void InsertRecipe(Recipe recipe)
|
|
|
|
|
{
|
|
|
|
|
recipesData[recipe.Info.Id] = new RecipeData(recipe.Info, recipe.Owner.Id, recipe.Ingredients, recipe.Steps);
|
|
|
|
|
Save(RECIPES_FILENAME, RECIPES_SERIALIZER, recipesData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InsertUser(User user)
|
|
|
|
|
public async void InsertUser(User user)
|
|
|
|
|
{
|
|
|
|
|
usersData[user.Id] = new UserData(user, new Dictionary<Guid, RecipeRate>(), new Dictionary<Guid, uint>());
|
|
|
|
|
Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InsertRate(Guid userId, Guid recipeId, RecipeRate rate)
|
|
|
|
|
public async void InsertRate(Guid userId, Guid recipeId, RecipeRate rate)
|
|
|
|
|
{
|
|
|
|
|
usersData[userId].Rates[recipeId] = rate;
|
|
|
|
|
Save(USERS_FILENAME, USERS_SERIALIZER, usersData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveRecipe(Guid id)
|
|
|
|
|
public async void RemoveRecipe(Guid id)
|
|
|
|
|
{
|
|
|
|
|
recipesData.Remove(id);
|
|
|
|
|
Save(RECIPES_FILENAME, RECIPES_SERIALIZER, recipesData);
|
|
|
|
@ -141,19 +145,16 @@ namespace LocalEndpoint.Data
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Length == 0)
|
|
|
|
|
return new Dictionary<K, V>(); //file is empty thus there is nothing to deserialize
|
|
|
|
|
Console.WriteLine(File.ReadAllText(file));
|
|
|
|
|
|
|
|
|
|
using (var stream = File.OpenRead(file))
|
|
|
|
|
return deserializer.ReadObject(stream) as Dictionary<K, V> ?? throw new Exception("object read from " + file + " is not a dictionnary");
|
|
|
|
|
string text = File.ReadAllText(file);
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<K, V>>(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Save<K, T>(string fileName, DataContractSerializer serializer, Dictionary<K, T> dict)
|
|
|
|
|
private async void Save<K, T>(string fileName, DataContractSerializer serializer, Dictionary<K, T> dict)
|
|
|
|
|
{
|
|
|
|
|
using (var stream = File.OpenWrite(dbPath + "/" + fileName))
|
|
|
|
|
{
|
|
|
|
|
serializer.WriteObject(stream, dict);
|
|
|
|
|
stream.Flush();
|
|
|
|
|
}
|
|
|
|
|
string json = JsonSerializer.Serialize(dict);
|
|
|
|
|
File.WriteAllText(dbPath + "/" + fileName, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|