public async Task <RozkladUser> Find(string guid)
        {
            if (!_memoryCache.TryGetValue(guid, out RozkladUserWrapper rozkladUserWrapper))
            {
                rozkladUserWrapper =
                    await _dbContext.RozkladUserWrappers
                    .AsNoTracking()
                    .Where(u => u.Guid == guid)
                    .FirstOrDefaultAsync();

                if (rozkladUserWrapper == null)
                {
                    return(null);
                }
                var facultyGroups = await _facultyGroupsProvider.GetFacultyGroups();

                foreach (var groupTypeId in rozkladUserWrapper.FacultyGroupsTypeIds)
                {
                    rozkladUserWrapper.FacultyGroups.Add(facultyGroups
                                                         .FirstOrDefault(g => g.TypeId == groupTypeId));
                }

                _memoryCache.Set(guid, rozkladUserWrapper, absoluteExpirationRelativeToNow: TimeSpan.FromHours(1));
            }


            return(rozkladUserWrapper);
        }
Beispiel #2
0
        private async Task GetFacultyGroup()
        {
            var facultyGroupTypeId = _facultyGroupTypeId;
            var facultyGroups      = await _facultyGroupsProvider.GetFacultyGroups(_faculty);

            _facultyGroup = facultyGroups.FirstOrDefault(g => g.TypeId == facultyGroupTypeId);
        }
Beispiel #3
0
        private async Task <ICollection <Group> > GetFacultyGroups(LongLastingUserActionArguments userActionArguments)
        {
            if (userActionArguments.Parameters[typeof(ICollection <Group>)] is ICollection <Group> facultyGroups)
            {
                return(facultyGroups);
            }

            if (!(userActionArguments.Parameters[typeof(Faculty)] is Faculty faculty))
            {
                throw new ArgumentNullException(nameof(faculty),
                                                $"{nameof(LongLastingUserActionArguments)}.{nameof(userActionArguments.Parameters)}[{nameof(Faculty)}] is null");
            }

            facultyGroups = await _facultyGroupsProvider.GetFacultyGroups(faculty);

            return(facultyGroups);
        }
        public async Task <bool> Handle(LongLastingUserActionArguments userActionArguments)
        {
            string userInput = null;
            ICollection <Faculty> faculties     = null;
            Faculty             selectedFaculty = null;
            ICollection <Group> facultyGroups   = null;

            try
            {
                var isHandled = true;

                userInput = (userActionArguments.Parameters[typeof(Message)] as Message)?.Text;

                if (string.IsNullOrWhiteSpace(userInput))
                {
                    await _botActions.ShowMessage(o => o.ShowIncorrectInputMessage = true);

                    return(isHandled);
                }

                if (userInput == _currentUserLocalizationService["back"])
                {
                    await _longLastingUserActionManager.ClearUserAction(userActionArguments.TelegramRozkladUser);

                    await _botActions.ShowMainMenu();

                    return(true);
                }

                faculties = await _facultiesProvider.GetFaculties();

                selectedFaculty = faculties.FirstOrDefault(f => f.FullName == userInput);

                if (selectedFaculty == null)
                {
                    await _botActions.ShowMessage(o =>
                                                  o.MessageTextLocalizationValue = "such-faculty-was-not-found");

                    return(isHandled);
                }

                facultyGroups = await _facultyGroupsProvider.GetFacultyGroups(selectedFaculty);

                if (!facultyGroups.Any())
                {
                    await _longLastingUserActionManager.ClearUserAction(userActionArguments.TelegramRozkladUser);

                    await _botActions.ShowFacultyGroupsForFacultyDoesNotExistMessage();

                    return(isHandled);
                }

                await _longLastingUserActionManager.UpsertUserAction(new LongLastingUserActionArguments
                {
                    TelegramRozkladUser = userActionArguments.TelegramRozkladUser,
                    UserActionType      = LongLastingUserActionType.TimetableSelectingFacultyGroupToAdd,
                    Parameters          = new Dictionary <Type, object>
                    {
                        { typeof(ICollection <Group>), facultyGroups },
                        { typeof(Faculty), selectedFaculty }
                    }
                });

                await _botActions.ShowTimetableSelectingFacultyGroupToAddMenu(
                    new ShowTimetableSelectingFacultyGroupToAddMenuOptions
                {
                    FacultyGroups = facultyGroups
                });

                return(isHandled);
            }
            catch (Exception e)
            {
                var facultiesString = string.Empty;

                if (faculties != null)
                {
                    facultiesString = string.Join(",", faculties?.Select(f => f.ToString()));
                }

                var facultyGroupsString = string.Empty;

                if (facultyGroups != null)
                {
                    facultyGroupsString = string.Join(",", facultyGroups.Select(f => f.ToString()));
                }

                _logger.LogError(TelegramLogEvents.TimetableSelectingFacultyError, e,
                                 "userInput: {userInput}. " +
                                 "faculties: {facultiesString}. " +
                                 "selectedFaculty: {selectedFaculty}. " +
                                 "facultyGroups: {facultyGroupsString}. " +
                                 "userActionArguments: {userActionArguments}. ",
                                 userInput, facultiesString, selectedFaculty, facultyGroupsString, userActionArguments);

                throw;
            }
        }