Esempio n. 1
0
        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());
        }
Esempio n. 2
0
        //--------------------------------------------------------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());
        }
Esempio n. 3
0
        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());
        }
Esempio n. 4
0
        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);
        }