The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TeknikEntities.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Teknik.Areas.Blog.Models;
  2. using Teknik.Areas.Users.Models;
  3. using Teknik.Areas.Contact.Models;
  4. using Teknik.Areas.Upload.Models;
  5. using Teknik.Areas.Paste.Models;
  6. using Teknik.Areas.Podcast.Models;
  7. using Teknik.Areas.Stats.Models;
  8. using Teknik.Areas.Shortener.Models;
  9. using Teknik.Attributes;
  10. using System.Linq;
  11. using Teknik.Areas.Vault.Models;
  12. using Microsoft.EntityFrameworkCore;
  13. using Teknik.Models;
  14. using System.Configuration;
  15. namespace Teknik.Data
  16. {
  17. public class TeknikEntities : DbContext
  18. {
  19. // Users
  20. public DbSet<User> Users { get; set; }
  21. public DbSet<LoginInfo> UserLogins { get; set; }
  22. public DbSet<InviteCode> InviteCodes { get; set; }
  23. // User Settings
  24. public DbSet<UserSettings> UserSettings { get; set; }
  25. public DbSet<BlogSettings> BlogSettings { get; set; }
  26. public DbSet<UploadSettings> UploadSettings { get; set; }
  27. // Blogs
  28. public DbSet<Blog> Blogs { get; set; }
  29. public DbSet<BlogPost> BlogPosts { get; set; }
  30. public DbSet<BlogPostTag> BlogPostTagss { get; set; }
  31. public DbSet<BlogPostComment> BlogPostComments { get; set; }
  32. // Contact
  33. public DbSet<Contact> Contact { get; set; }
  34. // Uploads
  35. public DbSet<Upload> Uploads { get; set; }
  36. // Pastes
  37. public DbSet<Paste> Pastes { get; set; }
  38. // Podcasts
  39. public DbSet<Podcast> Podcasts { get; set; }
  40. public DbSet<PodcastFile> PodcastFiles { get; set; }
  41. public DbSet<PodcastComment> PodcastComments { get; set; }
  42. // Transparency
  43. public DbSet<Transaction> Transactions { get; set; }
  44. public DbSet<Takedown> Takedowns { get; set; }
  45. // Url Shortener
  46. public DbSet<ShortenedUrl> ShortenedUrls { get; set; }
  47. // Vaults
  48. public DbSet<Vault> Vaults { get; set; }
  49. public DbSet<VaultItem> VaultItems { get; set; }
  50. public TeknikEntities(DbContextOptions<TeknikEntities> options)
  51. : base(options)
  52. {
  53. }
  54. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  55. {
  56. if (!optionsBuilder.IsConfigured)
  57. {
  58. optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["TeknikEntities"].ConnectionString);
  59. }
  60. }
  61. protected override void OnModelCreating(ModelBuilder modelBuilder)
  62. {
  63. // User
  64. modelBuilder.Entity<User>()
  65. .HasKey(u => u.UserId);
  66. modelBuilder.Entity<User>().OwnsOne(u => u.UserSettings);
  67. modelBuilder.Entity<User>().OwnsOne(u => u.BlogSettings);
  68. modelBuilder.Entity<User>().OwnsOne(u => u.UploadSettings);
  69. modelBuilder.Entity<User>().HasMany(u => u.Uploads).WithOne(u => u.User);
  70. modelBuilder.Entity<User>().HasMany(u => u.Pastes).WithOne(u => u.User);
  71. modelBuilder.Entity<User>().HasMany(u => u.ShortenedUrls).WithOne(u => u.User);
  72. modelBuilder.Entity<User>().HasMany(u => u.Vaults).WithOne(u => u.User);
  73. modelBuilder.Entity<User>().HasMany(u => u.OwnedInviteCodes).WithOne(i => i.Owner);
  74. modelBuilder.Entity<User>().HasOne(u => u.ClaimedInviteCode).WithOne(i => i.ClaimedUser);
  75. modelBuilder.Entity<User>().HasMany(u => u.Logins).WithOne(r => r.User);
  76. modelBuilder.Entity<User>().HasOne(u => u.ClaimedInviteCode).WithOne(t => t.ClaimedUser); // Legacy???
  77. modelBuilder.Entity<User>().HasMany(u => u.OwnedInviteCodes).WithOne(t => t.Owner); // Legacy???
  78. // Invite Codes
  79. //modelBuilder.Entity<InviteCode>().HasOne(t => t.ClaimedUser).WithOne(u => u.ClaimedInviteCode).HasPrincipalKey("ClaimedUserId").HasForeignKey("ClaimedUser_UserId"); // Legacy???
  80. //modelBuilder.Entity<InviteCode>().HasOne(t => t.Owner).WithMany(u => u.OwnedInviteCodes).HasPrincipalKey("ClaimedUserId").HasForeignKey("Owner_UserId"); // Legacy???
  81. // Blogs
  82. modelBuilder.Entity<BlogPost>().HasMany(p => p.Comments).WithOne(c => c.BlogPost);
  83. // Uploads
  84. modelBuilder.Entity<Upload>().HasOne(u => u.User);
  85. // Pastes
  86. modelBuilder.Entity<Paste>().HasOne(u => u.User);
  87. // Shortened URLs
  88. modelBuilder.Entity<ShortenedUrl>().HasOne(u => u.User);
  89. // Vaults
  90. modelBuilder.Entity<Vault>().HasOne(u => u.User);
  91. // Takedowns
  92. modelBuilder.Entity<Takedown>().HasMany(t => t.Attachments).WithOne().HasForeignKey("Takedown_TakedownId"); // Legacy???
  93. // Transactions
  94. modelBuilder.Entity<Transaction>().Property(t => t.Amount).HasColumnType("decimal(19, 5)");
  95. // Users
  96. modelBuilder.Entity<User>().ToTable("Users");
  97. modelBuilder.Entity<LoginInfo>().ToTable("UserLogins");
  98. modelBuilder.Entity<InviteCode>().ToTable("InviteCodes");
  99. // User Settings
  100. modelBuilder.Entity<UserSettings>().ToTable("Users");
  101. modelBuilder.Entity<BlogSettings>().ToTable("Users");
  102. modelBuilder.Entity<UploadSettings>().ToTable("Users");
  103. // Blogs
  104. modelBuilder.Entity<Blog>().ToTable("Blogs");
  105. modelBuilder.Entity<BlogPost>().ToTable("BlogPosts");
  106. modelBuilder.Entity<BlogPostComment>().ToTable("BlogPostComments");
  107. modelBuilder.Entity<BlogPostTag>().ToTable("BlogPostTags");
  108. // Contact
  109. modelBuilder.Entity<Contact>().ToTable("Contact");
  110. // Uploads
  111. modelBuilder.Entity<Upload>().ToTable("Uploads");
  112. // Pastes
  113. modelBuilder.Entity<Paste>().ToTable("Pastes");
  114. // Shortened Urls
  115. modelBuilder.Entity<ShortenedUrl>().ToTable("ShortenedUrls");
  116. // Vaults
  117. modelBuilder.Entity<Vault>().ToTable("Vaults");
  118. modelBuilder.Entity<VaultItem>().ToTable("VaultItems");
  119. // Podcasts
  120. modelBuilder.Entity<Podcast>().ToTable("Podcasts");
  121. modelBuilder.Entity<PodcastFile>().ToTable("PodcastFiles");
  122. modelBuilder.Entity<PodcastComment>().ToTable("PodcastComments");
  123. modelBuilder.Entity<PodcastTag>().ToTable("PodcastTags");
  124. // Transparency
  125. modelBuilder.Entity<Transaction>().ToTable("Transactions");
  126. modelBuilder.Entity<Takedown>().ToTable("Takedowns");
  127. // Custom Attributes
  128. foreach (var entityType in modelBuilder.Model.GetEntityTypes())
  129. {
  130. foreach (var property in entityType.GetProperties())
  131. {
  132. var attributes = property?.PropertyInfo?.GetCustomAttributes(typeof(CaseSensitiveAttribute), false);
  133. if (attributes != null && attributes.Any())
  134. {
  135. property.SetAnnotation("CaseSensitive", true);
  136. }
  137. }
  138. }
  139. base.OnModelCreating(modelBuilder);
  140. }
  141. }
  142. }