forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeRepository.cs
More file actions
93 lines (77 loc) · 2.24 KB
/
Copy pathRecipeRepository.cs
File metadata and controls
93 lines (77 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using RecipeSharing.Domain.Entities;
namespace RecipeSharing.Infrastructure.Persistence;
/// <summary>
/// Interface for recipe repository operations.
/// </summary>
public interface IRecipeRepository
{
/// <summary>
/// Gets all recipes from the repository.
/// </summary>
Task<IEnumerable<Recipe>> GetAllAsync();
/// <summary>
/// Gets a specific recipe by identifier.
/// </summary>
Task<Recipe?> GetByIdAsync(int id);
/// <summary>
/// Adds a new recipe to the repository.
/// </summary>
Task<Recipe> AddAsync(Recipe recipe);
/// <summary>
/// Updates an existing recipe in the repository.
/// </summary>
Task<Recipe> UpdateAsync(Recipe recipe);
/// <summary>
/// Deletes a recipe from the repository.
/// </summary>
Task DeleteAsync(int id);
/// <summary>
/// Saves all pending changes.
/// </summary>
Task SaveChangesAsync();
}
/// <summary>
/// In-memory implementation of the recipe repository.
/// This is a placeholder for future database implementations.
/// </summary>
public sealed class InMemoryRecipeRepository : IRecipeRepository
{
private readonly Dictionary<int, Recipe> _recipes = [];
private int _nextId = 1;
public Task<IEnumerable<Recipe>> GetAllAsync()
{
return Task.FromResult(_recipes.Values.AsEnumerable());
}
public Task<Recipe?> GetByIdAsync(int id)
{
_recipes.TryGetValue(id, out var recipe);
return Task.FromResult(recipe);
}
public Task<Recipe> AddAsync(Recipe recipe)
{
recipe.Id = _nextId++;
_recipes[recipe.Id] = recipe;
return Task.FromResult(recipe);
}
public Task<Recipe> UpdateAsync(Recipe recipe)
{
if (!_recipes.ContainsKey(recipe.Id))
{
throw new InvalidOperationException($"Recipe with ID {recipe.Id} not found.");
}
_recipes[recipe.Id] = recipe;
return Task.FromResult(recipe);
}
public Task DeleteAsync(int id)
{
if (!_recipes.Remove(id))
{
throw new InvalidOperationException($"Recipe with ID {id} not found.");
}
return Task.CompletedTask;
}
public Task SaveChangesAsync()
{
return Task.CompletedTask;
}
}