Escali License control 1
PriceListService.cs
Gå til dokumentasjonen til denne filen.
4using Microsoft.EntityFrameworkCore;
5
7{
8
12public class PriceListService
13{
14
15 private readonly DataContext _db;
16
18 {
19 _db = db;
20 }
21
26 public async Task<List<PriceList>> GetAllPriceLists()
27 {
28 var res = await _db.PriceLists
29 .Include(pl => pl.PriceElements)
30 .Include(pl => pl.Currency)
31 .ToListAsync();
32 return res;
33 }
34
41 public async Task<PriceList> GetPriceListById(int id)
42 {
43 var res = await _db.PriceLists.FindAsync(id);
44 return res;
45 }
46
54 public async Task<PriceList> AddPriceListFromCopy(PriceList priceList, int priceListToCopy, decimal priceAdjustmentFactor)
55 {
56 if (_db.PriceLists.Any(p => p.PriceListName == priceList.PriceListName))
57 throw new InvalidOperationException($"Prisliste med navn {priceList.PriceListName} finnes allerede.");
58
59 if (priceList.DateFrom != null && priceList.DateTo != null)
60 if (!ServiceUtils.DateFromIsBeforeDateTo(priceList.DateFrom.Value, priceList.DateTo.Value))
61 throw new InvalidOperationException("Sluttdato må være etter startdato.");
62
63 // priceList.CurrencySeq = await GetCurrencySeq(priceList.Currency.CurrencyName);
64 // priceList.Currency = null;
65
66 var res = _db.PriceLists.Add(priceList);
67 await _db.SaveChangesAsync();
68 var savedPriceList = res.Entity;
69
70 var copy = await _db.PriceLists.Include(pl => pl.PriceElements).Where(pl => pl.PriceListSeq == priceListToCopy).ToListAsync();
71 if (!copy.Any())
72 {
73 throw new InvalidOperationException("Prisliste for utgangspunkt finnes ikke i databasen.");
74 }
75
76 var copiedPriceElements = new List<PriceElement>();
77 copy.First().PriceElements.ForEach(pe =>
78 {
79 var priceElementCopy = new PriceElement()
80 {
81 ModuleLevelSeq = pe.ModuleLevelSeq,
82 Price = pe.Price * priceAdjustmentFactor,
83 IsActive = pe.IsActive,
84 PriceListSeq = savedPriceList.PriceListSeq
85 };
86 copiedPriceElements.Add(priceElementCopy);
87 });
88
89 savedPriceList.PriceElements = copiedPriceElements;
90 await _db.SaveChangesAsync();
91
92 return res.Entity;
93
94 }
95
103 public async Task<PriceList> AddPriceList(PriceList priceList)
104 {
105 if (_db.PriceLists.Any(p => p.PriceListName == priceList.PriceListName))
106 throw new InvalidOperationException($"Prisliste med navn {priceList.PriceListName} finnes allerede.");
107
108 if (priceList.DateFrom != null && priceList.DateTo != null)
109 if (!ServiceUtils.DateFromIsBeforeDateTo(priceList.DateFrom.Value, priceList.DateTo.Value))
110 throw new InvalidOperationException("Sluttdato må være etter startdato.");
111
112 // priceList.CurrencySeq = await GetCurrencySeq(priceList.Currency.CurrencyName);
113 // priceList.Currency = null;
114
115 var res = _db.PriceLists.Add(priceList);
116 await _db.SaveChangesAsync();
117 return res.Entity;
118 }
119
127 public async Task<PriceList> UpdatePriceList(PriceList priceList)
128 {
129 if (_db.PriceLists.Any(p => p.PriceListName == priceList.PriceListName && p.PriceListSeq != priceList.PriceListSeq))
130 throw new InvalidOperationException($"Prisliste med navn {priceList.PriceListName} finnes allerede.");
131
132 if (priceList.DateFrom != null && priceList.DateTo != null)
133 if (!ServiceUtils.DateFromIsBeforeDateTo(priceList.DateFrom.Value, priceList.DateTo.Value))
134 throw new InvalidOperationException("Sluttdato må være etter startdato.");
135
136 // priceList.CurrencySeq = await GetCurrencySeq(priceList.Currency.CurrencyName);
137 // priceList.Currency = null;
138
139 _db.Entry(priceList).State = EntityState.Modified;
140 await _db.SaveChangesAsync();
141 _db.ChangeTracker.Clear();
142
143 priceList.Currency = await _db.Currencies.FindAsync(priceList.CurrencySeq);
144
145 return priceList;
146 }
147
148 }
149}
PriceListService class inserts and updates PriceList in the Database
async Task< PriceList > AddPriceList(PriceList priceList)
Adds a PriceList to the database
async Task< PriceList > GetPriceListById(int id)
Read one PriceList with id from database
async Task< List< PriceList > > GetAllPriceLists()
Read all PriceLists from database
async Task< PriceList > UpdatePriceList(PriceList priceList)
Updates changes on a PriceList in the database
async Task< PriceList > AddPriceListFromCopy(PriceList priceList, int priceListToCopy, decimal priceAdjustmentFactor)
Adds a PriceList to the database. Copies the PriceElements from priceListToCopy and multiply prices w...
static bool DateFromIsBeforeDateTo(DateTime dateFrom, DateTime dateTo)
Compares dateFrom and dateTo
Definition: ServiceUtils.cs:11