示例#1
0
        public object Post(CreateLicense request)
        {
            var license = new Model.License().PopulateWith(request);

            documentSession.Store(license);
            documentSession.SaveChanges();

            return
                new HttpResult(new LicenseDto
                                   {
                                       Customer = documentSession.Load<Customer>(license.CustomerId),
                                       Product =
                                           new ProductDto().PopulateWith(documentSession.Load<Product>(license.ProductId))
                                   }.PopulateWith(license))
                    {
                        StatusCode = HttpStatusCode.Created,
                        Headers =
                            {
                                {HttpHeaders.Location, Request.AbsoluteUri.CombineWith(license.Id)}
                            }
                    };
        }
示例#2
0
        public void ExecuteCheckout(Customer purchasingCustomer, Customer owningCustomer)
        {
            var currentUser = context.GetUser(HttpContext.Current.User.Identity);

            //Add PurchasingCustomer if none existing
            if (purchasingCustomer.ObjectId == new Guid())
            {
                context.Customers.Add(purchasingCustomer);
                context.SaveChanges();
                if (!currentUser.IsVendorAdmin)
                {
                    context.UserCustomerRights.Add(new UserCustomerRight
                    {
                        RightObject = purchasingCustomer,
                        RightId = EditEntityMembers.Id,
                        UserId = currentUser.UserId
                    });
                    context.SaveChanges();
                }
            }

            //Add OwningCustomer if none existing
            if (owningCustomer != purchasingCustomer)
            {
                if (owningCustomer.ObjectId == new Guid())
                {
                    context.Customers.Add(owningCustomer);
                    context.SaveChanges();
                    if (!currentUser.IsVendorAdmin)
                    {
                        context.UserCustomerRights.Add(new UserCustomerRight
                        {
                            RightObject = owningCustomer,
                            RightId = EditEntityInfo.Id,
                            UserId = currentUser.UserId
                        });
                        context.SaveChanges();
                    }
                }
            }

            //Create licenses for every transactionitem
            foreach (var item in Transaction.TransactionItems)
            {
                if (item.License == null)
                {
                    var newLicense = new Model.License(item.Sku, owningCustomer, purchasingCustomer);
                    context.Licenses.Add(newLicense);

                    item.License = newLicense;
                }
            }
            context.SaveChanges();

            var existingCustomerApps = (from x in context.Licenses
                join y in context.LicenseCustomerApps on x.ObjectId equals y.LicenseId
                where x.OwningCustomerId == owningCustomer.ObjectId
                select y.CustomerApp).ToList();

            //Add to any existing apps
            if (existingCustomerApps.Any())
            {
                foreach (var customerApp in existingCustomerApps)
                {
                    customerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                }
                context.SaveChanges();
            }
            else
            {
                //Create default application containing all licenses
                var newCustomerApp = new CustomerApp()
                {
                    ApplicationName = owningCustomer.Name + "_Application"
                };
                context.CustomerApps.Add(newCustomerApp);
                newCustomerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                newCustomerApp.CustomerAppKeys.Add(new CustomerAppKey());
            }
            Transaction.Status = TransactionStatus.Complete;
            context.SaveChanges();
        }