-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductService.cs
54 lines (48 loc) · 1.48 KB
/
ProductService.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
using CallorieCounter.Models;
using System.Collections.Generic;
using System.Linq;
namespace CallorieCounter
{
using CallorieCounter.Models;
using System.Collections.Generic;
using System.Linq;
public class ProductService : IProductService
{
private static List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Яблоко", CaloriesPer100g = 52 },
new Product { Id = 2, Name = "Банан", CaloriesPer100g = 89 }
};
public IEnumerable<Product> GetProducts()
{
return products.ToList();
}
public Product GetProductById(int id)
{
return products.FirstOrDefault(p => p.Id == id);
}
public Product CreateProduct(Product newProduct)
{
newProduct.Id = products.Count + 1;
products.Add(newProduct);
return newProduct;
}
public void UpdateProduct(int id, Product updatedProduct)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
{
product.Name = updatedProduct.Name;
product.CaloriesPer100g = updatedProduct.CaloriesPer100g;
}
}
public void DeleteProduct(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
{
products.Remove(product);
}
}
}
}