Escali License control 1
PriceElementService.cs
Gå til dokumentasjonen til denne filen.
3using Microsoft.EntityFrameworkCore;
4
6{
7
12{
13
14 private readonly DataContext _db;
15
17 {
18 _db = db;
19 }
20
25 public async Task<List<PriceElement>> GetAllPriceElements()
26 {
27 var res = await _db.PriceElements
28 .Include(pe => pe.PriceList)
29 .ThenInclude(pl => pl.Currency)
30 .ToListAsync();
31 return res;
32 }
33
40 public async Task<PriceElement> GetPriceElementById(int id)
41 {
42 var res = await _db.PriceElements.FindAsync(id);
43 return res;
44 }
45
52 public async Task<PriceElement> AddPriceElement(PriceElement priceElement)
53 {
54 var res = _db.PriceElements.Add(priceElement);
55 await _db.SaveChangesAsync();
56 return res.Entity;
57 }
58
66 public async Task<PriceElement> UpdatePriceElement(PriceElement priceElement)
67 {
68 var res = _db.PriceElements.Update(priceElement);
69 await _db.SaveChangesAsync();
70 return res.Entity;
71 }
72
79 public async Task<PriceElement> DeactivatePriceElement(int id)
80 {
81 var priceElement = await _db.PriceElements.FindAsync(id);
82 if (priceElement == null) throw new NullReferenceException($"PriceElement with id {id} doesn't exist.");
83 priceElement.IsActive = false;
84 var res = await UpdatePriceElement(priceElement);
85 return res;
86 }
87
94 public async Task<PriceElement> ActivatePriceElement(int id)
95 {
96 var priceElement = await _db.PriceElements.FindAsync(id);
97 if (priceElement == null) throw new NullReferenceException($"PriceElement with id {id} doesn't exist.");
98 priceElement.IsActive = true;
99 var res = await UpdatePriceElement(priceElement);
100 return res;
101 }
102
103 }
104}
PriceElementService class inserts and updates PriceElement in the Database
async Task< PriceElement > ActivatePriceElement(int id)
Activates a PriceElement by setting IsActive attribute to false
async Task< PriceElement > GetPriceElementById(int id)
Read one PriceElement with id from database
async Task< PriceElement > AddPriceElement(PriceElement priceElement)
Adds a PriceElement to the database
async Task< PriceElement > DeactivatePriceElement(int id)
Deactivates a PriceElement by setting IsActive attribute to false
async Task< List< PriceElement > > GetAllPriceElements()
Read all PriceElements from database
async Task< PriceElement > UpdatePriceElement(PriceElement priceElement)
Updates changes on a PriceElement in the database