public async Task <VsfResponse> GetUser(string userId)
 {
     using (await UserLocker.LockAsync(userId))
     {
         return(new VsfSuccessResponse <VsfUser>(await _userAdapter.GetUserById(userId)));
     }
 }
Esempio n. 2
0
        public override async Task ExecuteAsync(Message message, TelegramBotClient botClient)
        {
            var chatId = (int)message.Chat.Id;

            using (TelegramContext db = new TelegramContext())
            {
                var User = db.Users.Where(p => p.Id == message.From.Id).SingleOrDefault();
                if (User == null)
                {
                    User user = new User {
                        Id = chatId, Name = message.From.Username
                    };
                    UserState us = new UserState {
                        User = user, State = (int)UserStatesEnum.Empty, LastMessageId = message.MessageId
                    };
                    db.Users.Add(user);
                    db.UserStates.Add(us);
                    db.SaveChanges();

                    UserLocker.AddUser(chatId);
                    UserLocker.LockUser(chatId);
                }

                VacanciesManager.MainMenu(message, botClient);
            }
        }
        public async Task <VsfResponse> UpdateUser(string userId, UserUpdateModel userUpdateModel)
        {
            using (await UserLocker.LockAsync(userId))
            {
                if (await _userAdapter.UpdateUser(userId, userUpdateModel.Customer))
                {
                    return(new VsfSuccessResponse <VsfUser>(await _userAdapter.GetUserById(userId)));
                }

                return(new VsfErrorResponse("User update failed."));
            }
        }
        public async Task <VsfResponse> ChangePassword(string userId, ChangePasswordModel changePasswordModel)
        {
            using (await UserLocker.LockAsync(userId))
            {
                if (!await _userAdapter.ChangePassword(userId,
                                                       changePasswordModel.CurrentPassword, changePasswordModel.NewPassword))
                {
                    return(new VsfErrorResponse("The password doesn't match this account."));
                }

                return(new VsfSuccessResponse <string>("Password changed."));
            }
        }
        public async Task <VsfResponse> CreateLoginResponse(UserLoginModel userLoginModel)
        {
            var user = await _userAdapter.GetUserByCredentials(userLoginModel.Username, userLoginModel.Password);

            if (user == null)
            {
                return(new VsfErrorResponse("You did not sign in correctly or your account is temporarily disabled."));
            }

            using (await UserLocker.LockAsync(user.Id))
            {
                var userClaims = _userClaimsProvider.GetClaims(user).ToList();
                var authToken  = await _userTokenProvider.GenerateNewToken(userClaims);

                var refreshToken = await _userTokenProvider.GenerateNewRefreshToken(userClaims);

                return(new LoginResponse(authToken, refreshToken));
            }
        }
Esempio n. 6
0
        public async Task <OkResult> Post([FromBody] Update update)
        {
            if (update == null)
            {
                return(Ok());
            }


            var commands  = Bot.Commands;
            var message   = update.Message;
            var botClient = await Bot.GetBotClientAsync();

            try
            {
                if (update.Type == UpdateType.Message)
                {
                    if (UserLocker.IsUserLocked(update.Message.Chat.Id))
                    {
                        return(Ok());
                    }
                    else
                    {
                        UserLocker.LockUser(update.Message.Chat.Id);
                    }

                    if (commands.All(x => x.Name != message.Text))
                    {
                        try
                        {
                            await Task.Run(() => StateManager.RemoveLastMessage(botClient, (int)update.Message.Chat.Id));

                            await Task.Run(() => botClient.DeleteMessageAsync((int)update.Message.Chat.Id, message.MessageId));
                        }
                        catch { }
                    }



                    foreach (var command in commands)
                    {
                        if (command.Name == message.Text)
                        {
                            await command.ExecuteAsync(message, botClient);

                            return(Ok());
                        }
                    }
                    StateManager.StateControl(botClient, update);
                }
                else if (update.Type == UpdateType.CallbackQuery)
                {
                    if (UserLocker.IsUserLocked(update.CallbackQuery.Message.Chat.Id))
                    {
                        return(Ok());
                    }
                    else
                    {
                        UserLocker.LockUser(update.CallbackQuery.Message.Chat.Id);
                    }
                    StateManager.RemoveLastMessage(botClient, (int)update.CallbackQuery.Message.Chat.Id);
                    CallbackQueryManager.ChoseCallBackQuery(botClient, update);
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                await botClient.SendTextMessageAsync(message.Chat.Id, "Возникла ошибка. Обратитесь к администратору с следующим текстом: " + ex.Message, Telegram.Bot.Types.Enums.ParseMode.Default);

                return(Ok());
            }
            finally
            {
                if (update != null)
                {
                    if (update.Type == UpdateType.Message)
                    {
                        UserLocker.UnlockUser(update.Message.Chat.Id);
                    }
                    else if (update.Type == UpdateType.CallbackQuery)
                    {
                        UserLocker.UnlockUser(update.CallbackQuery.Message.Chat.Id);
                    }
                }
            }
        }