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 7.3KB

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