Пример #1
0
        public void EditWord_GivenWordEditModelWithUniqueNewWord_ShouldChangeOriginalWordToNewWord()
        {
            WordEditInfoModel wordEditInfoModel = new WordEditInfoModel {
                OriginalWord = "sula", NewWord = "alus"
            };

            _wordRepository.EditWord(wordEditInfoModel.OriginalWord, wordEditInfoModel.NewWord).Returns(true);
            _userContract.GetUser("10").Returns(new User {
                Id = 10, UserIp = "10", SearchesLeft = 10
            });

            _userContract.GetUser("127.0.0.1").Returns(new User {
                Id = 1, UserIp = "127.0.0.1", SearchesLeft = 10
            });

            var result = _dictionaryManagingService.EditWord(wordEditInfoModel, "10");

            _userManagingService.Received().GiveUserAdditionalSearch("127.0.0.1");

            result.NewWord.ShouldBeNull();
            result.OriginalWord.ShouldBe("alus");
            result.EditStatus.ShouldBe(WordEditStatus.EditSuccessful);
        }
Пример #2
0
        public override AnswerItem Reply(MessageItem mItem)
        {
            var answer = new AnswerItem
            {
                Message = "👀"
            };

            if (string.IsNullOrEmpty(mItem.TextOnly))
            {
                answer.Message =
                    "Type a chinese word to view it's flash card. Use chinese characters only! Pinyin and translation are not supported!";
            }
            else
            {
                try
                {
                    var word = _repository.GetWord(mItem.Text, mItem.UserId);

                    if (word == null)
                    {
                        answer.Message = "The word is not found. But you can add it, right?";
                        return(answer);
                    }

                    var file = _repository.GetWordFlashCard(word.CardAll.Id);

                    answer.Message = string.IsNullOrEmpty(word.Usage)
                        ? word.ToScoreString()
                        : word.ToScoreString() + Environment.NewLine + word.Usage;

                    answer.Picture = new GenerateImageResult
                    {
                        Height = word.CardAll.Height, Width = word.CardAll.Width, ImageBody = file
                    };

                    word.Score.ViewCount++;
                    word.Score.LastView = _repository.GetRepositoryTime();
                    _repository.EditWord(word);
                }
                catch (Exception e)
                {
                    answer.Message = e.Message;
                    return(answer);
                }
            }

            return(answer);
        }
        public WordEditInfoModel EditWord(WordEditInfoModel wordEditInfoModel, string userIp)
        {
            bool isEditSuccessful = _wordRepository.EditWord(wordEditInfoModel.OriginalWord, wordEditInfoModel.NewWord);

            if (isEditSuccessful)
            {
                wordEditInfoModel.EditStatus   = WordEditStatus.EditSuccessful;
                wordEditInfoModel.OriginalWord = wordEditInfoModel.NewWord;
                wordEditInfoModel.NewWord      = null;

                _userManagingService.GiveUserAdditionalSearch(userIp);
            }
            else
            {
                wordEditInfoModel.EditStatus = WordEditStatus.EditUnsuccesful;
                wordEditInfoModel.NewWord    = null;
            }

            return(wordEditInfoModel);
        }
Пример #4
0
        public override AnswerItem Reply(MessageItem mItem)
        {
            var message = new AnswerItem
            {
                Message = GetCommandIconUnicode()
            };

            try
            {
                var idUser = mItem.ChatId;

                if (string.IsNullOrEmpty(mItem.TextOnly))
                {
                    message.Message = "Type a word to edit. Use chinese characters only!";
                }
                else if (mItem.TextOnly.StartsWith(EditCmd))
                {
                    if (!long.TryParse(
                            mItem.TextOnly.Split(new[] { EditCmdSeparator }, StringSplitOptions.RemoveEmptyEntries)
                            .LastOrDefault(),
                            out long idWord))
                    {
                        message.Message += "I can not edit your word now, sorry.";
                        return(message);
                    }

                    var lastWord = _repository.GetWord(idWord);
                    //_repository.GetCurrentUserWordStatistic(idUser);

                    if (lastWord == null)
                    {
                        message.Message += "No words to edit";
                        return(message);
                    }
                    message.Message = lastWord.ToEditString();
                }
                else if (mItem.TextOnly.Length > ImportCommand.MaxImportRowLength)
                {
                    message.Message =
                        $"Too much characters. Maximum is {ImportCommand.MaxImportRowLength}";
                }
                else
                {
                    var editedText = new[] { mItem.TextOnly };

                    if (!_importCommand.ValidateArray(editedText))
                    {
                        var wordToFind = _repository.GetWord(mItem.TextOnly, idUser);

                        if (wordToFind == null)
                        {
                            message.Message += "Bad text";
                            return(message);
                        }

                        message.Message = wordToFind.ToEditString();
                        return(message);
                    }

                    var word       = _parseProvider.ImportWords(editedText);
                    var parsedWord = word.SuccessfulWords.FirstOrDefault();

                    if (parsedWord == null)
                    {
                        message.Message += word.FailedWords.FirstOrDefault();
                        return(message);
                    }

                    _importCommand.UploadFiles(parsedWord);

                    _repository.EditWord(parsedWord);
                    message.Message += "The word has been updated";
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                message.Message += ex.Message;
            }
            return(message);
        }