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