//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\ #region --Attributes-- #endregion //--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public override void Add() { using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.Add(this); } }
public override void Update() { using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.Update(this); } }
public async Task <IEnumerable <Dish> > LoadDishesAsync(string canteenId, DateTime date) { // Wait for the old update to finish first: if (!(updateTask is null) && !updateTask.IsCompleted) { await updateTask.ConfAwaitFalse(); } List <Dish> dishes; using (CanteensDbContext ctx = new CanteensDbContext()) { dishes = ctx.Dishes.Where(d => string.Equals(d.CanteenId, canteenId) && d.Date.Date.CompareTo(date.Date) == 0).Include(ctx.GetIncludePaths(typeof(Dish))).ToList(); } // Sort dishes, so side dishes are at the end: dishes.Sort((a, b) => { if (a.IsSideDish && b.IsSideDish) { return(0); } if (a.IsSideDish) { return(1); } if (b.IsSideDish) { return(-1); } return(0); }); return(dishes); }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task UpdateAsync(string canteenId, bool force) { // Wait for the old update to finish first: if (updateTask is null || updateTask.IsCompleted) { updateTask = Task.Run(async() => { // Get current language: Language lang = CanteensDbContext.GetActiveLanguage(); if (lang is null) { Logger.Error("Failed to download dishes. No language available."); return; } Logger.Debug($"Dish language: {lang.Name}"); if (!force && CacheDbContext.IsCacheEntryValid(BuildCanteenDishUrl(canteenId, lang))) { Logger.Info("No need to fetch dishes. Cache is still valid."); return; } IEnumerable <Dish> dishes = await DownloadDishesAsync(canteenId, lang); if (!(dishes is null)) { using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.RemoveRange(ctx.Dishes.Where(d => string.Equals(d.CanteenId, canteenId))); ctx.AddRange(dishes); } CacheDbContext.UpdateCacheEntry(BuildCanteenDishUrl(canteenId, lang), DateTime.Now.Add(MAX_TIME_IN_CACHE)); } }); } await updateTask.ConfAwaitFalse(); }
/// <summary> /// Returns the previous date a dish for the given <paramref name="canteenId"/> was found after the given <paramref name="date"/>. /// In case no date was found. <see cref="DateTime.MinValue"/> will be returned. /// </summary> public DateTime GetPrevDate(string canteenId, DateTime date) { Dish dish = null; using (CanteensDbContext ctx = new CanteensDbContext()) { dish = ctx.Dishes.Where(d => d.Date.Date < date.Date && string.Equals(d.CanteenId, canteenId)).OrderByDescending(d => d.Date).FirstOrDefault(); } return(dish is null ? DateTime.MinValue : dish.Date); }
/// <summary> /// Returns the next date a dish for the given <paramref name="canteenId"/> was found after the given <paramref name="date"/>. /// In case no date was found. <see cref="DateTime.MaxValue"/> will be returned. /// </summary> public DateTime GetNextDate(string canteenId, DateTime date) { DateTime dish = DateTime.MaxValue; using (CanteensDbContext ctx = new CanteensDbContext()) { dish = ctx.Dishes.Where(d => d.Date.Date > date.Date && string.Equals(d.CanteenId, canteenId)).Select(d => d.Date).OrderBy(d => d).FirstOrDefault(); } return(dish == DateTime.MinValue ? DateTime.MaxValue : dish.Date); }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- private void MigrateActiveLanguage(List <Language> languages) { Language activeLang = CanteensDbContext.GetActiveLanguage(); if (activeLang is null) { bool foundEnglish = false; for (int i = 0; i < languages.Count(); i++) { if (string.Equals(languages[i].Name, "EN")) { languages[i].Active = true; foundEnglish = true; break; } } foreach (Language lang in languages) { if (string.Equals(lang.Name, "EN")) { lang.Active = true; foundEnglish = true; break; } } if (!foundEnglish) { Logger.Warn("English not found as language for setting it as default language for canteens in result."); languages.First().Active = true; } } else { bool foundActiveLanguage = false; foreach (Language lang in languages) { if (string.Equals(lang.Name, activeLang.Name)) { lang.Active = true; foundActiveLanguage = true; break; } } if (!foundActiveLanguage) { Logger.Warn($"Active language {activeLang.Name} not found as language for setting it as default language for canteens in result."); languages.First().Active = true; } } }
public async Task <IEnumerable <Label> > UpdateLabelsAsync(bool force) { // Wait for the old update to finish first: if (!(updateLabelsTask is null) && !updateLabelsTask.IsCompleted) { return(await updateLabelsTask.ConfAwaitFalse()); } updateLabelsTask = Task.Run(async() => { IEnumerable <Label> labels = null; if (!force && CacheDbContext.IsCacheEntryValid(LABELS_URI.ToString())) { Logger.Info("No need to fetch labels. Cache is still valid."); using (CanteensDbContext ctx = new CanteensDbContext()) { labels = ctx.Labels.Include(ctx.GetIncludePaths(typeof(Label))).ToList(); } if (LABEL_CACHE.Count <= 0) { UpdateLabelCache(labels); } } labels = await DownloadLabelsAsync(); if (!(labels is null)) { using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.RemoveRange(ctx.Labels); ctx.RemoveRange(ctx.LabelTranslations); ctx.AddRange(labels); } CacheDbContext.UpdateCacheEntry(LABELS_URI.ToString(), DateTime.Now.Add(MAX_TIME_IN_CACHE)); UpdateLabelCache(labels); return(labels); } Logger.Info("Failed to retrieve labels. Returning from DB."); using (CanteensDbContext ctx = new CanteensDbContext()) { labels = ctx.Labels.Include(ctx.GetIncludePaths(typeof(Label))).ToList(); } if (LABEL_CACHE.Count <= 0) { UpdateLabelCache(labels); } return(labels); }); return(await updateLabelsTask.ConfAwaitFalse()); }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task <IEnumerable <Canteen> > UpdateCanteensAsync(bool force) { // Wait for the old update to finish first: if (!(updateCanteensTask is null) && !updateCanteensTask.IsCompleted) { return(await updateCanteensTask.ConfAwaitFalse()); } updateCanteensTask = Task.Run(async() => { // Get current language: Language lang = CanteensDbContext.GetActiveLanguage(); if (lang is null) { Logger.Error("Failed to download canteens. No language available."); return(new List <Canteen>()); } Logger.Debug($"Canteen language: {lang.Name}"); if (!force && CacheDbContext.IsCacheEntryValid(CANTEENS_URI_POSTDIX + '_' + lang.BaseUrl)) { Logger.Info("No need to fetch canteens. Cache is still valid."); using (CanteensDbContext ctx = new CanteensDbContext()) { return(ctx.Canteens.Include(ctx.GetIncludePaths(typeof(Canteen))).ToList()); } } IEnumerable <Canteen> canteens = await DownloadCanteensAsync(lang); if (!(canteens is null)) { using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.RemoveRange(ctx.Canteens); ctx.AddRange(canteens); } CacheDbContext.UpdateCacheEntry(CANTEENS_URI_POSTDIX + '_' + lang.BaseUrl, DateTime.Now.Add(MAX_TIME_IN_CACHE)); return(canteens); } Logger.Info("Failed to retrieve canteens. Returning from DB."); using (CanteensDbContext ctx = new CanteensDbContext()) { return(ctx.Canteens.Include(ctx.GetIncludePaths(typeof(Canteen))).ToList()); } }); return(await updateCanteensTask.ConfAwaitFalse()); }
public async Task <IEnumerable <Language> > UpdateLanguagesAsync(bool force) { // Wait for the old update to finish first: if (!(updateLanguagesTask is null) && !updateLanguagesTask.IsCompleted) { return(await updateLanguagesTask.ConfAwaitFalse()); } updateLanguagesTask = Task.Run(async() => { if (!force && CacheDbContext.IsCacheEntryValid(LANGUAGES_URI.ToString())) { Logger.Info("No need to fetch canteen languages. Cache is still valid."); using (CanteensDbContext ctx = new CanteensDbContext()) { return(ctx.Languages.Include(ctx.GetIncludePaths(typeof(Language))).ToList()); } } IEnumerable <Language> languages = await DownloadLanguagesAsync(); if (!(languages is null)) { // Keep currently selected language or select english per default: List <Language> langList = languages.ToList(); MigrateActiveLanguage(langList); using (CanteensDbContext ctx = new CanteensDbContext()) { ctx.RemoveRange(ctx.Languages); ctx.AddRange(langList); } CacheDbContext.UpdateCacheEntry(LANGUAGES_URI.ToString(), DateTime.Now.Add(MAX_TIME_IN_CACHE)); return((IEnumerable <Language>)langList); } Logger.Info("Failed to retrieve canteen languages. Returning from DB."); using (CanteensDbContext ctx = new CanteensDbContext()) { return(ctx.Languages.Include(ctx.GetIncludePaths(typeof(Language))).ToList()); } }); return(await updateLanguagesTask.ConfAwaitFalse()); }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- /// <summary> /// Gets called on App start and performs update task e.g. migrate the DB to a new format. /// </summary> #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public static async Task OnAppStartAsync() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { PackageVersion versionLastStart = GetLastStartedVersion(); // Check if version != 0.0.0.0 => first ever start of the app: if (!(versionLastStart.Major == 0 && versionLastStart.Major == versionLastStart.Minor && versionLastStart.Minor == versionLastStart.Revision && versionLastStart.Revision == versionLastStart.Build) || Settings.GetSettingBoolean(SettingsConsts.INITIALLY_STARTED)) { if (!Compare(versionLastStart, GetPackageVersion())) { // Total refactoring in version 2.0.0.0: if (versionLastStart.Major < 2) { Logger.Info("Started updating to version 2.0.0.0."); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.0.0.0."); } // DB layout for dishes changed in 2.1.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 1) { Logger.Info("Started updating to version 2.1.0.0."); Logger.Info("Resetting canteens DB..."); try { StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("canteens.db"); if (!(file is null)) { await file.DeleteAsync(); } } catch (Exception e) { Logger.Error("Failed to remove old canteens DB with:", e); } Logger.Info("Updating canteens and dishes..."); await CanteenManager.INSTANCE.UpdateCanteensAsync(true); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.1.0.0."); } // DB layout for dishes changed in 2.2.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 2) { Logger.Info("Started updating to version 2.2.0.0."); Logger.Info("Resetting canteens DB..."); try { StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("canteens.db"); if (!(file is null)) { await file.DeleteAsync(); } } catch (Exception e) { Logger.Error("Failed to remove old canteens DB with:", e); } Logger.Info("Updating canteens and dishes..."); await CanteenManager.INSTANCE.UpdateCanteensAsync(true); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.2.0.0."); } // DB layout for TUMonline changed in 2.3.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 3) { Logger.Info("Started updating to version 2.3.0.0."); using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { await ctx.RecreateDbAsync(); } CacheDbContext.ClearCache(); Logger.Info("Finished updating to version 2.2.0.0."); } // New tables for the canteen DB in 2.4.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 4) { Logger.Info("Started updating to version 2.4.0.0."); using (CanteensDbContext ctx = new CanteensDbContext()) { await ctx.RecreateDbAsync(); } CacheDbContext.ClearCache(); Logger.Info("Finished updating to version 2.4.0.0."); } } } SetVersion(GetPackageVersion()); }