diff --git a/JetKarmaBot/Models/Award.cs b/JetKarmaBot/Models/Award.cs index f5e9023..36354f4 100644 --- a/JetKarmaBot/Models/Award.cs +++ b/JetKarmaBot/Models/Award.cs @@ -10,7 +10,7 @@ namespace JetKarmaBot.Models public long ChatId { get; set; } public int FromId { get; set; } public int ToId { get; set; } - public sbyte AwardTypeId { get; set; } + public sbyte? AwardTypeId { get; set; } public sbyte Amount { get; set; } public DateTime Date { get; set; } [ForeignKey("AwardTypeId")] diff --git a/JetKarmaBot/Models/AwardType.cs b/JetKarmaBot/Models/AwardType.cs index 2afeea9..e2a4a74 100644 --- a/JetKarmaBot/Models/AwardType.cs +++ b/JetKarmaBot/Models/AwardType.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; namespace JetKarmaBot.Models { @@ -12,10 +13,13 @@ namespace JetKarmaBot.Models public sbyte AwardTypeId { get; set; } public string CommandName { get; set; } + public long ChatId { get; set; } public string Name { get; set; } public string Symbol { get; set; } public string Description { get; set; } public virtual ICollection Awards { get; set; } + [ForeignKey("ChatId")] + public virtual Chat Chat { get; set; } } } diff --git a/JetKarmaBot/Models/Chat.cs b/JetKarmaBot/Models/Chat.cs index ddb6076..acb0069 100644 --- a/JetKarmaBot/Models/Chat.cs +++ b/JetKarmaBot/Models/Chat.cs @@ -5,15 +5,11 @@ namespace JetKarmaBot.Models { public partial class Chat { - public Chat() - { - Awards = new HashSet(); - } - public long ChatId { get; set; } public string Locale { get; set; } public bool IsAdministrator { get; set; } public virtual ICollection Awards { get; set; } + public virtual ICollection AwardTypes { get; set; } } } diff --git a/JetKarmaBot/Models/KarmaContext.cs b/JetKarmaBot/Models/KarmaContext.cs index 179c5e4..4307dd5 100644 --- a/JetKarmaBot/Models/KarmaContext.cs +++ b/JetKarmaBot/Models/KarmaContext.cs @@ -112,8 +112,8 @@ namespace JetKarmaBot.Models .HasName("awardtypeid_UNIQUE") .IsUnique(); - entity.HasIndex(e => e.CommandName) - .HasName("commandname_UNIQUE") + entity.HasIndex(e => new { e.CommandName, e.ChatId }) + .HasName("un_cnandchat") .IsUnique(); entity.Property(e => e.AwardTypeId) @@ -139,6 +139,12 @@ namespace JetKarmaBot.Models .IsRequired() .HasColumnName("symbol") .HasColumnType("varchar(16)"); + + entity.HasOne(d => d.Chat) + .WithMany(p => p.AwardTypes) + .HasForeignKey(d => d.ChatId) + .OnDelete(DeleteBehavior.ClientSetNull) + .HasConstraintName("fk_chat"); }); modelBuilder.Entity(entity => diff --git a/karma.sql b/karma.sql index 22cd05d..abef214 100644 --- a/karma.sql +++ b/karma.sql @@ -3,17 +3,15 @@ DROP TABLE IF EXISTS `awardtype`; CREATE TABLE `awardtype` ( - `awardtypeid` tinyint(3) NOT NULL PRIMARY KEY, - `commandname` varchar(35) NOT NULL UNIQUE, + `awardtypeid` tinyint(3) NOT NULL PRIMARY KEY AUTO_INCREMENT, + `commandname` varchar(35) NOT NULL, + `chatid` bigint(20) NOT NULL REFERENCES `chat` (`chatid`), `name` varchar(32) NOT NULL, `symbol` varchar(16) NOT NULL, - `description` text NOT NULL + `description` text NOT NULL, + UNIQUE KEY `un_cnandchat` (`commandname`, `chatid`) ); -LOCK TABLES `awardtype` WRITE; -INSERT INTO `awardtype` VALUES (1,'example','Example','Examples','An example'); -UNLOCK TABLES; - DROP TABLE IF EXISTS `chat`; CREATE TABLE `chat` ( `chatid` bigint(20) NOT NULL PRIMARY KEY, @@ -23,9 +21,8 @@ CREATE TABLE `chat` ( DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( - `userid` bigint(20) NOT NULL, - `username` varchar(45) DEFAULT NULL, - PRIMARY KEY (`userid`) + `userid` bigint(20) NOT NULL PRIMARY KEY, + `username` varchar(45) DEFAULT NULL ); DROP TABLE IF EXISTS `award`; @@ -34,7 +31,7 @@ CREATE TABLE `award` ( `chatid` bigint(20) NOT NULL REFERENCES `chat` (`chatid`), `fromid` bigint(20) NOT NULL REFERENCES `user` (`userid`), `toid` bigint(20) NOT NULL REFERENCES `user` (`userid`), - `awardtypeid` tinyint(3) NOT NULL REFERENCES `awardtype` (`awardtypeid`), + `awardtypeid` tinyint(3) REFERENCES `awardtype` (`awardtypeid`), `amount` tinyint(3) NOT NULL DEFAULT 1, `date` datetime NOT NULL DEFAULT current_timestamp() ); \ No newline at end of file