WIP entity framework

This commit is contained in:
Basique Evangelist 2019-01-06 18:29:00 +03:00
parent 93e69db9ad
commit ba9f698040
10 changed files with 141 additions and 117 deletions

View File

@ -17,7 +17,7 @@ namespace JetKarmaBot.Commands
{
using (var db = Db.GetContext())
{
var currentLocale = Locale[db.Chat.Find(args.Message.Chat.Id).Locale];
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
if (args.Message.ReplyToMessage == null)
{
Client.SendTextMessageAsync(args.Message.Chat.Id, currentLocale["jetkarmabot.award.errawardnoreply"]);
@ -52,16 +52,16 @@ namespace JetKarmaBot.Commands
var text = args.Message.Text;
var awardTypeText = cmd.Parameters.FirstOrDefault();
var awardType = awardTypeText != null
? db.Awardtype.First(at => at.Commandname == awardTypeText)
: db.Awardtype.Find(1);
? db.AwardTypes.First(at => at.CommandName == awardTypeText)
: db.AwardTypes.Find((sbyte)1);
db.Award.Add(new Models.Award()
db.Awards.Add(new Models.Award()
{
Awardtypeid = awardType.Awardtypeid,
AwardTypeId = awardType.AwardTypeId,
Amount = (sbyte)(awarding ? 1 : -1),
Fromid = awarder.Id,
Toid = recipient.Id,
Chatid = args.Message.Chat.Id
FromId = awarder.Id,
ToId = recipient.Id,
ChatId = args.Message.Chat.Id
});
db.SaveChanges();
@ -70,8 +70,8 @@ namespace JetKarmaBot.Commands
? string.Format(currentLocale["jetkarmabot.award.awardmessage"], awardType.Name, "@" + recipient.Username)
: string.Format(currentLocale["jetkarmabot.award.revokemessage"], awardType.Name, "@" + recipient.Username);
var currentCount = db.Award
.Where(aw => aw.Toid == recipient.Id && aw.Awardtypeid == awardType.Awardtypeid)
var currentCount = db.Awards
.Where(aw => aw.ToId == recipient.Id && aw.AwardTypeId == awardType.AwardTypeId)
.Sum(aw => aw.Amount);
var response = message + "\n" + String.Format(currentLocale["jetkarmabot.award.statustext"], "@" + recipient.Username, currentCount, awardType.Symbol);

View File

@ -16,7 +16,9 @@ namespace JetKarmaBot.Commands
public bool Execute(CommandString cmd, MessageEventArgs args)
{
var currentLocale = Locale[Db.Chats[args.Message.Chat.Id].Locale];
using (var db = Db.GetContext())
{
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
if (cmd.Parameters.Length < 1)
{
Client.SendTextMessageAsync(
@ -25,14 +27,16 @@ namespace JetKarmaBot.Commands
replyToMessageId: args.Message.MessageId);
return true;
}
Db.ChangeChatLocale(Db.Chats[args.Message.Chat.Id], cmd.Parameters[0]);
currentLocale = Locale[Db.Chats[args.Message.Chat.Id].Locale];
db.Chats.Find(args.Message.Chat.Id).Locale = cmd.Parameters[0];
currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
Client.SendTextMessageAsync(
args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.justchanged"],
replyToMessageId: args.Message.MessageId);
db.SaveChanges();
return true;
}
}
[Inject] KarmaContextFactory Db { get; set; }
[Inject] TelegramBotClient Client { get; set; }

View File

@ -15,8 +15,8 @@ namespace JetKarmaBot.Commands
{
using (var db = Db.GetContext())
{
db.Chat.Add(new Models.Chat { Chatid = args.Message.Chat.Id });
db.User.Add(new Models.User { Userid = args.Message.From.Id });
db.Chats.Add(new Models.Chat { ChatId = args.Message.Chat.Id });
db.Users.Add(new Models.User { UserId = args.Message.From.Id });
db.SaveChanges();
return true;
}

View File

@ -17,7 +17,9 @@ namespace JetKarmaBot.Commands
public bool Execute(CommandString cmd, MessageEventArgs args)
{
var currentLocale = Locale[Db.Chats[args.Message.Chat.Id].Locale];
using (var db = Db.GetContext())
{
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
var asker = args.Message.From;
var awardTypeName = cmd.Parameters.FirstOrDefault();
@ -25,18 +27,27 @@ namespace JetKarmaBot.Commands
if (string.IsNullOrWhiteSpace(awardTypeName))
{
var awards = Db.CountAllUserAwards(asker.Id);
// var awards = db.Awards.Where(x => x.ToId == asker.Id)
// .GroupBy(x => x.AwardTypeId)
// .Select(x => new { AwardTypeId = x.Key, Amount = x.Sum(y => y.Amount) });
var awardsQuery = from award in db.Awards
where award.ToId == asker.Id
group award by award.AwardTypeId into g
select new { AwardTypeId = g.Key, Amount = g.Sum(x => x.Amount) };
response = currentLocale["jetkarmabot.status.listalltext"] + "\n"
+ string.Join("\n", awards.Select(a => $" - {Db.AwardTypes[a.AwardTypeId].Symbol} {a.Amount}"));
+ string.Join("\n", awardsQuery.Select(a => $" - {db.AwardTypes.Find(a.AwardTypeId).Symbol} {a.Amount}"));
}
else
{
var awardTypeId = Db.GetAwardTypeId(cmd.Parameters.FirstOrDefault());
var awardType = Db.AwardTypes[awardTypeId];
var awardTypeIdQuery = from awt in db.AwardTypes
where awt.CommandName == awardTypeName
select awt.AwardTypeId;
var awardTypeId = awardTypeIdQuery.First();
var awardType = db.AwardTypes.Find(awardTypeId);
response = string.Format(currentLocale["jetkarmabot.status.listspecifictext"], Db.CountUserAwards(asker.Id, awardTypeId), awardType.Symbol);
response = string.Format(currentLocale["jetkarmabot.status.listspecifictext"], db.Awards.Where(x => x.AwardTypeId == awardTypeId && x.ToId == asker.Id).Sum(x => x.Amount), awardType.Symbol);
}
Client.SendTextMessageAsync(
@ -45,6 +56,7 @@ namespace JetKarmaBot.Commands
replyToMessageId: args.Message.MessageId);
return true;
}
}
[Inject] KarmaContextFactory Db { get; set; }
[Inject] TelegramBotClient Client { get; set; }

View File

@ -1,21 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace JetKarmaBot.Models
{
public partial class Award
{
public int Awardid { get; set; }
public long Chatid { get; set; }
public long Fromid { get; set; }
public long Toid { get; set; }
public sbyte Awardtypeid { get; set; }
public int AwardId { get; set; }
public long ChatId { get; set; }
public int FromId { get; set; }
public int ToId { get; set; }
public sbyte AwardTypeId { get; set; }
public sbyte Amount { get; set; }
public DateTime Date { get; set; }
public Awardtype Awardtype { get; set; }
public Chat Chat { get; set; }
public User From { get; set; }
public User To { get; set; }
[ForeignKey("AwardTypeId")]
public virtual AwardType AwardType { get; set; }
[ForeignKey("ChatId")]
public virtual Chat Chat { get; set; }
[ForeignKey("FromId")]
public virtual User From { get; set; }
[ForeignKey("ToId")]
public virtual User To { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
namespace JetKarmaBot.Models
{
public partial class AwardType
{
public AwardType()
{
Awards = new HashSet<Award>();
}
public sbyte AwardTypeId { get; set; }
public string CommandName { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
public string Description { get; set; }
public virtual ICollection<Award> Awards { get; set; }
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
namespace JetKarmaBot.Models
{
public partial class Awardtype
{
public Awardtype()
{
Award = new HashSet<Award>();
}
public sbyte Awardtypeid { get; set; }
public string Commandname { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
public string Description { get; set; }
public ICollection<Award> Award { get; set; }
}
}

View File

@ -7,12 +7,12 @@ namespace JetKarmaBot.Models
{
public Chat()
{
Award = new HashSet<Award>();
Awards = new HashSet<Award>();
}
public long Chatid { get; set; }
public long ChatId { get; set; }
public string Locale { get; set; }
public ICollection<Award> Award { get; set; }
public virtual ICollection<Award> Awards { get; set; }
}
}

View File

@ -1,9 +1,11 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Perfusion;
namespace JetKarmaBot.Models
{
[Transient]
public partial class KarmaContext : DbContext
{
public KarmaContext()
@ -15,10 +17,10 @@ namespace JetKarmaBot.Models
{
}
public virtual DbSet<Award> Award { get; set; }
public virtual DbSet<Awardtype> Awardtype { get; set; }
public virtual DbSet<Chat> Chat { get; set; }
public virtual DbSet<User> User { get; set; }
public virtual DbSet<Award> Awards { get; set; }
public virtual DbSet<AwardType> AwardTypes { get; set; }
public virtual DbSet<Chat> Chats { get; set; }
public virtual DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -31,23 +33,23 @@ namespace JetKarmaBot.Models
{
entity.ToTable("award");
entity.HasIndex(e => e.Awardid)
entity.HasIndex(e => e.AwardId)
.HasName("awardid_UNIQUE")
.IsUnique();
entity.HasIndex(e => e.Awardtypeid)
entity.HasIndex(e => e.AwardTypeId)
.HasName("fk_awardtype_idx");
entity.HasIndex(e => e.Chatid)
entity.HasIndex(e => e.ChatId)
.HasName("fk_chat_idx");
entity.HasIndex(e => e.Fromid)
entity.HasIndex(e => e.FromId)
.HasName("fk_from_idx");
entity.HasIndex(e => e.Toid)
entity.HasIndex(e => e.ToId)
.HasName("fk_to_idx");
entity.Property(e => e.Awardid)
entity.Property(e => e.AwardId)
.HasColumnName("awardid")
.HasColumnType("int(11)");
@ -56,11 +58,11 @@ namespace JetKarmaBot.Models
.HasColumnType("tinyint(3)")
.HasDefaultValueSql("'1'");
entity.Property(e => e.Awardtypeid)
entity.Property(e => e.AwardTypeId)
.HasColumnName("awardtypeid")
.HasColumnType("tinyint(3)");
entity.Property(e => e.Chatid)
entity.Property(e => e.ChatId)
.HasColumnName("chatid")
.HasColumnType("bigint(20)");
@ -69,56 +71,56 @@ namespace JetKarmaBot.Models
.HasColumnType("datetime")
.HasDefaultValueSql("'CURRENT_TIMESTAMP'");
entity.Property(e => e.Fromid)
entity.Property(e => e.FromId)
.HasColumnName("fromid")
.HasColumnType("bigint(20)");
entity.Property(e => e.Toid)
entity.Property(e => e.ToId)
.HasColumnName("toid")
.HasColumnType("bigint(20)");
entity.HasOne(d => d.Awardtype)
.WithMany(p => p.Award)
.HasForeignKey(d => d.Awardtypeid)
entity.HasOne(d => d.AwardType)
.WithMany(p => p.Awards)
.HasForeignKey(d => d.AwardTypeId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_awardtype");
entity.HasOne(d => d.Chat)
.WithMany(p => p.Award)
.HasForeignKey(d => d.Chatid)
.WithMany(p => p.Awards)
.HasForeignKey(d => d.ChatId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_chat");
entity.HasOne(d => d.From)
.WithMany(p => p.AwardFrom)
.HasForeignKey(d => d.Fromid)
.WithMany(p => p.AwardsFrom)
.HasForeignKey(d => d.FromId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_from");
entity.HasOne(d => d.To)
.WithMany(p => p.AwardTo)
.HasForeignKey(d => d.Toid)
.WithMany(p => p.AwardsTo)
.HasForeignKey(d => d.ToId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_to");
});
modelBuilder.Entity<Awardtype>(entity =>
modelBuilder.Entity<AwardType>(entity =>
{
entity.ToTable("awardtype");
entity.HasIndex(e => e.Awardtypeid)
entity.HasIndex(e => e.AwardTypeId)
.HasName("awardtypeid_UNIQUE")
.IsUnique();
entity.HasIndex(e => e.Commandname)
entity.HasIndex(e => e.CommandName)
.HasName("commandname_UNIQUE")
.IsUnique();
entity.Property(e => e.Awardtypeid)
entity.Property(e => e.AwardTypeId)
.HasColumnName("awardtypeid")
.HasColumnType("tinyint(3)");
entity.Property(e => e.Commandname)
entity.Property(e => e.CommandName)
.IsRequired()
.HasColumnName("commandname")
.HasColumnType("varchar(35)");
@ -143,7 +145,7 @@ namespace JetKarmaBot.Models
{
entity.ToTable("chat");
entity.Property(e => e.Chatid)
entity.Property(e => e.ChatId)
.HasColumnName("chatid")
.HasColumnType("bigint(20)");
@ -158,7 +160,7 @@ namespace JetKarmaBot.Models
{
entity.ToTable("user");
entity.Property(e => e.Userid)
entity.Property(e => e.UserId)
.HasColumnName("userid")
.HasColumnType("bigint(20)");

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace JetKarmaBot.Models
{
@ -7,14 +8,15 @@ namespace JetKarmaBot.Models
{
public User()
{
AwardFrom = new HashSet<Award>();
AwardTo = new HashSet<Award>();
AwardsFrom = new HashSet<Award>();
AwardsTo = new HashSet<Award>();
}
public long Userid { get; set; }
public int UserId { get; set; }
public string Username { get; set; }
public ICollection<Award> AwardFrom { get; set; }
public ICollection<Award> AwardTo { get; set; }
[InverseProperty("From")]
public virtual ICollection<Award> AwardsFrom { get; set; }
[InverseProperty("To")]
public virtual ICollection<Award> AwardsTo { get; set; }
}
}