Escali License control 1
ProductService.cs
Gå til dokumentasjonen til denne filen.
3using Microsoft.EntityFrameworkCore;
4
6{
7
11public class ProductService
12{
13 private readonly DataContext _db;
14
16 {
17 _db = db;
18 }
19
24 public async Task<List<Product>> GetAllProducts()
25 {
26 var res = await _db.Products.ToListAsync();
27 return res;
28 }
29
34 public async Task<List<Product>> GetAllProductWithChildren()
35 {
36 var res = await _db.Products
37 .Include(p => p.PriceLists)
38 .Include(p => p.Modules)
39 .ThenInclude(m => m.ModuleLevels)
40 .ThenInclude(ml => ml.PriceElements)
41 .ThenInclude(pe => pe.PriceList)
42 .ThenInclude(pl => pl.Currency)
43 .Include(p => p.Agreements)
44 .ThenInclude(a => a.AgreementElements)
45 .AsSplitQuery()
46 .ToListAsync();
47 return res;
48 }
49
56 public async Task<Product> GetProductById(int id)
57 {
58 var res = await _db.Products.FindAsync(id);
59 return res;
60 }
61
68 public async Task<Product> AddProduct(Product product)
69 {
70 if (_db.Products.Any(p => p.ProductName == product.ProductName))
71 throw new InvalidOperationException($"Unique name violation.");
72
73 var res = _db.Products.Add(product);
74 await _db.SaveChangesAsync();
75 return res.Entity;
76
77 }
78
86 public async Task<Product> UpdateProduct(Product product)
87 {
88 if (_db.Products.Any(p => p.ProductName == product.ProductName && p.ProductSeq != product.ProductSeq))
89 throw new InvalidOperationException($"Unique name violation.");
90
91 var res = _db.Products.Update(product);
92 await _db.SaveChangesAsync();
93 return res.Entity;
94 }
95
96 }
97}
ProductService class inserts and updates Product in the Database
async Task< Product > GetProductById(int id)
Read one Product with id from database
async Task< List< Product > > GetAllProducts()
Read all Products from database
async Task< Product > UpdateProduct(Product product)
Updates changes on a Product in the database
async Task< List< Product > > GetAllProductWithChildren()
Read all Products from database included referenced tables
async Task< Product > AddProduct(Product product)
Adds a Product to the database