using Endpoint; using Models; using System.Collections.Immutable; namespace LocalEndpoint { internal class RecipesService : IRecipesService { private readonly RecipesDatabase db; private readonly Dictionary accountsData = new Dictionary(); public RecipesService(RecipesDatabase db) { this.db = db; } public ImmutableList PopularRecipes() { return db.ListAll().Take(4).ToImmutableList().ConvertAll(v => v.Info); } public Recipe GetRecipe(RecipeInfo info) { return db.Get(info.Id); } public IAccountOwnedRecipes GetRecipesOf(Account account) { return GetOrInitData(account).Recipes; } public IAccountRecipesPreferences GetPreferencesOf(Account account) { return GetOrInitData(account).Preferences; } private AccountData GetOrInitData(Account account) { AccountData? data; accountsData.TryGetValue(account, out data); if (data == null) { AccountOwnedRecipes recipes = new AccountOwnedRecipes(account, db); AccountRecipesPreferences preferences = new AccountRecipesPreferences(account, db); data = new AccountData(recipes, preferences); accountsData.Add(account, data); } return data; } } }