yavsc/src/Yavsc.Server/Models/ApplicationDbContext.cs

289 lines
11 KiB
C#
Raw Normal View History

2017-01-19 12:59:49 +01:00

2025-07-15 17:35:14 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Yavsc.Abstract.Models.Messaging;
2018-04-21 19:29:06 +02:00
using Yavsc.Server.Models.EMailing;
2018-06-08 09:57:53 +02:00
using Yavsc.Server.Models.IT.SourceCode;
using Yavsc.Server.Models.IT;
2025-07-15 17:35:14 +01:00
using Yavsc.Abstract.Identity;
using Yavsc.Server.Models.Calendar;
2026-02-09 01:03:33 +00:00
namespace Yavsc.Models
{
2025-07-15 17:35:14 +01:00
using Haircut;
using IT.Evolution;
using IT.Fixing;
using Streaming;
2017-02-11 15:46:26 +01:00
using Relationship;
using Forms;
using Auth;
using Billing;
2017-02-12 22:20:57 +01:00
using Musical;
using Workflow;
using Identity;
using Market;
using Chat;
using Messaging;
using Access;
2017-02-12 22:20:57 +01:00
using Musical.Profiles;
2017-02-12 03:53:33 +01:00
using Workflow.Profiles;
using Drawing;
2017-05-07 22:12:04 +02:00
using Attributes;
using Bank;
using Payment;
2017-10-04 23:53:47 +02:00
using Blog;
2026-02-09 01:03:33 +00:00
using IdentityServer8.Models;
using IdentityServer8.EntityFramework.Entities;
2017-02-25 17:48:11 +01:00
2026-02-09 01:03:33 +00:00
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IdentityServer8.EntityFramework.Interfaces.IConfigurationDbContext
2025-07-15 17:35:14 +01:00
{
public ApplicationDbContext()
{
}
2025-07-15 17:35:14 +01:00
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
2019-05-07 14:01:23 +01:00
builder.Entity<Contact>().HasKey(x => new { x.OwnerId, x.UserId });
2019-05-08 01:35:10 +01:00
builder.Entity<DeviceDeclaration>().Property(x => x.DeclarationDate).HasDefaultValueSql("LOCALTIMESTAMP");
builder.Entity<BlogTag>().HasKey(x => new { x.PostId, x.TagId });
2023-04-05 22:29:40 +01:00
builder.Entity<ApplicationUser>().Property(u => u.FullName).IsRequired(false);
builder.Entity<ApplicationUser>().Property(u => u.DedicatedGoogleCalendar).IsRequired(false);
builder.Entity<ApplicationUser>().HasMany<ChatConnection>(c => c.Connections);
builder.Entity<ApplicationUser>().Property(u => u.Avatar).HasDefaultValue(Constants.DefaultAvatar);
builder.Entity<ApplicationUser>().Property(u => u.DiskQuota).HasDefaultValue(Constants.DefaultFSQ);
2023-04-05 22:29:40 +01:00
builder.Entity<ApplicationUser>().HasAlternateKey(u => u.Email);
builder.Entity<BlackListed>().HasOne<ApplicationUser>(bl => bl.User);
builder.Entity<BlackListed>().HasOne<ApplicationUser>(bl => bl.Owner);
builder.Entity<UserActivity>().HasKey(u => new { u.DoesCode, u.UserId });
builder.Entity<Instrumentation>().HasKey(u => new { u.InstrumentId, u.UserId });
builder.Entity<CircleAuthorizationToBlogPost>().HasKey(a => new { a.CircleId, a.BlogPostId });
2020-09-12 01:11:30 +01:00
builder.Entity<CircleMember>().HasKey(c => new { c.MemberId, c.CircleId });
2024-11-06 13:00:34 +00:00
builder.Entity<DismissClicked>().HasKey(c => new { uid = c.UserId, notid = c.NotificationId });
builder.Entity<HairTaintInstance>().HasKey(ti => new { ti.TaintId, ti.PrestationId });
builder.Entity<HyperLink>().HasKey(l => new { l.HRef, l.Method });
builder.Entity<Period>().HasKey(l => new { l.Start, l.End });
2025-07-15 15:49:48 +01:00
builder.Entity<Cratie.Option>().HasKey(o => new { o.Code, o.CodeScrutin });
builder.Entity<Notification>().Property(n => n.icon).HasDefaultValue("exclam");
2019-05-08 01:35:10 +01:00
builder.Entity<ChatRoomAccess>().HasKey(p => new { room = p.ChannelName, user = p.UserId });
builder.Entity<InstrumentRating>().HasAlternateKey(i => new { Instrument = i.InstrumentId, owner = i.OwnerId });
2026-03-01 14:09:10 +00:00
foreach (var et in builder.Model.GetEntityTypes())
{
if (et.ClrType.GetInterface("IBaseTrackedEntity") != null)
2023-03-19 17:57:55 +00:00
et.FindProperty("DateCreated").SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Ignore);
2017-01-20 14:19:23 +01:00
}
2023-03-26 20:41:42 +01:00
builder.Entity<Activity>().Property(a => a.ParentCode).IsRequired(false);
2025-07-10 19:14:17 +01:00
// builder.Entity<IdentityUserLogin<String>>().HasKey(i=> new { i.LoginProvider, i.UserId, i.ProviderKey });
}
2024-11-06 13:00:34 +00:00
/// <summary>
/// Activities referenced on this site
/// </summary>
/// <returns></returns>
public DbSet<Activity> Activities { get; set; }
2024-11-06 13:00:34 +00:00
public DbSet<UserActivity> UserActivities { get; set; }
2026-03-01 17:25:25 +00:00
/// <summary>
/// Users posts
/// </summary>
/// <returns></returns>
public DbSet<BlogPost> BlogSpot { get; set; }
/// <summary>
2024-11-06 13:00:34 +00:00
/// Skills powered by this site
/// </summary>
/// <returns></returns>
public DbSet<Skill> SiteSkills { get; set; }
/// <summary>
/// Circle members
/// </summary>
/// <returns></returns>
public DbSet<CircleMember> CircleMembers { get; set; }
/// <summary>
/// Special commands, talking about
/// a given place and date.
/// </summary>
2017-02-27 19:28:32 +01:00
public DbSet<RdvQuery> RdvQueries { get; set; }
2017-06-08 00:26:29 +02:00
2017-02-27 19:28:32 +01:00
public DbSet<HairCutQuery> HairCutQueries { get; set; }
2017-04-01 02:14:10 +02:00
public DbSet<HairPrestation> HairPrestation { get; set; }
2017-06-08 00:26:29 +02:00
2017-02-27 19:28:32 +01:00
public DbSet<HairMultiCutQuery> HairMultiCutQueries { get; set; }
public DbSet<PerformerProfile> Performers { get; set; }
2017-06-08 00:26:29 +02:00
public DbSet<Estimate> Estimates { get; set; }
public DbSet<AccountBalance> BankStatus { get; set; }
2025-07-15 15:49:48 +01:00
public DbSet<BalanceImpact> BalanceImpact { get; set; }
2017-04-01 02:14:10 +02:00
/// <summary>
2019-05-08 01:35:10 +01:00
/// References all declared external NativeConfidential devices
/// </summary>
/// <returns></returns>
2019-05-08 01:35:10 +01:00
public DbSet<DeviceDeclaration> DeviceDeclaration { get; set; }
public DbSet<Service> Services { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ExceptionSIREN> ExceptionsSIREN { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Tag> Tags { get; set; }
2017-09-20 13:07:13 +02:00
public DbSet<BlogTag> TagsDomain { get; set; }
public DbSet<EstimateTemplate> EstimateTemplates { get; set; }
2019-05-08 01:35:10 +01:00
public DbSet<Contact> Contact { get; set; }
public DbSet<ClientProviderInfo> ClientProviderInfo { get; set; }
public DbSet<BlackListed> BlackListed { get; set; }
2019-05-07 14:01:23 +01:00
public DbSet<MusicalPreference> MusicalPreference { get; set; }
public DbSet<MusicalTendency> MusicalTendency { get; set; }
2017-03-06 14:50:57 +01:00
public DbSet<Instrument> Instrument { get; set; }
2017-04-01 02:14:10 +02:00
[ActivitySettings]
public DbSet<DjSettings> DjSettings { get; set; }
2017-04-01 02:14:10 +02:00
2017-03-05 16:32:30 +01:00
[ActivitySettings]
public DbSet<Instrumentation> Instrumentation { get; set; }
2017-03-05 16:32:30 +01:00
[ActivitySettings]
public DbSet<FormationSettings> FormationSettings { get; set; }
2017-04-01 02:14:10 +02:00
2017-03-05 16:32:30 +01:00
[ActivitySettings]
public DbSet<GeneralSettings> GeneralSettings { get; set; }
2019-05-07 14:01:23 +01:00
public DbSet<CoWorking> CoWorking { get; set; }
2017-01-17 14:56:53 +01:00
2024-12-14 23:49:13 +00:00
private void AddTimestamps(string userId)
{
var entities =
ChangeTracker.Entries()
2024-12-14 23:49:13 +00:00
.Where(x => x.Entity.GetType().GetInterface(nameof(ITrackedEntity)) != null
&& (x.State == EntityState.Added || x.State == EntityState.Modified));
2017-01-17 14:56:53 +01:00
foreach (var entity in entities)
2017-01-17 14:56:53 +01:00
{
if (entity.State == EntityState.Added)
{
2024-12-14 20:21:41 +00:00
((ITrackedEntity)entity.Entity).DateCreated = DateTime.Now;
2024-12-14 23:49:13 +00:00
((ITrackedEntity)entity.Entity).UserCreated = userId;
}
2017-01-17 14:56:53 +01:00
2024-12-14 20:21:41 +00:00
((ITrackedEntity)entity.Entity).DateModified = DateTime.Now;
2024-12-14 23:49:13 +00:00
((ITrackedEntity)entity.Entity).UserModified = userId;
}
2017-01-17 14:56:53 +01:00
}
public int SaveChanges(string userId)
{
2017-02-25 17:48:11 +01:00
AddTimestamps(userId);
2017-01-17 14:56:53 +01:00
return base.SaveChanges();
}
2017-02-25 17:48:11 +01:00
public async Task<int> SaveChangesAsync(string userId, CancellationToken ctoken = default(CancellationToken))
{
2017-02-25 17:48:11 +01:00
AddTimestamps(userId);
2020-09-12 01:11:30 +01:00
return await base.SaveChangesAsync(ctoken);
2017-01-17 14:56:53 +01:00
}
2017-04-01 02:14:10 +02:00
public DbSet<Circle> Circle { get; set; }
2017-01-20 14:19:23 +01:00
2019-05-07 14:01:23 +01:00
public DbSet<CircleAuthorizationToBlogPost> CircleAuthorizationToBlogPost { get; set; }
2017-01-20 14:19:23 +01:00
public DbSet<CommandForm> CommandForm { get; set; }
2017-01-23 09:52:13 +01:00
public DbSet<Form> Form { get; set; }
2017-01-24 10:43:42 +01:00
2019-05-07 14:01:23 +01:00
public DbSet<Ban> Ban { get; set; }
2017-02-12 03:53:33 +01:00
public DbSet<HairTaint> HairTaint { get; set; }
2017-02-12 03:53:33 +01:00
public DbSet<Color> Color { get; set; }
2017-02-20 15:53:25 +01:00
public DbSet<Notification> Notification { get; set; }
2017-02-20 15:53:25 +01:00
2024-11-06 13:00:34 +00:00
public DbSet<DismissClicked> DismissClicked { get; set; }
2017-02-27 19:28:32 +01:00
2017-02-28 14:01:24 +01:00
[ActivitySettings]
public DbSet<BrusherProfile> BrusherProfile { get; set; }
public DbSet<BankIdentity> BankIdentity { get; set; }
2017-02-01 03:53:00 +01:00
2019-05-07 14:01:23 +01:00
public DbSet<PayPalPayment> PayPalPayment { get; set; }
2017-05-15 13:36:14 +02:00
2019-05-07 14:01:23 +01:00
public DbSet<HyperLink> HyperLink { get; set; }
2017-05-15 13:36:14 +02:00
public DbSet<Period> Period { get; set; }
2017-06-01 14:00:19 +02:00
2019-05-07 14:01:23 +01:00
public DbSet<BlogTag> BlogTag { get; set; }
2017-09-20 13:07:13 +02:00
public DbSet<ApplicationUser> ApplicationUser { get; set; }
public DbSet<Feature> Feature { get; set; }
2017-10-02 04:43:20 +02:00
public DbSet<Bug> Bug { get; set; }
2017-10-02 04:43:20 +02:00
public DbSet<Comment> Comment { get; set; }
2017-10-04 23:53:47 +02:00
public DbSet<Announce> Announce { get; set; }
2019-05-14 21:22:44 +01:00
// TODO remove and opt for for memory only storing,
// as long as it must be set empty each time the service is restarted,
// and that chatting should be kept as must as possible independent from db context
public DbSet<ChatConnection> ChatConnection { get; set; }
2018-01-02 16:35:03 +01:00
public DbSet<ChatRoom> ChatRoom { get; set; }
2018-04-21 19:29:06 +02:00
public DbSet<MailingTemplate> MailingTemplate { get; set; }
2018-01-02 16:35:03 +01:00
public DbSet<GitRepositoryReference> GitRepositoryReference { get; set; }
2018-06-08 09:57:53 +02:00
2021-06-06 20:32:39 +01:00
public DbSet<Project> Project { get; set; }
2020-12-30 03:10:02 +00:00
[Obsolete("use signaled flows")]
2019-01-03 14:42:35 +00:00
public DbSet<LiveFlow> LiveFlow { get; set; }
2019-05-08 03:13:07 +01:00
public DbSet<ChatRoomAccess> ChatRoomAccess { get; set; }
2019-08-20 16:49:38 +01:00
public DbSet<InstrumentRating> InstrumentRating { get; set; }
2024-11-06 13:00:34 +00:00
public DbSet<Scope> Scopes { get; set; }
2025-07-15 17:35:14 +01:00
public DbSet<BlogSpotPublication> blogSpotPublications { get; set; }
2026-02-09 01:03:33 +00:00
public DbSet<IdentityServer8.EntityFramework.Entities.Client> Clients { get; set; }
public DbSet<ClientCorsOrigin> ClientCorsOrigins { get; set; }
public DbSet<IdentityServer8.EntityFramework.Entities.IdentityResource> IdentityResources { get; set; }
public DbSet<IdentityServer8.EntityFramework.Entities.ApiResource> ApiResources { get; set; }
public DbSet<IdentityServer8.EntityFramework.Entities.ApiScope> ApiScopes { get; set; }
public DbSet<IdentityResourceClaim> IdentityResourceClaims { get; set; }
public DbSet<IdentityResourceProperty> IdentityResourceProperties { get; set; }
public DbSet<ApiResourceSecret> ApiResourceSecrets { get; set; }
public DbSet<ApiResourceScope> ApiResourceScopes { get; set; }
public DbSet<ApiResourceClaim> ApiResourceClaims { get; set; }
public DbSet<ApiResourceProperty> ApiResourceProperties { get; set; }
public DbSet<ApiScopeClaim> ApiScopeClaims { get; set; }
public DbSet<ApiScopeProperty> ApiScopeProperties { get; set; }
2026-03-01 02:13:26 +00:00
}
}