Escali License control 1
PriceListViewModel.cs
Gå til dokumentasjonen til denne filen.
1using AutoMapper;
8using Microsoft.EntityFrameworkCore;
9
11{
13 {
15 private readonly AlertService _alertService;
16
17 public List<PriceListClientModel>? PriceLists { get; private set; }
18
19 public PriceListViewModel(DataContext context, IMapper mapper, AlertService alertService)
20 {
22 _mapper = mapper;
23 _alertService = alertService;
24 }
25
26 public async Task GetPriceLists()
27 {
28 var priceLists = await _priceListService.GetAllPriceLists();
29 UpdatePriceLists(_mapper.Map<List<PriceListClientModel>>(priceLists));
30 }
31
32 public async Task<PriceListClientModel?> CreatePricelist(PriceListClientModel priceList, int priceListToCopy, decimal priceAdjustmentFactor)
33 {
34 var mapped = _mapper.Map<PriceList>(priceList);
35 try
36 {
37 var res = await _priceListService.AddPriceListFromCopy(mapped, priceListToCopy, priceAdjustmentFactor);
38 _alertService.UpdateInfoMessage(new InfoMessage("Ny prisliste lagt til!", InfoMessageType.SUCCESS));
39
40 var updatedPricelists = PriceLists ?? new List<PriceListClientModel>();
41 updatedPricelists.Add(_mapper.Map<PriceListClientModel>(res));
42
43 UpdatePriceLists(updatedPricelists);
44 return _mapper.Map<PriceListClientModel>(res);
45 }
46 catch (DbUpdateException e)
47 {
48 _alertService.UpdateInfoMessage(new InfoMessage("Det oppstod en feil i database.", InfoMessageType.ERROR));
49 return null;
50 }
51 catch (InvalidOperationException e)
52 {
54 return null;
55 }
56 }
57
58 public async Task<PriceListClientModel?> UpdatePricelist(PriceListClientModel priceList)
59 {
60 var mapped = _mapper.Map<PriceList>(priceList);
61 try
62 {
63 var res = await _priceListService.UpdatePriceList(mapped);
64 _alertService.UpdateInfoMessage(new InfoMessage("Prisliste ble oppdatert!", InfoMessageType.SUCCESS));
65
66 var updatedPricelists = PriceLists;
67 updatedPricelists = updatedPricelists!.Select(pl => pl.PriceListSeq != priceList.PriceListSeq ? pl : _mapper.Map<PriceListClientModel>(res)).ToList();
68
69 UpdatePriceLists(updatedPricelists);
70 return _mapper.Map<PriceListClientModel>(res);
71 }
72 catch (DbUpdateException e)
73 {
74 _alertService.UpdateInfoMessage(new InfoMessage("Det oppstod en feil i database.", InfoMessageType.ERROR));
75 return null;
76 }
77 catch (InvalidOperationException e)
78 {
80 return null;
81 }
82 }
83
84 private void UpdatePriceLists(List<PriceListClientModel> priceLists)
85 {
86 PriceLists = priceLists;
88 }
89
90 }
91}
InfoMessageType
var context
Definition: Program.cs:49
async void UpdateInfoMessage(InfoMessage msg)
Definition: AlertService.cs:12
void UpdatePriceLists(List< PriceListClientModel > priceLists)
async Task< PriceListClientModel?> UpdatePricelist(PriceListClientModel priceList)
PriceListViewModel(DataContext context, IMapper mapper, AlertService alertService)
async Task< PriceListClientModel?> CreatePricelist(PriceListClientModel priceList, int priceListToCopy, decimal priceAdjustmentFactor)
PriceListService class inserts and updates PriceList in the 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...