Пример #1
0
        public async Task <LoadMessagesOperationResult> LoadMessagesAsync(int?top = null)
        {
            IEnumerable <ChatMessage> messages;

            try
            {
                messages = await _chatMessagesRepository.GetMessagesAsync(top);
            }
            catch (Exception ex)
            {
                return(ServerOperationResult.Failure <LoadMessagesOperationResult>(ex.Message));
            }

            return(new LoadMessagesOperationResult
            {
                Messages = messages
            });
        }
Пример #2
0
        public async Task <LoginOperationResult> LoginAsync(string login, string password)
        {
            var clientCallback = OperationContext.Current.GetCallbackChannel <IClientCallback>();

            CheckAlreadyLoggedInAsync(login);

            User user;

            try
            {
                user = await _usersRepository.LoginAsync(login, password);
            }
            catch (Exception ex)
            {
                return(ServerOperationResult.Failure <LoginOperationResult>(ex.Message));
            }

            return(user == null
                ? ServerOperationResult.Failure <LoginOperationResult>("Не верный логин или пароль.")
                : Login(user, clientCallback));
        }
Пример #3
0
        public async Task <SendMessageOperationResult> SendMessageAsync(ChatMessage message, bool save = true)
        {
            if (save)
            {
                try
                {
                    await _chatMessagesRepository.AddMessageAsync(message);
                }
                catch (Exception ex)
                {
                    return(ServerOperationResult.Failure <SendMessageOperationResult>(ex.Message));
                }
            }

            foreach (var clientInfo in _clients.Where(c => c.Value.User.Id != message.UserId))
            {
                clientInfo.Value.Callback.SetMessage(message);
            }

            return(new SendMessageOperationResult());
        }
Пример #4
0
        public async Task <LoginOperationResult> RegisterAsync(string userName, string login, string password)
        {
            var clientCallback = OperationContext.Current.GetCallbackChannel <IClientCallback>();

            User user;

            try
            {
                var loginExists = await _usersRepository.CheckLoginExists(login);

                if (loginExists)
                {
                    return(ServerOperationResult.Failure <LoginOperationResult>($"Логин {login} уже зарегистрирован."));
                }

                user = await _usersRepository.RegisterAsync(userName, login, password);
            }
            catch (Exception ex)
            {
                return(ServerOperationResult.Failure <LoginOperationResult>(ex.Message));
            }

            return(Login(user, clientCallback));
        }