Browse Source

stickers database structure

Tigran 4 years ago
parent
commit
2eccbfaf7d

+ 6 - 23
CardCollector/Commands/InlineQuery/ShowStickersInGroup.cs

@@ -1,10 +1,8 @@
-using System.Collections.Generic;
+using System.Linq;
 using System.Threading.Tasks;
 using CardCollector.Controllers;
 using CardCollector.DataBase.Entity;
 using Telegram.Bot.Types;
-using Telegram.Bot.Types.Enums;
-using Telegram.Bot.Types.InlineQueryResults;
 
 namespace CardCollector.Commands.InlineQuery
 {
@@ -14,34 +12,19 @@ namespace CardCollector.Commands.InlineQuery
         
         public override async Task<Telegram.Bot.Types.Message> Execute()
         {
-            var filter = Update.InlineQuery!.Id;
-            var stickersList = GetStickersList(filter);
-            
+            var filter = Update.InlineQuery!.Query;
+            var stickersList = await User.GetStickersList("send_sticker",filter);
             await MessageController.AnswerInlineQuery(InlineQueryId, stickersList, "название");
-            return new();
-        }
-
-        private IEnumerable<InlineQueryResult> GetStickersList(string filter)
-        {
-            var results = new List<InlineQueryResult>
-            {
-                new InlineQueryResultCachedSticker("send_sticker=", "CAACAgIAAxkBAAIWs2DuY4vB50ARmyRwsgABs_7o5weDaAAC-g4AAmq4cUtH6M1FoN4bxSAE")
-            };
-            return results;
+            return new Telegram.Bot.Types.Message();
         }
 
         protected override bool IsMatches(string command)
         {
-            return command.Contains(ChatType.Group.ToString()) ||
-                   command.Contains(ChatType.Supergroup.ToString()) ||
-                   command.Contains(ChatType.Private.ToString());
+            return command.Contains("Group") || command.Contains("Supergroup") || command.Contains("Private");
         }
 
         public ShowStickersInGroup(UserEntity user, Update update, string inlineQueryId)
-            : base(user, update, inlineQueryId)
-        {
-            
-        }
+            : base(user, update, inlineQueryId) { }
         public ShowStickersInGroup() { }
     }
 }

+ 2 - 0
CardCollector/DataBase/CardCollectorDatabase.cs

@@ -22,6 +22,8 @@ namespace CardCollector.DataBase
         // Таблицы базы данных, представленные Entity объектами
         public DbSet<UserEntity> Users { get; set; }
         public DbSet<CashEntity> CashTable { get; set; }
+        public DbSet<UserStickerRelationEntity> UserStickerRelations { get; set; }
+        public DbSet<StickerEntity> Stickers { get; set; }
 
         
         // Конфигурация подключения к БД

+ 19 - 0
CardCollector/DataBase/Entity/StickerEntity.cs

@@ -0,0 +1,19 @@
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace CardCollector.DataBase.Entity
+{
+    [Table("stickers")]
+    public class StickerEntity
+    {
+        [Column("id"), MaxLength(127)] public string Id { get; set; }
+        [Column("title"), MaxLength(256)] public string Title { get; set; }
+        [Column("author"), MaxLength(128)] public string Author { get; set; }
+
+        [Column("income_coins"), MaxLength(32)] public int IncomeCoins { get; set; } = 0;
+        [Column("income_gems"), MaxLength(32)] public int IncomeGems { get; set; } = 0;
+        [Column("tier"), MaxLength(32)] public int Tier { get; set; } = 1;
+        [Column("emoji"), MaxLength(127)] public string Emoji { get; set; } = "";
+        [Column("description"), MaxLength(1024)] public string Description { get; set; } = "";
+    }
+}

+ 21 - 0
CardCollector/DataBase/Entity/UserEntity.cs

@@ -1,6 +1,9 @@
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
+using System.Threading.Tasks;
+using CardCollector.DataBase.EntityDao;
+using Telegram.Bot.Types.InlineQueryResults;
 
 namespace CardCollector.DataBase.Entity
 {
@@ -14,5 +17,23 @@ namespace CardCollector.DataBase.Entity
         [Column("is_blocked"), MaxLength(11)] public bool IsBlocked { get; set; }
         
         [NotMapped] public CashEntity Cash { get; set; }
+        
+        public async Task<IEnumerable<InlineQueryResult>> GetStickersList(string command, string filter)
+        {
+            var result = new List<InlineQueryResult>();
+            var stickers = await UserStickerRelationDao.GetListById(Id);
+            foreach (var sticker in stickers.Values)
+            {
+                if (filter != "")
+                {
+                    var stickerInfo = await StickerDao.GetStickerInfo(sticker.StickerId);
+                    if (!stickerInfo.Title.Contains(filter)) break;
+                }
+                var item = new InlineQueryResultCachedSticker($"{command}={sticker.ShortHash}", sticker.StickerId);
+                result.Add(item);
+                if (result.Count > 49) return result;
+            }
+            return result;
+        }
     }
 }

+ 15 - 0
CardCollector/DataBase/Entity/UserStickerRelationEntity.cs

@@ -0,0 +1,15 @@
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace CardCollector.DataBase.Entity
+{
+    [Table("user_to_stickers_relations")]
+    public class UserStickerRelationEntity
+    {
+        [Column("id"), MaxLength(127)] public long Id { get; set; }
+        [Column("sticker_id"), MaxLength(127)] public string StickerId { get; set; }
+        [Column("user_id"), MaxLength(127)] public long UserId { get; set; }
+        [Column("count"), MaxLength(32)] public int Count { get; set; }
+        [Column("short_hash"), MaxLength(40)] public string ShortHash { get; set; }
+    }
+}

+ 30 - 0
CardCollector/DataBase/EntityDao/StickerDao.cs

@@ -0,0 +1,30 @@
+using System.Threading.Tasks;
+using CardCollector.DataBase.Entity;
+using Microsoft.EntityFrameworkCore;
+
+namespace CardCollector.DataBase.EntityDao
+{
+    public static class StickerDao
+    {
+        private static readonly DbSet<StickerEntity> Table = CardCollectorDatabase.Instance.Stickers;
+        
+        public static async Task<StickerEntity> GetStickerInfo(string stickerId)
+        {
+            return await Table.FindAsync(stickerId);
+        }
+
+        private static async Task<StickerEntity> AddNew(string fileId, string title, string author,
+            int incomeCoins = 0, int incomeGems = 0, int tier = 1, string emoji = "", string description = "")
+
+        {
+            var cash = new StickerEntity
+            {
+                Id = fileId, Title = title, Author = author,
+                IncomeCoins = incomeCoins, IncomeGems = incomeGems,
+                Tier = tier, Emoji = emoji, Description = description
+            };
+            var result = await Table.AddAsync(cash);
+            return result.Entity;
+        }
+    }
+}

+ 34 - 0
CardCollector/DataBase/EntityDao/UserStickerRelationDao.cs

@@ -0,0 +1,34 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using CardCollector.DataBase.Entity;
+using Microsoft.EntityFrameworkCore;
+
+namespace CardCollector.DataBase.EntityDao
+{
+    public static class UserStickerRelationDao
+    {
+        private static readonly DbSet<UserStickerRelationEntity> Table = CardCollectorDatabase.Instance.UserStickerRelations;
+        
+        public static async Task<Dictionary<string, UserStickerRelationEntity>> GetListById(long userId)
+        {
+            var result = await Table
+                .Where(i => i.UserId == userId)
+                .ToDictionaryAsync(p=> p.StickerId, p=> p);
+            return result;
+        }
+
+        private static async Task<UserStickerRelationEntity> AddNew(long userId, string stickerId, int count)
+        {
+            var cash = new UserStickerRelationEntity
+            {
+                UserId = userId,
+                StickerId = stickerId,
+                Count = count,
+                ShortHash = Utilities.CreateMD5(stickerId)
+            };
+            var result = await Table.AddAsync(cash);
+            return result.Entity;
+        }
+    }
+}

+ 1 - 1
CardCollector/Extensions/DictionaryExtensions.cs → CardCollector/Extensions/ListExtensions.cs

@@ -1,6 +1,6 @@
 namespace CardCollector.Extensions
 {
-    public static class DictionaryExtensions
+    public static class ListExtensions
     {
     }
 }

+ 16 - 1
CardCollector/Utilities.cs

@@ -1,4 +1,6 @@
-namespace CardCollector
+using System.Text;
+
+namespace CardCollector
 {
     public class Utilities
     {
@@ -6,5 +8,18 @@
         {
             return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
         }
+        
+        public static string CreateMD5(string input)
+        {
+            // Use input string to calculate MD5 hash
+            using var md5 = System.Security.Cryptography.MD5.Create();
+            var inputBytes = Encoding.ASCII.GetBytes(input);
+            var hashBytes = md5.ComputeHash(inputBytes);
+            // Convert the byte array to hexadecimal string
+            var sb = new StringBuilder();
+            foreach (var t in hashBytes)
+                sb.Append(t.ToString("X2"));
+            return sb.ToString();
+        }
     }
 }