-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeService.cs
More file actions
54 lines (46 loc) · 1.43 KB
/
RecipeService.cs
File metadata and controls
54 lines (46 loc) · 1.43 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
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Recipe_Generator.Services
{
public class RecipeService : IRecipeService
{
private readonly RecipeDbContext _context;
public RecipeService(RecipeDbContext context)
{
_context = context;
}
public async Task AddRecipeAsync(Recipe recipe)
{
_context.Recipes.Add(recipe);
await _context.SaveChangesAsync();
}
public async Task DeleteRecipeAsync(int id)
{
var recipe = await _context.Recipes.FindAsync(id);
if (recipe != null)
{
_context.Recipes.Remove(recipe);
await _context.SaveChangesAsync();
}
}
public async Task<List<string>> GetCategoriesAsync()
{
return await _context.Recipes.Select(r => r.Category).Distinct().ToListAsync();
}
public async Task<Recipe?> GetRecipeByIdAsync(int id)
{
return await _context.Recipes.FindAsync(id);
}
public async Task<List<Recipe>> GetRecipesAsync()
{
return await _context.Recipes.ToListAsync();
}
public async Task UpdateRecipeAsync(Recipe recipe)
{
_context.Recipes.Update(recipe);
await _context.SaveChangesAsync();
}
}
}