-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMealService.cs
65 lines (57 loc) · 1.65 KB
/
MealService.cs
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
using CallorieCounter.Models;
using System.Collections.Generic;
using System.Linq;
namespace CallorieCounter;
using CallorieCounter.Models;
using System.Collections.Generic;
using System.Linq;
public class MealService : IMealService
{
private static List<Meal> meals = new List<Meal>
{
new Meal { Id = 1, UserId = 1, ProductId = 1, Amount = 150, Date = DateTime.Now },
new Meal { Id = 2, UserId = 2, ProductId = 2, Amount = 200, Date = DateTime.Now }
};
public IEnumerable<Meal> GetMealsForUser(int userId)
{
return meals.Where(m => m.UserId == userId).ToList();
}
public double CalculateMealCalories(Meal meal, Product product)
{
return (meal.Amount / 100) * product.CaloriesPer100g;
}
public Meal GetMealById(int id)
{
var meal = meals.FirstOrDefault(m => m.Id == id);
if (meal == null)
{
throw new InvalidOperationException("Meal not found.");
}
return meal;
}
public Meal CreateMeal(Meal newMeal)
{
newMeal.Id = meals.Count + 1;
meals.Add(newMeal);
return newMeal;
}
public void UpdateMeal(int id, Meal updatedMeal)
{
var meal = meals.FirstOrDefault(m => m.Id == id);
if (meal != null)
{
meal.UserId = updatedMeal.UserId;
meal.ProductId = updatedMeal.ProductId;
meal.Amount = updatedMeal.Amount;
meal.Date = updatedMeal.Date;
}
}
public void DeleteMeal(int id)
{
var meal = meals.FirstOrDefault(m => m.Id == id);
if (meal != null)
{
meals.Remove(meal);
}
}
}