You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ShopNCook/LocalEndpoint/RecipesService.cs

55 lines
1.5 KiB

using Endpoint;
using Models;
using System.Collections.Immutable;
namespace LocalEndpoint
{
internal class RecipesService : IRecipesService
{
private readonly RecipesDatabase db;
private readonly Dictionary<Account, AccountData> accountsData = new Dictionary<Account, AccountData>();
public RecipesService(RecipesDatabase db)
{
this.db = db;
}
public ImmutableList<RecipeInfo> 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;
}
}
}