using System; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Authentication.OAuth; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using Yavsc.Models.Booking; namespace Yavsc.Models { public class ApplicationDbContext : IdentityDbContext, IApplicationStore { 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.CreationDate).HasDefaultValueSql("LOCALTIMESTAMP"); } public DbSet Applications { get; set; } /// /// Activities referenced on this site /// /// public DbSet Activities { 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; } /// /// Commands, from an user, to a performer /// (A performer is an user who's actived a main activity /// on his profile). /// /// public DbSet Commands { get; set; } /// /// Special commands, talking about /// a given place and date. /// /// public DbSet BookQueries { get; set; } public DbSet Performers { get; set; } public DbSet Estimates { get; set; } public DbSet BankStatus { get; set; } public DbSet BankBook { get; set; } public DbSet Map { 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 Task ClearTokensAsync() { Tokens.RemoveRange(this.Tokens); SaveChanges(); return Task.FromResult(0); } public Task DeleteTokensAsync(string email) { if (string.IsNullOrEmpty(email)) { throw new ArgumentException("email MUST have a value"); } var item = this.Tokens.FirstOrDefault(x => x.UserId == email); if (item != null) { Tokens.Remove(item); SaveChanges(); } return Task.FromResult(0); } 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); return Task.FromResult(item); } } public Task StoreTokenAsync(string googleUserId, OAuthTokenResponse value) { 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 = value.AccessToken, RefreshToken = value.RefreshToken, Expiration = DateTime.Now.AddSeconds(int.Parse(value.ExpiresIn)), UserId = googleUserId }); } else { item.AccessToken = value.AccessToken; item.Expiration = DateTime.Now.AddMinutes(int.Parse(value.ExpiresIn)); if (value.RefreshToken != null) item.RefreshToken = value.RefreshToken; Tokens.Update(item); } SaveChanges(); return Task.FromResult(0); } IApplication IApplicationStore.FindApplication(string clientId) { return Applications.FirstOrDefault( app=>app.ApplicationID == clientId); } } }