Exemple #1
0
        /**
         * This function asks for two database connections that are open
         * simultaneously. It uses them one at a time, but this seems like
         * the only way to accept both contexts and let the consumer share
         * the contexts with other functions.
         */
        public static Models.Customer GetCustomerByKey(
            SizeUp.Data.API.APIContext apiContext,
            SizeUp.Data.SizeUpContext sizeupContext,
            Guid key)
        {
            Models.Customer customer;
            Int64[]         serviceAreaReferences;

//            using (var context = ContextFactory.APIContext)
//            {
            apiContext.ContextOptions.LazyLoadingEnabled = false;
            customer = apiContext.APIKeys.Where(i => i.KeyValue == key && i.IsActive)
                       .Select(k => new Models.Customer
            {
                Id   = k.Id,
                Name = k.Name
            })
                       .FirstOrDefault();
            if (customer == null)
            {
                throw new System.Data.ObjectNotFoundException(key.ToString());
            }

            customer.Domains = apiContext.APIKeyDomains.Where(d => d.APIKeyId == customer.Id)
                               .Select(d => d.Domain).ToArray();
            customer.IdentityProviders = apiContext.IdentityProviders.Where(d => d.APIKeyId == customer.Id)
                                         .Select(idp => new Models.IdentityProvider
            {
                EntryPoint = idp.EntryPoint,
                Name       = idp.Name
            }).ToArray();
            serviceAreaReferences = apiContext.ServiceAreas.Where(area => area.APIKeyId == customer.Id)
                                    .Select(area => area.GeographicLocationId).ToArray();
//            }

//            using (var context = ContextFactory.SizeUpContext)
//            {
            customer.ServiceAreas = sizeupContext.GeographicLocations
                                    .Join(sizeupContext.Granularities,
                                          loc => loc.GranularityId,
                                          gran => gran.Id,
                                          (loc, gran) => new Models.ServiceArea {
                Granularity = gran.Name, Id = loc.Id, Name = loc.LongName
            })
                                    .Where(svcArea => serviceAreaReferences.Contains(svcArea.Id))
                                    .ToArray();
//            }
            return(customer);
        }
Exemple #2
0
 public Core.DataLayer.Models.Customer GetCustomer(SizeUp.Data.API.APIContext context, Guid key)
 {
     Core.DataLayer.Models.Customer customer = null;
     using (var sizeupContext = ContextFactory.SizeUpContext)
     {
         try
         {
             customer = SizeUp.Core.DataLayer.Customer.GetCustomerByKey(context, sizeupContext, key);
         }
         catch (System.Data.ObjectNotFoundException exc)
         {
             // This is actually an error, but the error is a real possibility and I don't
             // want it to abort the function. An entire API refactor is planned, which will
             // eventually remove the possibility of failure here.
             // TODO: if we get a logging framework, log the error.
         }
     }
     return(customer);
 }