Browse Source

Refactor code

Tigran 4 years ago
parent
commit
372ede6575

+ 1 - 0
MafiaTelegramBot/Controllers/MessageController.cs

@@ -45,6 +45,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (NullReferenceException)
             {
+                Console.WriteLine("Command not found");
             }
             catch (Exception exception)
             {

+ 2 - 17
MafiaTelegramBot/Models/Commands/Command.cs

@@ -1,30 +1,15 @@
-using System.Linq;
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 
 namespace MafiaTelegramBot.Models.Commands
 {
-    public abstract class Command
+    public abstract class Command : UpdateModel
     {
-        protected abstract string Name { get; }
-
-        protected abstract Task<Message> Execute(Update update, TelegramBotClient client);
-
-        private bool Contains(string command)
-        {
-            return command.Contains(Name);
-        }
-        
         public static Task Update(Update update)
         {
             var commands = Bot.Commands;
             var message = update.Message.Text;
-            var client = Bot.Get();
-
-            return (from command in commands
-                where command.Contains(message)
-                select command.Execute(update, client)).FirstOrDefault();
+            return FirstOrDefault(commands, message).Execute(update);
         }
     }
 }

+ 1 - 2
MafiaTelegramBot/Models/Commands/ConnectGameCommand.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 
@@ -9,7 +8,7 @@ namespace MafiaTelegramBot.Models.Commands
     {
         protected override string Name => strings.connect_game;
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 1 - 2
MafiaTelegramBot/Models/Commands/CreateGameCommand.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 
@@ -8,7 +7,7 @@ namespace MafiaTelegramBot.Models.Commands
     public class CreateGameCommand : Command
     {
         protected override string Name => strings.create_game;
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 1 - 2
MafiaTelegramBot/Models/Commands/ShowProfileCommand.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Threading.Tasks;
 using MafiaTelegramBot.DataBase;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 using Telegram.Bot.Types.ReplyMarkups;
@@ -12,7 +11,7 @@ namespace MafiaTelegramBot.Models.Commands
     {
         protected override string Name => strings.show_profile;
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 1 - 2
MafiaTelegramBot/Models/Commands/StartCommand.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 using Telegram.Bot.Types.ReplyMarkups;
@@ -10,7 +9,7 @@ namespace MafiaTelegramBot.Models.Commands
     {
         protected override string Name => "/start";
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 1 - 2
MafiaTelegramBot/Models/Inlines/MyRolesQuery.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 
@@ -9,7 +8,7 @@ namespace MafiaTelegramBot.Models.Inlines
     {
         protected override string Name => strings.my_roles_callback;
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.CallbackQuery.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 2 - 17
MafiaTelegramBot/Models/Inlines/Query.cs

@@ -1,30 +1,15 @@
-using System.Linq;
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 
 namespace MafiaTelegramBot.Models.Inlines
 {
-    public abstract class Query
+    public abstract class Query : UpdateModel
     {
-        protected abstract string Name { get; }
-
-        protected abstract Task<Message> Execute(Update update, TelegramBotClient client);
-
-        private bool Contains(string command)
-        {
-            return command.Contains(Name);
-        }
-        
         public static Task Update(Update update)
         {
             var queries = Bot.Queries;
             var data = update.CallbackQuery.Data;
-            var client = Bot.Get();
-
-            return (from query in queries
-                where query.Contains(data)
-                select query.Execute(update, client)).FirstOrDefault();
+            return FirstOrDefault(queries, data).Execute(update);
         }
     }
 }

+ 1 - 2
MafiaTelegramBot/Models/Inlines/SettingsQuery.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 
@@ -9,7 +8,7 @@ namespace MafiaTelegramBot.Models.Inlines
     {
         protected override string Name => strings.settings_callback;
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.CallbackQuery.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 1 - 2
MafiaTelegramBot/Models/Inlines/ShopQuery.cs

@@ -1,5 +1,4 @@
 using System.Threading.Tasks;
-using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 
@@ -9,7 +8,7 @@ namespace MafiaTelegramBot.Models.Inlines
     {
         protected override string Name => strings.shop_callback;
 
-        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        public override async Task<Message> Execute(Update update)
         {
             var chatId = update.CallbackQuery.Message.Chat.Id;
             await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);

+ 26 - 0
MafiaTelegramBot/Models/UpdateModel.cs

@@ -0,0 +1,26 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Telegram.Bot.Types;
+
+namespace MafiaTelegramBot.Models
+{
+    public abstract class UpdateModel
+    {
+        protected abstract string Name { get; }
+        
+        public abstract Task<Message> Execute(Update update);
+
+        private bool Contains(string command)
+        {
+            return command.Contains(Name);
+        }
+
+        protected static UpdateModel FirstOrDefault(IReadOnlyList<UpdateModel> list, string data)
+        {
+            return (from item in list
+                where item.Contains(data)
+                select item).FirstOrDefault();
+        }
+    }
+}