Beispiel #1
0
        public Task UpsertUserAction(LongLastingUserActionArguments arguments)
        {
            var wrapper = new ArgumentsWrapper
            {
                AddDate   = DateTimeOffset.UtcNow,
                Arguments = arguments
            };

            _actionCache[arguments.TelegramRozkladUser.TelegramId] = wrapper;

            return(Task.CompletedTask);
        }
Beispiel #2
0
        public ILongLastingUserActionHandler GetHandler(LongLastingUserActionArguments actionArguments)
        {
            switch (actionArguments.UserActionType)
            {
            case LongLastingUserActionType.TimetableSelectingFaculty:
                return(_serviceProvider.GetService <TimetableSelectingFacultyActionHandler>());

            case LongLastingUserActionType.TimetableSelectingFacultyGroupToAdd:
                return(_serviceProvider.GetService <TimetableSelectingFacultyGroupToAddActionHandler>());

            default:
                throw new NotImplementedException();
            }
        }
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;
            }
        }
Beispiel #5
0
        public async Task <bool> Handle(LongLastingUserActionArguments userActionArguments)
        {
            string userInput = null;
            ICollection <Group> facultyGroups = null;
            Group facultyGroup = null;

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

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

                    return(true);
                }

                if (userInput == _currentUserLocalizationService["back"])
                {
                    await _telegramBotActions.ShowTimetableSelectingFacultyMenu();

                    return(true);
                }

                facultyGroups = await GetFacultyGroups(userActionArguments);

                facultyGroup = facultyGroups.FirstOrDefault(f => f.Name == userInput);

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

                    return(true);
                }

                var currentTelegramUser = userActionArguments.TelegramRozkladUser;

                currentTelegramUser.FacultyGroups.Add(facultyGroup);
                await _telegramRozkladUserDao.Update(currentTelegramUser);

                await _longLastingUserActionManager.ClearUserAction(currentTelegramUser);

                await _telegramBotActions.ShowMainMenu(new ShowMainMenuOptions
                {
                    LocalizationValueToShow = "faculty-group-has-been-added"
                });

                return(true);
            }
            catch (Exception e)
            {
                var facultyGroupsString = string.Empty;

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

                _logger.LogError(TelegramLogEvents.TimetableSelectingFacultyGroupToAddError, e,
                                 "userInput: {userInput}. " +
                                 "facultyGroups: {facultyGroupsString}. " +
                                 "facultyGroup: {facultyGroup}. " +
                                 "userActionArguments: {userActionArguments}",
                                 userInput, facultyGroupsString, facultyGroup, userActionArguments);

                throw;
            }
        }