Ejemplo n.º 1
0
        //--------------------------------------------------------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();
        }
Ejemplo n.º 2
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <List <NewsSource> > UpdateNewsSourcesAsync(bool force)
        {
            // Wait for the old update to finish first:
            if (updateTask is null || updateTask.IsCompleted)
            {
                updateTask = Task.Run(async() =>
                {
                    List <NewsSource> newsSources;
                    if (!force && CacheDbContext.IsCacheEntryValid(NEWS_SOURCES_URI.ToString()))
                    {
                        Logger.Info("No need to fetch news sources. Cache is still valid.");
                        using (NewsDbContext ctx = new NewsDbContext())
                        {
                            newsSources = ctx.NewsSources.ToList();
                        }
                    }
                    else
                    {
                        newsSources = await DownloadNewsSourcesAsync();
                        if (!(newsSources is null))
                        {
                            using (NewsDbContext ctx = new NewsDbContext())
                            {
                                ctx.RemoveRange(ctx.NewsSources);
                                ctx.AddRange(newsSources);
                            }
                            CacheDbContext.UpdateCacheEntry(NEWS_SOURCES_URI.ToString(), DateTime.Now.Add(MAX_TIME_IN_CACHE));
                            return(newsSources);
                        }
                    }
                    return(new List <NewsSource>());
                });
            }
            return(await updateTask.ConfAwaitFalse());
        }
Ejemplo n.º 3
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <User> UpdateUserAsync(TumOnlineCredentials credentials, string obfuscatedId, bool force)
        {
            // Wait for the old update to finish first:
            if (!(updateTask is null) && !updateTask.IsCompleted)
            {
                try
                {
                    return(await updateTask.ConfAwaitFalse());
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Awaiting for user task failed with:", e);
                    return(null);
                }
            }

            updateTask = Task.Run(async() =>
            {
                if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.PERSON_DETAILS.NAME))
                {
                    Logger.Info("No need to fetch user. Cache is still valid.");
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        return(ctx.Users.Include(ctx.GetIncludePaths(typeof(User))).FirstOrDefault());
                    }
                }
                User user = null;
                try
                {
                    user = await DownloadUserAsync(credentials, obfuscatedId, force);
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Failed to request user with:", e);
                }
                if (!(user is null))
                {
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        foreach (User userOld in ctx.Users.Where(u => u.Id == user.Id).Include(ctx.GetIncludePaths(typeof(User))))
                        {
                            if (userOld.Groups.Count() > 0)
                            {
                                ctx.RemoveRange(userOld.Groups);
                                userOld.Groups.Clear();
                            }
                            ctx.Remove(userOld);
                        }
                        ctx.Add(user);
                    }
                    CacheDbContext.UpdateCacheEntry(TumOnlineService.PERSON_DETAILS.NAME, DateTime.Now.Add(TumOnlineService.PERSON_DETAILS.VALIDITY));
                }
Ejemplo n.º 4
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <IEnumerable <CalendarEvent> > UpdateAsync(TumOnlineCredentials credentials, bool force)
        {
            // Wait for the old update to finish first:
            if (!(updateTask is null) && !updateTask.IsCompleted)
            {
                try
                {
                    return(await updateTask.ConfAwaitFalse());
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Awaiting for calendar task failed with:", e);
                    return(new List <CalendarEvent>());
                }
            }

            updateTask = Task.Run(async() =>
            {
                if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.CALENDAR.NAME))
                {
                    Logger.Info("No need to fetch calendar events. Cache is still valid.");
                    return(LoadCalendarEventsFromDb());
                }
                IEnumerable <CalendarEvent> events = null;
                try
                {
                    events = await DownloadCalendarEventsAsync(credentials, force);
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Failed to request calendar events with:", e);
                }
                if (!(events is null))
                {
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        ctx.RemoveRange(ctx.CalendarEvents);
                        ctx.AddRange(events);
                    }
                    CacheDbContext.UpdateCacheEntry(TumOnlineService.CALENDAR.NAME, DateTime.Now.Add(TumOnlineService.CALENDAR.VALIDITY));

                    // Update the Windows calendar:
                    if (!Settings.GetSettingBoolean(SettingsConsts.DISABLE_WINDOWS_CALENDAR_INTEGRATION))
                    {
                        await UpdateWindowsCalendarAsync(events);
                    }
                    else
                    {
                        Logger.Debug("Not updating the Windows calendar. Setting disabled.");
                    }
                }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
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());
        }
Ejemplo n.º 7
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <IEnumerable <TuitionFee> > UpdateAsync(TumOnlineCredentials credentials, bool force)
        {
            // Wait for the old update to finish first:
            if (!(updateTask is null) && !updateTask.IsCompleted)
            {
                try
                {
                    return(await updateTask.ConfAwaitFalse());
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Awaiting for tuition fees task failed with:", e);
                    return(new List <TuitionFee>());
                }
            }

            updateTask = Task.Run(async() =>
            {
                if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.TUITION_FEE_STATUS.NAME))
                {
                    Logger.Info("No need to fetch tuition fees. Cache is still valid.");
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        return(ctx.TuitionFees.Include(ctx.GetIncludePaths(typeof(TuitionFee))).ToList());
                    }
                }
                IEnumerable <TuitionFee> fees = null;
                try
                {
                    fees = await DownloadFeesAsync(credentials, force);
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Failed to request tuition fees with:", e);
                }
                if (!(fees is null))
                {
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        ctx.RemoveRange(ctx.TuitionFees);
                        ctx.AddRange(fees);
                    }
                    CacheDbContext.UpdateCacheEntry(TumOnlineService.GRADES.NAME, DateTime.Now.Add(TumOnlineService.GRADES.VALIDITY));
                }
Ejemplo n.º 8
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <Identity> UpdateAsync(TumOnlineCredentials credentials, bool force)
        {
            // Wait for the old update to finish first:
            if (!(updateTask is null) && !updateTask.IsCompleted)
            {
                try
                {
                    return(await updateTask.ConfAwaitFalse());
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Awaiting for identity task failed with:", e);
                    return(null);
                }
            }

            updateTask = Task.Run(async() =>
            {
                if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.ID.NAME))
                {
                    Logger.Info("No need to fetch identity. Cache is still valid.");
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        return(ctx.Identities.Include(ctx.GetIncludePaths(typeof(Identity))).FirstOrDefault());
                    }
                }
                Identity identity = null;
                try
                {
                    identity = await DownloadIdentityAsync(credentials, force);
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Failed to request identity with:", e);
                }
                if (!(identity is null))
                {
                    using (TumOnlineDbContext ctx = new TumOnlineDbContext())
                    {
                        ctx.RemoveRange(ctx.Identities);
                        ctx.Add(identity);
                    }
                    CacheDbContext.UpdateCacheEntry(TumOnlineService.ID.NAME, DateTime.Now.Add(TumOnlineService.ID.VALIDITY));
                }
Ejemplo n.º 9
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());
        }
Ejemplo n.º 10
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--


        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async Task <IEnumerable <StudyRoomGroup> > UpdateAsync(bool force)
        {
            // Wait for the old update to finish first:
            if (!(updateTask is null) && !updateTask.IsCompleted)
            {
                return(await updateTask.ConfAwaitFalse());
            }

            updateTask = Task.Run(async() =>
            {
                if (!force && CacheDbContext.IsCacheEntryValid(STUDY_ROOMS_URI.ToString()))
                {
                    Logger.Info("No need to fetch study rooms. Cache is still valid.");
                    using (StudyRoomDbContext ctx = new StudyRoomDbContext())
                    {
                        return(ctx.StudyRoomGroups.Include(ctx.GetIncludePaths(typeof(StudyRoomGroup))).ToList());
                    }
                }
                IEnumerable <StudyRoomGroup> groups = null;
                try
                {
                    groups = await DownloadRoomsAsync(force);
                }
                catch (Exception e)
                {
                    InvokeOnRequestError(new RequestErrorEventArgs(e));
                    Logger.Error("Failed to request study rooms with:", e);
                }
                if (!(groups is null))
                {
                    using (StudyRoomDbContext ctx = new StudyRoomDbContext())
                    {
                        ctx.RemoveRange(ctx.StudyRoomGroups);
                        ctx.RemoveRange(ctx.StudyRooms);
                        ctx.RemoveRange(ctx.StudyRoomAttributes);
                        ctx.AddRange(groups);
                    }
                    CacheDbContext.UpdateCacheEntry(STUDY_ROOMS_URI.ToString(), DateTime.Now.Add(MAX_TIME_IN_CACHE));
                }