using System; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using System.Threading; using Yavsc.Models.Haircut; using Yavsc.Models.IT.Maintaining; using Yavsc.Models.IT.Fixing; using Yavsc.Server.Models.EMailing; using Yavsc.Server.Models.IT.SourceCode; using Yavsc.Server.Models.IT; namespace Yavsc.Models { using Relationship; using Forms; using Yavsc; using Auth; using Billing; using Musical; using OAuth; using Workflow; using Identity; using Market; using Chat; using Messaging; using Access; using Musical.Profiles; using Workflow.Profiles; using Drawing; using Attributes; using Bank; using Payment; using Yavsc.Models.Calendar; using Blog; using Newtonsoft.Json.Linq; using Yavsc.Abstract.Identity; public class ApplicationDbContext : IdentityDbContext { 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); builder.Entity().HasKey(x => new { x.OwnerId, x.UserId }); builder.Entity().Property(x => x.DeclarationDate).HasDefaultValueSql("LOCALTIMESTAMP"); builder.Entity().HasKey(x => new { x.PostId, x.TagId }); builder.Entity().HasMany(c => c.Connections); builder.Entity().Property(u => u.Avatar).HasDefaultValue(Constants.DefaultAvatar); builder.Entity().Property(u => u.DiskQuota).HasDefaultValue(Constants.DefaultFSQ); builder.Entity().HasKey(u => new { u.DoesCode, u.UserId }); builder.Entity().HasKey(u => new { u.InstrumentId, u.UserId }); builder.Entity().HasKey(a => new { a.CircleId, a.BlogPostId }); builder.Entity().HasKey(c => new { MemberId = c.MemberId, CircleId = c.CircleId }); builder.Entity().HasKey(c => new { uid = c.UserId, notid = c.NotificationId }); builder.Entity().HasKey(ti => new { ti.TaintId, ti.PrestationId }); builder.Entity().HasKey(l => new { l.HRef, l.Method }); builder.Entity().HasKey(l => new { l.Start, l.End }); builder.Entity().HasKey(o => new { o.Code, o.CodeScrutin }); builder.Entity().Property(n => n.icon).HasDefaultValue("exclam"); builder.Entity().HasKey(p => new { room = p.ChannelName, user = p.ChatUserConnectionId }); foreach (var et in builder.Model.GetEntityTypes()) { if (et.ClrType.GetInterface("IBaseTrackedEntity") != null) et.FindProperty("DateCreated").IsReadOnlyAfterSave = true; } } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var appSetup = (string)AppDomain.CurrentDomain.GetData(Constants.YavscConnectionStringEnvName); if (appSetup!=null) optionsBuilder.UseNpgsql(appSetup); } public DbSet Applications { get; set; } public DbSet RefreshTokens { get; set; } /// /// Activities referenced on this site /// /// public DbSet Activities { get; set; } public DbSet UserActivities { get; set; } /// /// Users posts /// /// public DbSet Blogspot { get; set; } /// /// Skills propulsed by this site /// /// public DbSet SiteSkills { get; set; } /// /// Circle members /// /// public DbSet CircleMembers { get; set; } /// /// Special commands, talking about /// a given place and date. /// public DbSet RdvQueries { get; set; } public DbSet HairCutQueries { get; set; } public DbSet HairPrestation { get; set; } public DbSet HairMultiCutQueries { get; set; } public DbSet Performers { get; set; } public DbSet Estimates { get; set; } public DbSet BankStatus { get; set; } public DbSet BankBook { get; set; } /// /// Google Calendar offline /// open auth tokens /// /// tokens public DbSet Tokens { get; set; } /// /// References all declared external GCM devices /// /// public DbSet GCMDevices { get; set; } public DbSet Services { get; set; } public DbSet Products { get; set; } public Task GetTokensAsync(string googleUserId) { if (string.IsNullOrEmpty(googleUserId)) { throw new ArgumentException("email MUST have a value"); } using (var context = new ApplicationDbContext()) { var item = this.Tokens.FirstOrDefault(x => x.UserId == googleUserId); // TODO Refresh token return Task.FromResult(item); } } public Task StoreTokenAsync(string googleUserId, JObject response, string accessToken, string tokenType, string refreshToken, string expiresIn ) { if (string.IsNullOrEmpty(googleUserId)) { throw new ArgumentException("googleUserId MUST have a value"); } var item = this.Tokens.SingleOrDefaultAsync(x => x.UserId == googleUserId).Result; if (item == null) { Tokens.Add(new OAuth2Tokens { TokenType = "Bearer", // FIXME why value.TokenType would be null? AccessToken = accessToken, RefreshToken = refreshToken, Expiration = DateTime.Now.AddSeconds(int.Parse(expiresIn)), UserId = googleUserId }); } else { item.AccessToken = accessToken; item.Expiration = DateTime.Now.AddMinutes(int.Parse(expiresIn)); if (refreshToken != null) item.RefreshToken = refreshToken; Tokens.Update(item); } SaveChanges(googleUserId); return Task.FromResult(0); } Client FindApplication(string clientId) { return Applications.FirstOrDefault( app => app.Id == clientId); } public DbSet ExceptionsSIREN { get; set; } public DbSet Locations { get; set; } public DbSet Tags { get; set; } public DbSet TagsDomain { get; set; } public DbSet EstimateTemplates { get; set; } public DbSet Contacts { get; set; } public DbSet ClientProviderInfo { get; set; } public DbSet Connections { get; set; } public DbSet BlackListed { get; set; } public DbSet MusicalPreferences { get; set; } public DbSet MusicalTendency { get; set; } public DbSet Instrument { get; set; } [ActivitySettings] public DbSet DjSettings { get; set; } [ActivitySettings] public DbSet Instrumentation { get; set; } [ActivitySettings] public DbSet FormationSettings { get; set; } [ActivitySettings] public DbSet GeneralSettings { get; set; } public DbSet WorkflowProviders { get; set; } private void AddTimestamps(string currentUsername) { var entities = ChangeTracker.Entries() .Where(x => x.Entity.GetType().GetInterface("IBaseTrackedEntity") != null && (x.State == EntityState.Added || x.State == EntityState.Modified)); foreach (var entity in entities) { if (entity.State == EntityState.Added) { ((IBaseTrackedEntity)entity.Entity).DateCreated = DateTime.Now; ((IBaseTrackedEntity)entity.Entity).UserCreated = currentUsername; } ((IBaseTrackedEntity)entity.Entity).DateModified = DateTime.Now; ((IBaseTrackedEntity)entity.Entity).UserModified = currentUsername; } } public int SaveChanges(string userId) { AddTimestamps(userId); return base.SaveChanges(); } public async Task SaveChangesAsync(string userId, CancellationToken ctoken = default(CancellationToken)) { AddTimestamps(userId); return await base.SaveChangesAsync(); } public DbSet Circle { get; set; } public DbSet BlogACL { get; set; } public DbSet CommandForm { get; set; } public DbSet
Form { get; set; } public DbSet Banlist { get; set; } public DbSet HairTaint { get; set; } public DbSet Color { get; set; } public DbSet Notification { get; set; } public DbSet DimissClicked { get; set; } [ActivitySettings] public DbSet BrusherProfile { get; set; } public DbSet BankIdentity { get; set; } public DbSet PayPalPayments { get; set; } public DbSet Links { get; set; } public DbSet Period { get; set; } public DbSet BlogTags { get; set; } public DbSet ApplicationUser { get; set; } public DbSet Feature { get; set; } public DbSet Bug { get; set; } public DbSet Comment { get; set; } public DbSet Announce { get; set; } public DbSet ChatConnection { get; set; } public DbSet ChatRoom { get; set; } public DbSet MailingTemplate { get; set; } public DbSet GitRepositoryReference { get; set; } public DbSet Projects { get; set; } } }