예제 #1
0
        public void Start()
        {
            var apiClient = new ApiClient(WebApi);
            var signer    = new AuthSigner(BotKey);

            _userApi    = new UserApi(apiClient, signer);
            _wordApi    = new WordApi(apiClient);
            _serviceApi = new ServiceApi(apiClient);
            _folderApi  = new FolderApi(apiClient);

            _userApi.LoginUser(new ApiUser {
                Id = AdminId
            });
            _restWordRepository = new RestWordRepository(_wordApi, _userApi, _serviceApi, _folderApi);

            _testWords         = GetTestWords();
            _testWordFileBytes = GetTestWordFileBytes();

            FillDb();
        }
예제 #2
0
 public WebCommand(AuthSigner authSigner, string baseWebPath)
 {
     _authSigner  = authSigner;
     _baseWebPath = baseWebPath;
 }
예제 #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            var botSettings = BotSettings;
            var tClient     = new TelegramBotClient(botSettings.TelegramBotKey)
            {
                Timeout = botSettings.PollingTimeout
            };

            var apiClient          = new ApiClient(botSettings.ApiPublicUrl);
            var wordApi            = new WordApi(apiClient);
            var signer             = new AuthSigner(botSettings.TelegramBotKey);
            var userApi            = new UserApi(apiClient, signer);
            var serviceApi         = new ServiceApi(apiClient);
            var folderApi          = new FolderApi(apiClient);
            var log4NetService     = new Log4NetService();
            var restWordRepository = new RestWordRepository(wordApi, userApi, serviceApi, folderApi);
            var antiDdosChecker    = new AntiDdosChecker(GetDateTime);

            var commandManager = new CommandManager(GetCommands, GetHiddenCommands,
                                                    new Dictionary <string, ECommands> {
                { botSettings.ServiceCommandPassword, ECommands.Admin }
            });

            apiClient.OnAuthenticationRequest += (o, e) =>
            {
                userApi.LoginUser(new ApiUser {
                    Id = botSettings.UserId.ToString()
                });
            };

            services.AddSingleton(a => antiDdosChecker);
            services.AddSingleton(bS => botSettings);
            services.AddSingleton(cl => tClient);
            services.AddSingleton <ICommandManager>(commandManager);
            services.AddSingleton <ILogService>(log4NetService);
            services.AddSingleton <IWordRepository>(restWordRepository);

            var flashCardUrl = $"{botSettings.ApiPublicUrl}/word/file";

            services.AddSingleton(qh =>
                                  new QueryHandler(tClient, log4NetService, restWordRepository, antiDdosChecker, flashCardUrl, commandManager, MaxUploadFileSize));

            services.AddSingleton <ISyllableColorProvider, ClassicSyllableColorProvider>();
            services.AddSingleton <IChineseWordParseProvider, PinyinChineseWordParseProvider>();
            services.AddSingleton <IStudyProvider, ClassicStudyProvider>();
            services.AddSingleton <ISyllablesToStringConverter, ClassicSyllablesToStringConverter>();
            services.AddSingleton <IChinesePinyinConverter, Pinyin4NetConverter>();
            services.AddSingleton <IFlashCardGenerator, FontFlashCardGenerator>();

            services.AddTransient(a => new AboutCommand(ReleaseNotesInfo, AboutInfo));
            services.AddTransient(a => new HelpCommand(GetCommands));
            services.AddTransient(a => new StartCommand(GetCommands, botSettings.WebhookPublicUrl));
            services.AddTransient(a => new FolderCommand(ServiceProvider.GetService <IWordRepository>(), botSettings.FolderManagementText));

            services.AddTransient(a => new DefaultCommand(ServiceProvider.GetService <IWordRepository>()));
            services.AddTransient(a => new ImportCommand(ServiceProvider.GetService <IChineseWordParseProvider>(),
                                                         ServiceProvider.GetService <IWordRepository>(), ServiceProvider.GetService <IFlashCardGenerator>(),
                                                         MaxUploadFileSize));
            services.AddTransient(a => new AddCommand(ServiceProvider.GetService <IChineseWordParseProvider>(), ServiceProvider.GetService <IWordRepository>(), ServiceProvider.GetService <IFlashCardGenerator>()));
            services.AddTransient(a => new ViewCommand(ServiceProvider.GetService <IWordRepository>()));
            services.AddTransient(a => new DeleteCommand(ServiceProvider.GetService <IWordRepository>()));
            services.AddTransient(a => new EditCommand(ServiceProvider.GetService <IWordRepository>(),
                                                       ServiceProvider.GetService <IChineseWordParseProvider>(), ServiceProvider.GetService <ImportCommand>(),
                                                       ServiceProvider.GetService <IFlashCardGenerator>()));
            services.AddTransient(a => new LearnWritingCommand(ServiceProvider.GetService <IStudyProvider>(),
                                                               ServiceProvider.GetService <EditCommand>()));
            services.AddTransient(a => new LearnViewCommand(ServiceProvider.GetService <IStudyProvider>(),
                                                            ServiceProvider.GetService <EditCommand>()));
            services.AddTransient(a => new LearnSpeakCommand(ServiceProvider.GetService <IStudyProvider>(),
                                                             ServiceProvider.GetService <EditCommand>()));
            services.AddTransient(a => new LearnTranslationCommand(ServiceProvider.GetService <IStudyProvider>(),
                                                                   ServiceProvider.GetService <EditCommand>()));
            services.AddTransient(a => new ModeCommand(ServiceProvider.GetService <IWordRepository>()));

            services.AddTransient(a => new PreInstallCommand(ServiceProvider.GetService <IWordRepository>()));

            services.AddTransient(a => new AdminCommand(ServiceProvider.GetService <IChineseWordParseProvider>(), ServiceProvider.GetService <IWordRepository>(), ServiceProvider.GetService <IFlashCardGenerator>(),
                                                        MaxUploadFileSize, PreInstalledFolder, botSettings.ServerUserId, botSettings.AdminUserId, tClient));
            services.AddTransient(a => new WebCommand(signer, botSettings.ApiPublicUrl));

            services.AddSingleton(a => new AntiDdosChecker(GetDateTime));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
예제 #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="apiClient">An instance of ApiClient</param>
 /// <param name="authSigner">Authentication signer class for security</param>
 public UserApi(ApiClient apiClient, AuthSigner authSigner) : base(apiClient)
 {
     _authSigner = authSigner;
 }
예제 #5
0
        static void Main()
        {
            var configurationRoot = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json")
                                    .Build();

            var telegramBotKey   = configurationRoot[TelegramBotKey];
            var userId           = configurationRoot[UserIdKey];
            var connectionString = configurationRoot[OldSqlDbKey];
            var site             = configurationRoot[NewWebApiKey];

            var apiClient  = new ApiClient(site);
            var signer     = new AuthSigner(telegramBotKey);
            var userApi    = new UserApi(apiClient, signer);
            var wordApi    = new WordApi(apiClient);
            var serviceApi = new ServiceApi(apiClient);
            var datetime   = serviceApi.GetDatetime() ?? DateTime.Now;

            apiClient.OnAuthenticationRequest += (o, e) =>
            {
                userApi.LoginUser(new ApiUser {
                    Id = userId
                });
            };

            using (var context = new LearnChineseContext(connectionString))
            {
                var users = context.User;

                var usersCount = 0;
                foreach (var user in users)
                {
                    try
                    {
                        userApi.CreateUser(new User
                        {
                            CurrentFolderId = 0,
                            CurrentWordId   = context.Score.Where(a => a.IdUser == user.IdUser && a.IsInLearnMode)
                                              .Select(a => a.IdWord).FirstOrDefault(),
                            IdUser      = user.IdUser,
                            Name        = user.Name,
                            JoinDate    = user.JoinDate,
                            LastCommand = user.LastCommand,
                            Mode        = user.Mode,
                            Who         = RightEnum.Write
                        });
                        usersCount++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{user.IdUser} {user.Name} - user is not added: Error {ex.Message}");
                    }
                }
                Console.WriteLine($"User count is {usersCount}");

                var words = context.Word;

                var wordsCount = 0;
                foreach (var word in words)
                {
                    try
                    {
                        var score = context.Score.FirstOrDefault(a => a.IdWord == word.Id);

                        var scoreApi = new Score {
                            Name = string.Empty
                        };
                        if (score == null)
                        {
                            scoreApi.LastView                  = datetime;
                            scoreApi.LastLearned               = null;
                            scoreApi.OriginalWordCount         = 0;
                            scoreApi.OriginalWordSuccessCount  = 0;
                            scoreApi.PronunciationCount        = 0;
                            scoreApi.PronunciationSuccessCount = 0;
                            scoreApi.TranslationCount          = 0;
                            scoreApi.TranslationSuccessCount   = 0;
                            scoreApi.RightAnswerNumber         = null;
                            scoreApi.ViewCount                 = 0;
                        }
                        else
                        {
                            scoreApi.LastView      = score.LastView;
                            scoreApi.LastLearned   = score.LastLearned;
                            scoreApi.LastLearnMode = score.LastLearnMode;

                            scoreApi.OriginalWordCount         = score.OriginalWordCount ?? 0;
                            scoreApi.OriginalWordSuccessCount  = score.OriginalWordSuccessCount ?? 0;
                            scoreApi.PronunciationCount        = score.PronunciationCount ?? 0;
                            scoreApi.PronunciationSuccessCount = score.PronunciationSuccessCount ?? 0;
                            scoreApi.TranslationCount          = score.TranslationCount ?? 0;
                            scoreApi.TranslationSuccessCount   = score.TranslationSuccessCount ?? 0;

                            scoreApi.RightAnswerNumber = score.RightAnswerNumber;
                            scoreApi.ViewCount         = score.ViewCount ?? 0;
                        }

                        var fileA = context.WordFileA.FirstOrDefault(a => a.IdWord == word.Id);
                        var fileO = context.WordFileO.FirstOrDefault(a => a.IdWord == word.Id);
                        var fileP = context.WordFileP.FirstOrDefault(a => a.IdWord == word.Id);
                        var fileT = context.WordFileT.FirstOrDefault(a => a.IdWord == word.Id);

                        if (fileA == null || fileO == null || fileP == null || fileT == null)
                        {
                            Console.WriteLine($"{word.OriginalWord} - word is not added: file is null");
                            continue;
                        }

                        var fileAId = wordApi.AddFile(new WordFileBytes {
                            Bytes = fileA.Bytes
                        });
                        var fileOId = wordApi.AddFile(new WordFileBytes {
                            Bytes = fileO.Bytes
                        });
                        var filePId = wordApi.AddFile(new WordFileBytes {
                            Bytes = fileP.Bytes
                        });
                        var fileTId = wordApi.AddFile(new WordFileBytes {
                            Bytes = fileT.Bytes
                        });

                        wordApi.AddWord(new Word
                        {
                            FolderId       = 0, LastModified = word.LastModified, OwnerId = word.IdOwner,
                            OriginalWord   = word.OriginalWord, Pronunciation = word.Pronunciation,
                            SyllablesCount = word.SyllablesCount, Translation = word.Translation, Usage = word.Usage,
                            Score          = scoreApi,
                            CardAll        = new WordFile
                            {
                                CreateDate = fileA.CreateDate, Height = fileA.Height ?? 0, Width = fileA.Width ?? 0,
                                Id         = fileAId
                            },
                            CardOriginalWord = new WordFile
                            {
                                CreateDate = fileO.CreateDate, Height = fileO.Height ?? 0, Width = fileO.Width ?? 0,
                                Id         = fileOId
                            },
                            CardPronunciation = new WordFile
                            {
                                CreateDate = fileP.CreateDate, Height = fileP.Height ?? 0, Width = fileP.Width ?? 0,
                                Id         = filePId
                            },
                            CardTranslation = new WordFile
                            {
                                CreateDate = fileT.CreateDate, Height = fileT.Height ?? 0, Width = fileT.Width ?? 0,
                                Id         = fileTId
                            }
                        });
                        wordsCount++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{word.OriginalWord} - word is not added: Error {ex.Message}");
                    }
                }
                Console.WriteLine($"Word count is {wordsCount}");
            }
            Console.ReadKey();
        }