protected override async Task <IEnumerable <Notification> > OnExecute(IReadableDataSource dataSource) { if (User != null && User.Id == 0) { throw new ArgumentOutOfRangeException(nameof(User), "User.Id must have a value"); } var databaseConnection = await dataSource.GetDbConnection(); var results = await databaseConnection.QueryAsync <Notification, NotificationPriority, User, Tenant, Notification>( "core.usp_ListClientNotifications", (n, np, usr, ten) => { n.Priority = np; if (usr != null && usr.Id != 0) { n.TargetUser = usr; } if (ten != null && ten.Id != 0) { n.TargetTenant = ten; } return(n); }, new { Calling_User_ID = User.Id, }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
protected override async Task <IEnumerable <Tenant> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var userTenants = await databaseConnection.QueryAsync <Tenant, Group, GroupMembership, Tenant>("tenants.usp_GetTenantsByUserId", (ten, grp, grpMem) => { // Tie the group to the WellKnownGroups dictionary if we can... if (ten != null && grp != null) { ten.WellKnownGroups = new Dictionary <WellKnownGroupType, Group>() { { WellKnownGroupType.TenantMembers, grp } }; } // ...then the group membership to the group... if (grp != null && grpMem != null) { grp.Users = new[] { grpMem }; } // ...then the user to the group membership if (grpMem != null) { grpMem.User = User; } return(ten); }, new { User_ID = User.Id, Active_Memberships_Only = ActiveMembershipsOnly }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(userTenants); }
protected override async Task <IEnumerable <AuditTrailEntry> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); // This technically isn't using some of our inheritence hierarchy, but it doesn't make much difference at this point var auditTrailEntries = await databaseConnection.QueryAsync <ImplAuditTrailEntry, User, Tenant, User, Service, ImplAuditTrailEntry>("security.usp_GetAuditTrail", (aud, rU, rT, pbU, svc) => { aud.RelatedUser = rU; aud.RelatedTenant = rT; aud.PerformedBy = pbU; aud.OriginatingService = svc; return(aud); }, new { Start_Time = StartTime, End_Time = EndTime, User_ID = AssociatedUser?.Id, Tenant_ID = AssociatedTenant?.Id, Performed_By_User_ID = PerformedBy?.Id, Type_ID = Type }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(auditTrailEntries); }
protected EmailBatchTypeHandlerBase(ILogger logger, IReadableDataSource dataSource, EmailBatch emailBatch, SendGridConfiguration sendGridConfiguration) { _logger = logger; _dataSource = dataSource; _sendGridConfiguration = sendGridConfiguration; EmailBatch = emailBatch; }
protected override async Task <IEnumerable <Product> > OnExecute(IReadableDataSource dataSource) { if (CurrentUser.Id == 0) { throw new ArgumentOutOfRangeException(nameof(User), "User.Id must have a value"); } var databaseConnection = await dataSource.GetDbConnection(); var resultSet = await databaseConnection.QueryMultipleAsync("products.usp_ListProducts", new { Include_Hidden = IncludeHiddenProducts, User_ID = CurrentUser.Id }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); // The first result set is pretty simple - we're just performing a straightforward mapping from a result set to // the list of products. var products = await resultSet.ReadAsync <Product>(); // The second and third result sets are a bit trickier, since we've got to join them together, then // convert from a dynamic object into a configuration or product characteristic IEnumerable <dynamic> productConfigurations = await resultSet.ReadAsync(); IEnumerable <dynamic> productCharacteristics = await resultSet.ReadAsync(); var groupedCharacteristics = from prod in products join pC in productCharacteristics on prod.Id equals pC.ProductId into gj join pConfig in productConfigurations on prod.Id equals pConfig.ProductId into gConfig select new { Product = prod, Characteristics = (from pC in gj select new ProductCharacteristic() { Id = pC.Id, Name = pC.Name, Description = pC.Description, Visible = pC.Visible, Enabled = pC.Enabled, ValueType = (ProductCharacteristicValueType)pC.ValueType }).ToArray(), Configuration = (from pConfig in gConfig select new RawProductConfiguration(pConfig.ConfigurationString as string) { Id = pConfig.Id }).Single() }; foreach (var prod in groupedCharacteristics) { prod.Product.ApplicableCharacteristics = prod.Characteristics; prod.Product.DefaultProductConfiguration = prod.Configuration; } return(products); }
protected override async Task <IEnumerable <Tenant> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var resultantFilter = NameFilter.Replace("*", "%"); var matchingTenants = await databaseConnection.QueryAsync <Tenant>("tenants.usp_GetTenantsByName", new { Name_Filter = resultantFilter }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(matchingTenants); }
protected override async Task <IEnumerable <EmailBatch> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var results = await databaseConnection.QueryAsync <EmailBatch, EmailBatchType, EmailBatch>("core.usp_ListBatches", (eb, ebt) => { eb.Type = ebt; return(eb); }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
/// <summary> /// Gets the correct database connection from this <see cref="IReadableDataSource"/>. /// </summary> /// <param name="dataSource">The <see cref="IReadableDataSource"/> to get the database connection from.</param> /// <returns>The associated database connection.</returns> public static async Task <IDbConnection> GetDbConnection(this IReadableDataSource dataSource) { if (dataSource == null) { throw new ArgumentNullException(nameof(dataSource)); } if (dataSource is SqlServerDataSource sqlServerDataSource) { return(await sqlServerDataSource.GetReadOnlyDatabaseConnection()); } throw new InvalidCastException($"{nameof(dataSource)} is not an expected type"); }
protected override async Task <IEnumerable <Tier> > OnExecute(IReadableDataSource dataSource) { if (CurrentUser.Id == 0) { throw new ArgumentOutOfRangeException(nameof(User), "User.Id must have a value"); } var databaseConnection = await dataSource.GetDbConnection(); var listTiersBatch = await databaseConnection.QueryMultipleAsync("products.usp_ListProductTiers", new { Product_ID = Product.Id, Include_Hidden = IncludeHiddenTiers, User_ID = CurrentUser.Id }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); // The first result set is simple enough: a mapped set of Tiers var productTiers = await listTiersBatch.ReadAsync <Tier>(); // The second result set is a bit more complex: convert from a dynamic object into a // tier product characteristic value IEnumerable <dynamic> tierCharacteristicValues = await listTiersBatch.ReadAsync(); var groupedTCVs = from tier in productTiers join tcv in tierCharacteristicValues on tier.Id equals tcv.TierId into gj select new { Tier = tier, TierCharacteristicValues = (from tcv in gj select new TierProductCharacteristic() { Id = tcv.TierCharacteristicValueId, CharacteristicValue = tcv.Value, ProductCharacteristic = new ProductCharacteristic() { Id = tcv.CharacteristicId, Name = tcv.Name, Description = tcv.Description, Visible = tcv.Visible, Enabled = tcv.Enabled, ValueType = (ProductCharacteristicValueType)tcv.ValueType } }).ToArray() }; foreach (var tier in groupedTCVs) { tier.Tier.Characteristics = tier.TierCharacteristicValues; } return(productTiers); }
protected override async Task <IEnumerable <Group> > OnExecute(IReadableDataSource dataSource) { if (Tenant.Id == 0) { throw new ArgumentOutOfRangeException(nameof(Tenant), "Tenant.Id must have a value"); } var databaseConnection = await dataSource.GetDbConnection(); var tenantGroups = await databaseConnection.QueryAsync <Group>("security.usp_GetTenantSecurityGroups", new { Tenant_ID = Tenant.Id }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(tenantGroups); }
protected override async Task <IEnumerable <ExternalComponent> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var results = await databaseConnection.QueryAsync <ExternalComponent, Purpose, ExternalComponentStatus, ExternalComponent>("components.usp_ListAll", (ec, ecP, ecS) => { ec.Purpose = ecP; ec.Status = ecS; return(ec); }, null, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
protected override async Task <IEnumerable <Notification> > OnExecute(IReadableDataSource dataSource) { if (User != null && User.Id == 0) { throw new ArgumentOutOfRangeException(nameof(User), "User.Id must have a value"); } if (Tenant != null && Tenant.Id == 0) { throw new ArgumentOutOfRangeException(nameof(Tenant), "Tenant.Id must have a value"); } if (User != null && User.Id != 0 && Tenant != null && Tenant.Id != 0) { throw new ArgumentException("User.Id and Tenant.Id cannot both have values"); } var databaseConnection = await dataSource.GetDbConnection(); var results = await databaseConnection.QueryAsync <Notification, NotificationPriority, User, Tenant, EmailBatch, Notification>( "core.usp_ListNotifications", (n, np, usr, ten, eb) => { n.Priority = np; if (usr != null && usr.Id != 0) { n.TargetUser = usr; } if (ten != null && ten.Id != 0) { n.TargetTenant = ten; } if (eb != null && eb.Id != 0) { n.EmailBatch = eb; } return(n); }, new { User_ID = User?.Id, Tenant_ID = Tenant?.Id }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
protected override async Task <IEnumerable <Invitation> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var tenantInvitations = await databaseConnection.QueryAsync <Invitation, User, Invitation>("tenants.usp_GetInvitationsByTenant", (inv, usr) => { if (inv != null && usr != null && usr.Id != 0) { inv.InvitedUser = usr; } return(inv); }, new { Tenant_ID = Tenant.Id, Include_Inapplicable = IncludeInapplicableInvitations }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(tenantInvitations); }
protected override async Task <IEnumerable <Product> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var resultSet = await databaseConnection.QueryMultipleAsync("products.usp_ListPublicProducts", commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); // The first result set is pretty simple - we're just performing a straightforward mapping from a result set to // the list of products. var products = resultSet.Read <Product, RawProductConfiguration, Product>((p, rpc) => { p.DefaultProductConfiguration = rpc; return(p); }); // The second result set is a bit trickier, since we've got to join them together, then // convert from a dynamic object into product tiers IEnumerable <dynamic> productTiers = await resultSet.ReadAsync(); var groupedTiers = from prod in products join pT in productTiers on prod.Id equals pT.ProductId into gj select new { Product = prod, Tiers = (from pT in gj select new Tier() { Id = pT.Id, ExternalId = pT.ExternalId, Name = pT.Name, Visible = pT.Visible, StartDate = pT.StartDate, EndDate = pT.EndDate }).ToArray() }; foreach (var prod in groupedTiers) { prod.Product.Tiers = prod.Tiers; } return(products); }
protected override async Task <IEnumerable <ExternalComponent> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); if (string.IsNullOrWhiteSpace(Purpose.Name)) { throw new ArgumentException("Purpose name must be specified."); } var results = await databaseConnection.QueryAsync <ExternalComponent, Purpose, ExternalComponentStatus, ExternalComponent>("components.usp_GetByStatusProvider", (ec, ecP, ecS) => { ec.Purpose = ecP; ec.Status = ecS; return(ec); }, new { Purpose_Name = Purpose.Name }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
protected override async Task <IEnumerable <SecurableResource> > OnExecute(IReadableDataSource dataSource) { if (CurrentUser.Id == 0) { throw new ArgumentOutOfRangeException(nameof(CurrentUser), "CurrentUser.Id must have a value"); } if (Tenant != null && Tenant.Id == 0) { throw new ArgumentOutOfRangeException(nameof(Tenant), "Tenant.Id must have a value"); } var databaseConnection = await dataSource.GetDbConnection(); var resultantFilter = NameFilter.Replace("*", "%"); var matchingObjects = await databaseConnection.QueryAsync("security.usp_GetSecurableResourcesByName", new { Name_Filter = resultantFilter, User_ID = CurrentUser.Id, Tenant_ID = Tenant?.Id }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return((from sr in matchingObjects select SecurableResourceHelpers.GetSecurableResourceFromDatabase(sr) as SecurableResource) .ToArray()); }
protected override async Task <IEnumerable <ProductCharacteristic> > OnExecute(IReadableDataSource dataSource) { var databaseConnection = await dataSource.GetDbConnection(); var results = await databaseConnection.QueryAsync <ProductCharacteristic>("products.usp_ListAllCharacteristics", commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); return(results); }
protected override async Task <IEnumerable <EmailBatch> > OnExecute(IReadableDataSource dataSource) { if (EmailBatch != null && EmailBatch.Id == 0) { throw new ArgumentNullException(nameof(EmailBatch), "EmailBatch.Id must have a value."); } if (EmailBatchType != null && EmailBatchType.Id == 0) { throw new ArgumentNullException(nameof(EmailBatchType), "EmailBatchType.Id must have a value."); } var databaseConnection = await dataSource.GetDbConnection(); var toAddressHashes = new List <ToAddressHash>(); // We don't know *exactly* what the possible hash is, so calculate a number of possibilities if (!string.IsNullOrWhiteSpace(EmailAddress)) { // We need to have a start time for this if (!StartTime.HasValue) { throw new ArgumentNullException(nameof(StartTime), "If EmailAddress contains a value, StartTime must also contain a value."); } var endTime = EndTime.HasValue ? EndTime.Value : DateTimeOffset.UtcNow; for (var currTime = StartTime.Value; currTime <= endTime; currTime = currTime.AddMonths(1)) { // Work out the month's salt, then calculate the email address hash var saltBytes = Encoding.UTF8.GetBytes(currTime.ToString("yyyy-MM..MM-yyyy")); var hashString = _hashCalculator.CalculateHash(EmailAddress.Trim().ToLower(), saltBytes); toAddressHashes.Add(new ToAddressHash(saltBytes, hashString)); } } var getHistoryReader = await databaseConnection.QueryMultipleAsync("core.usp_GetEmailHistory", new { Start_Time = StartTime, End_Time = EndTime, Email_Batch_ID = EmailBatch?.Id, Email_Batch_Type_ID = EmailBatchType?.Id, External_Message_ID = ServiceBusMessageId, To_Address_Hashes = toAddressHashes.AsTableValuedParameter("core.udt_ToAddressHash") }, commandType : System.Data.CommandType.StoredProcedure, commandTimeout : 30); var dynBatchRecords = await getHistoryReader.ReadAsync(); var emailBatches = (from eb in dynBatchRecords select new EmailBatch() { Id = eb.Id, ServiceBusMessageId = eb.ServiceBusMessageId, DateFirstProcessed = eb.DateFirstProcessed, DateLastProcessed = eb.DateLastProcessed, ProcessedSuccessfully = eb.ProcessedSuccessfully, Type = new EmailBatchType() { Id = eb.TypeId, Name = eb.TypeName } }).ToArray(); var ebDictionary = emailBatches.ToDictionary(eb => eb.Id); var dynEmailRecords = await getHistoryReader.ReadAsync(); // Group the email records up into their corresponding batches foreach (var batch in emailBatches) { batch.Emails = (from eml in dynEmailRecords where eml.EmailBatchId == batch.Id select new Jibberwock.DataModels.Core.Email() { Id = eml.Id, SendDate = eml.SendDate, ExternalEmailId = eml.ExternalEmailId }).ToArray(); } return(emailBatches); }
public NotificationEmailBatchTypeHandler(ILogger logger, IReadableDataSource dataSource, EmailBatch emailBatch, SendGridConfiguration sendGridConfiguration) : base(logger, dataSource, emailBatch, sendGridConfiguration) { }