PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MasterDataService.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using cwebplusApp.Shared.Models;
  2. using cwebplusApp.Shared.ResourceFiles;
  3. using Microsoft.Extensions.Localization;
  4. using Microsoft.JSInterop;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. namespace cwebplusApp.Shared.Services {
  10. public class MasterDataService {
  11. private readonly IBicycleRestService bicycleRestService;
  12. private readonly IJSRuntime jsRuntime;
  13. private readonly IStringLocalizer<Resources> i18n;
  14. private readonly OnlineStatusProvider onlineStatusProvider;
  15. private const string KeyNameColors = "colors";
  16. private const string KeyNameBcTypes = "bicycleTypes";
  17. private const string KeyNameBrands = "brands";
  18. private const string KeyNameSearchservices = "searchServices";
  19. private const string KeyNameZipCities = "zipCities";
  20. private bool initializedColors;
  21. private bool initializedBcTypes;
  22. private bool initializedBrands;
  23. private bool initializedSearchServices;
  24. private bool initializedZipCities;
  25. private ColorItem[] colors;
  26. private BicycleType[] bicycleTypes;
  27. private Brand[] brands;
  28. private SearchService[] searchServices;
  29. private ZipCity[] zipCities;
  30. public ColorItem[] Colors {
  31. get => this.colors;
  32. }
  33. public BicycleType[] BicycleTypes {
  34. get => this.bicycleTypes;
  35. }
  36. public Brand[] Brands {
  37. get => this.brands;
  38. }
  39. public ZipCity[] ZipCities {
  40. get => this.zipCities;
  41. }
  42. public SearchService[] SearchServices {
  43. get => this.searchServices;
  44. }
  45. public event EventHandler Changed;
  46. public MasterDataService(IJSRuntime _jsRuntime, IBicycleRestService _bicycleRestService, IStringLocalizer<Resources> _i18n, OnlineStatusProvider _onlineStatusProvider) {
  47. this.jsRuntime = _jsRuntime;
  48. this.bicycleRestService = _bicycleRestService;
  49. this.i18n = _i18n;
  50. this.onlineStatusProvider = _onlineStatusProvider;
  51. this.brands = Defaults.GetBrandDefaults(i18n).ToArray();
  52. this.colors = Defaults.GetColorDefaults(i18n).ToArray();
  53. this.bicycleTypes = Defaults.GetBicycleTypeDefaults(i18n).ToArray();
  54. this.searchServices = Defaults.GetSearchServiceDefaults(i18n).ToArray();
  55. }
  56. public async Task SynchronizeMasterdata() {
  57. if (onlineStatusProvider.Online) {
  58. await SynchronizeColors();
  59. await SynchronizeBcTypes();
  60. await SynchronizeBrands();
  61. await SynchronizeSearchServices();
  62. await SynchronizeZipCities();
  63. } else {
  64. throw new Exception("No internet connection available!");
  65. }
  66. }
  67. public async Task SynchronizeColors() {
  68. ColorItem[] _colors = (await bicycleRestService.GetColors()).ToArray();
  69. if (_colors != null && _colors.Length > 0) {
  70. this.colors = _colors;
  71. await SaveColorsToStorage(this.colors);
  72. };
  73. }
  74. public async Task SynchronizeBcTypes() {
  75. BicycleType[] _bicycleTypes = (await bicycleRestService.GetBicycleTypes()).ToArray();
  76. if (_bicycleTypes != null && _bicycleTypes.Length > 0) {
  77. this.bicycleTypes = _bicycleTypes;
  78. await SaveBcTypesToStorage(this.bicycleTypes);
  79. }
  80. }
  81. public async Task SynchronizeBrands() {
  82. Brand[] _brands = (await bicycleRestService.GetBrands()).ToArray();
  83. if (_brands != null && _brands.Length > 0) {
  84. this.brands = _brands;
  85. await SaveBrandsToStorage(this.brands);
  86. }
  87. }
  88. public async Task SynchronizeSearchServices() {
  89. SearchService[] _searchServices = (await bicycleRestService.GetSearchServices()).ToArray();
  90. if (_searchServices != null && _searchServices.Length > 0) {
  91. this.searchServices = _searchServices;
  92. await SaveSearchServicesToStorage(this.searchServices);
  93. }
  94. }
  95. public async Task SynchronizeZipCities() {
  96. ZipCity[] _zipCities = (await bicycleRestService.GetZipCities()).ToArray();
  97. if (_zipCities != null && _zipCities.Length > 0) {
  98. this.zipCities = _zipCities;
  99. await SaveZipCitiesToStorage(this.zipCities);
  100. }
  101. }
  102. public async Task<ColorItem[]> GetColors() {
  103. ColorItem[] _colors = await GetColorsFromStorage();
  104. if (_colors != null && _colors.Length > 0) {
  105. this.colors = _colors;
  106. }
  107. this.colors = SortColors(new List<ColorItem>(this.colors)).ToArray();
  108. return Colors;
  109. }
  110. public async Task<BicycleType[]> GetBicycleTypes() {
  111. BicycleType[] _bicycleTypes = await GetBicycleTypesFromStorage();
  112. if (_bicycleTypes != null && _bicycleTypes.Length > 0) {
  113. this.bicycleTypes = _bicycleTypes;
  114. }
  115. this.bicycleTypes = SortBicycleTypes(new List<BicycleType>(this.bicycleTypes)).ToArray();
  116. return BicycleTypes;
  117. }
  118. public async Task<Brand[]> GetBrands() {
  119. Brand[] _brands = await GetBrandsFromStorage();
  120. if (_brands != null && _brands.Length > 0) {
  121. this.brands = _brands;
  122. }
  123. this.brands = SortBrands(new List<Brand>(this.brands)).ToArray();
  124. return Brands;
  125. }
  126. public async Task<SearchService[]> GetSearchServices() {
  127. SearchService[] _searchServices = await GetSearchServicesFromStorage();
  128. if (_searchServices != null && _searchServices.Length > 0) {
  129. this.searchServices = _searchServices;
  130. }
  131. this.searchServices = SortSearchServices(new List<SearchService>(this.searchServices)).ToArray();
  132. return SearchServices;
  133. }
  134. public async Task<ZipCity[]> GetZipCities() {
  135. ZipCity[] _zipCities = await GetZipCitiesFromStorage();
  136. if (_zipCities != null && _zipCities.Length > 0) {
  137. this.zipCities = _zipCities;
  138. }
  139. this.zipCities = SortZipCities(new List<ZipCity>(this.zipCities)).ToArray();
  140. return ZipCities;
  141. }
  142. // This method is called from BlazorRegisterStorageEvent when the storage changed
  143. [JSInvokable]
  144. public void OnStorageUpdated(string key) {
  145. // Reset the settings. The next call to Get will reload the data
  146. if (key == KeyNameColors) {
  147. this.colors = null;
  148. Changed?.Invoke(this, EventArgs.Empty);
  149. } else if (key == KeyNameBcTypes) {
  150. this.bicycleTypes = null;
  151. Changed?.Invoke(this, EventArgs.Empty);
  152. } else if (key == KeyNameBrands) {
  153. this.brands = null;
  154. Changed?.Invoke(this, EventArgs.Empty);
  155. }
  156. }
  157. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  158. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  159. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  160. if (!initializedColors) {
  161. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  162. var reference = DotNetObjectReference.Create(this);
  163. await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  164. this.initializedColors = true;
  165. }
  166. // Read the JSON string that contains the data from the local storage
  167. ColorItem[] result;
  168. var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  169. if (String.IsNullOrEmpty(str)) {
  170. result = Array.Empty<ColorItem>();
  171. } else {
  172. result = JsonConvert.DeserializeObject<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
  173. }
  174. return result;
  175. }
  176. private async ValueTask<BicycleType[]> GetBicycleTypesFromStorage() {
  177. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  178. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  179. if (!initializedBcTypes) {
  180. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  181. var reference = DotNetObjectReference.Create(this);
  182. await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  183. this.initializedBcTypes = true;
  184. }
  185. // Read the JSON string that contains the data from the local storage
  186. BicycleType[] result;
  187. var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBcTypes);
  188. if (String.IsNullOrEmpty(str)) {
  189. result = Array.Empty<BicycleType>();
  190. } else {
  191. result = JsonConvert.DeserializeObject<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
  192. }
  193. return result;
  194. }
  195. private async ValueTask<Brand[]> GetBrandsFromStorage() {
  196. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  197. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  198. if (!initializedBrands) {
  199. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  200. var reference = DotNetObjectReference.Create(this);
  201. await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  202. this.initializedBrands = true;
  203. }
  204. // Read the JSON string that contains the data from the local storage
  205. Brand[] result;
  206. var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBrands);
  207. if (String.IsNullOrEmpty(str)) {
  208. result = Array.Empty<Brand>();
  209. } else {
  210. result = JsonConvert.DeserializeObject<Brand[]>(str) ?? Array.Empty<Brand>();
  211. }
  212. return result;
  213. }
  214. private async ValueTask<SearchService[]> GetSearchServicesFromStorage() {
  215. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  216. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  217. if (!initializedSearchServices) {
  218. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  219. var reference = DotNetObjectReference.Create(this);
  220. await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  221. this.initializedSearchServices = true;
  222. }
  223. // Read the JSON string that contains the data from the local storage
  224. SearchService[] result;
  225. var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameSearchservices);
  226. if (String.IsNullOrEmpty(str)) {
  227. result = Array.Empty<SearchService>();
  228. } else {
  229. result = JsonConvert.DeserializeObject<SearchService[]>(str) ?? Array.Empty<SearchService>();
  230. }
  231. return result;
  232. }
  233. private async ValueTask<ZipCity[]> GetZipCitiesFromStorage() {
  234. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  235. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  236. if (!this.initializedZipCities) {
  237. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  238. var reference = DotNetObjectReference.Create(this);
  239. await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  240. this.initializedZipCities = true;
  241. }
  242. // Read the JSON string that contains the data from the local storage
  243. ZipCity[] result;
  244. var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameZipCities);
  245. if (String.IsNullOrEmpty(str)) {
  246. result = Array.Empty<ZipCity>();
  247. } else {
  248. result = JsonConvert.DeserializeObject<ZipCity[]>(str) ?? Array.Empty<ZipCity>();
  249. }
  250. return result;
  251. }
  252. private async Task SaveColorsToStorage(ColorItem[] colors) {
  253. var json = JsonConvert.SerializeObject(colors);
  254. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  255. }
  256. private async Task SaveBcTypesToStorage(BicycleType[] bicycleTypes) {
  257. var json = JsonConvert.SerializeObject(bicycleTypes);
  258. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
  259. }
  260. private async Task SaveBrandsToStorage(Brand[] brands) {
  261. var json = JsonConvert.SerializeObject(brands);
  262. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json);
  263. }
  264. private async Task SaveSearchServicesToStorage(SearchService[] searchServices) {
  265. var json = JsonConvert.SerializeObject(searchServices);
  266. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameSearchservices, json);
  267. }
  268. private async Task SaveZipCitiesToStorage(ZipCity[] zipCities) {
  269. var json = JsonConvert.SerializeObject(zipCities);
  270. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameZipCities, json);
  271. }
  272. private static List<ColorItem> SortColors(List<ColorItem> colors) {
  273. colors.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? 1 : c2.Id == 0 ? -1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); });
  274. return colors;
  275. }
  276. private static List<BicycleType> SortBicycleTypes(List<BicycleType> bcTypes) {
  277. bcTypes.Sort(delegate (BicycleType bct1, BicycleType bct2) { return bct1.Id == 0 ? 1 : bct2.Id == 0 ? -1 : bct1.Bezeichnung.CompareTo(bct2.Bezeichnung); });
  278. return bcTypes;
  279. }
  280. private static List<Brand> SortBrands(List<Brand> brands) {
  281. brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? 1 : b2.Id == 0 ? -1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); });
  282. return brands;
  283. }
  284. private static List<SearchService> SortSearchServices(List<SearchService> searchServices) {
  285. searchServices.Sort(delegate (SearchService ss1, SearchService ss2) { return ss1.Id == 0 ? -1 : ss2.Id == 0 ? 1 : ss1.Bezeichnung.CompareTo(ss2.Bezeichnung); });
  286. return searchServices;
  287. }
  288. private static List<ZipCity> SortZipCities(List<ZipCity> zipCities) {
  289. int v = int.Parse("22");
  290. zipCities.Sort(delegate (ZipCity zc1, ZipCity zc2) { return int.Parse(zc1.Zip).CompareTo(int.Parse(zc2.Zip)); });
  291. return zipCities;
  292. }
  293. }
  294. }