// GET: Admin/SubscriptionProduct/Edit/5
        public async Task <ActionResult> Edit(int?subscriptionId, int?productId)
        {
            if (subscriptionId == null || productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await GetSubscriptionProduct(subscriptionId, productId);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            return(View(await subscriptionProduct.Convert(db)));
        }
Beispiel #2
0
        public async Task <ActionResult> Edit([Bind(Include = "ProductId,SubscriptionId, OldProductId, OldSubscriptionId")]
                                              SubscriptionProduct subscriptionProduct)
        {
            if (ModelState.IsValid)
            {
                var canChange = await subscriptionProduct.CanChange(db);

                if (canChange)
                {
                    return(RedirectToAction("Index"));
                }
            }
            return(View(subscriptionProduct));
        }
        // Take one SubscriptionProduct and Convert into one SubscriptionProductMdoel
        public static async Task <SubscriptionProductModel> Convert
            (this SubscriptionProduct subscriptionProduct, ApplicationDbContext db)
        {
            // Link using linq
            var model = new SubscriptionProductModel
            {
                ProductId      = subscriptionProduct.ProductId,
                SubscriptionId = subscriptionProduct.SubscriptionId,
                Products       = await db.Products.ToListAsync(),
                Subscriptions  = await db.Subscriptions.ToListAsync()
            };

            return(model);
        }
        public static async Task <bool> CanChange(
            this SubscriptionProduct subscriptionProduct,
            ApplicationDbContext db)
        {
            var oldSP = await db.SubscriptionProducts.CountAsync(sp =>
                                                                 sp.ProductId.Equals(subscriptionProduct.OldProductId) &&
                                                                 sp.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            var newSP = await db.SubscriptionProducts.CountAsync(sp =>
                                                                 sp.ProductId.Equals(subscriptionProduct.ProductId) &&
                                                                 sp.SubscriptionId.Equals(subscriptionProduct.SubscriptionId));

            return(oldSP.Equals(1) && newSP.Equals(0));
        }
Beispiel #5
0
        public static async Task <SubscriptionProductModel> Convert(this SubscriptionProduct subscriptionProduct, ApplicationDbContext db, bool addListData = true)
        {
            var model = new SubscriptionProductModel
            {
                SubscriptionId    = subscriptionProduct.SubscriptionId,
                ProductId         = subscriptionProduct.ProductId,
                Subscriptions     = addListData ? await db.Subscriptions.ToListAsync() : null,
                Products          = addListData ? await db.Products.ToListAsync() : null,
                SubscriptionTitle = (await db.Subscriptions.FirstOrDefaultAsync(i => i.Id.Equals(subscriptionProduct.SubscriptionId))).Title,
                ProductTitle      = (await db.Products.FirstOrDefaultAsync(p => p.Id.Equals(subscriptionProduct.ProductId))).Title
            };

            return(model);
        }
        public async Task <ActionResult> Create([Bind(Include = "Id,ProductId,SubscriptionId")] SubscriptionProduct subscriptionProduct)
        {
            if (ModelState.IsValid)
            {
                db.SubscriptionProducts.Add(subscriptionProduct);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.ProductId      = new SelectList(db.Products, "Id", "Title", subscriptionProduct.ProductId);
            ViewBag.SubscriptionId = new SelectList(db.Subscriptions, "Id", "Title", subscriptionProduct.SubscriptionId);
            return(View(subscriptionProduct));
        }
Beispiel #7
0
        public async Task <ActionResult> Create([Bind(Include = "ProductId,SubscriptionId")] SubscriptionProduct subscriptionProduct)
        {
            if (ModelState.IsValid)
            {
                db.SubscriptionProducts.Add(subscriptionProduct);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            var model = await subscriptionProduct.Convert(db);

            return(View(model));
        }
        // GET: Admin/SubscriptionProducts/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await db.SubscriptionProducts.FindAsync(id);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            return(View(subscriptionProduct));
        }
Beispiel #9
0
        public static async Task <bool> CanChange(this SubscriptionProduct subscriptionProduct, ApplicationDbContext db)
        {
            if (subscriptionProduct == null || db == null)
            {
                return(false);
            }

            //check current is available
            var oldSP = await db.SubscriptionProducts.CountAsync(sp => sp.ProductId.Equals(subscriptionProduct.OldProductId) && sp.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            //make sure that new is not already selected
            var newSP = await db.SubscriptionProducts.CountAsync(sp => sp.ProductId.Equals(subscriptionProduct.ProductId) && sp.SubscriptionId.Equals(subscriptionProduct.SubscriptionId));

            return(oldSP.Equals(1) && newSP.Equals(0));
        }
        // GET: Admin/SubscriptionProduct/Details/5
        public async Task <ActionResult> Details(int?subscriptionId, int?productId)
        {
            // send error message if parameter is missing
            if (subscriptionId == null || productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await GetSubscriptionProduct(subscriptionId, productId);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            return(View(await subscriptionProduct.Convert(db)));
        }
        // GET: Admin/SubscriptionProduct/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await db.SubscriptionProducts.FindAsync(id);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            ViewBag.ProductId      = new SelectList(db.Products, "Id", "Title", subscriptionProduct.ProductId);
            ViewBag.SubscriptionId = new SelectList(db.Subscriptions, "Id", "Title", subscriptionProduct.SubscriptionId);
            return(View(subscriptionProduct));
        }
        // GET: Admin/SubscriptionProduct/Details/5
        public async Task <ActionResult> Details(int?SubscriptionId, int?productId)
        {
            if (SubscriptionId == null || productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await GetSubscriptionProduct(SubscriptionId, productId);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            var model = await subscriptionProduct.Convert(db);

            return(View(model));
        }
        public async Task <ActionResult> Edit([Bind(Include = "ProductId,SubscriptionId,OldProductId,OldSubscriptionId")] SubscriptionProduct subscriptionProduct)
        {
            if (this.ModelState.IsValid)
            {
                var canChange = await subscriptionProduct.CanChange(this.db);

                if (canChange)
                {
                    await subscriptionProduct.Change(this.db);
                }

                return(this.RedirectToAction("Index"));
            }

            return(this.View(await subscriptionProduct.Convert(this.db)));
        }
        public async Task <ActionResult> Edit([Bind(Include = "ProductId,SubscriptionId,oldProductId,oldSubscriptionId")] SubscriptionProduct subscriptionProduct)
        {
            if (ModelState.IsValid)
            {
                var canChange = await subscriptionProduct.CanChange(db);

                if (canChange)
                {
                    await subscriptionProduct.Change(db);
                }
                return(RedirectToAction("Index"));

                //db.Entry(subscriptionProduct).State = EntityState.Modified;
                //await db.SaveChangesAsync();
                //return RedirectToAction("Index");
            }
            return(View(subscriptionProduct));
        }
Beispiel #15
0
        private async Task <SubscriptionProduct> GetSubscriptionProduct(int?subscriptionId, int?productId)
        {
            try
            {
                int sbsId = 0;
                int prdId = 0;

                int.TryParse(subscriptionId.ToString(), out sbsId);
                int.TryParse(productId.ToString(), out prdId);

                SubscriptionProduct subscriptionProduct = await db.SubscriptionProducts.FirstOrDefaultAsync(sp => sp.ProductId.Equals(prdId) && sp.SubscriptionId.Equals(sbsId));

                return(subscriptionProduct);
            }
            catch
            {
                return(null);
            }
        }
        // Definetly should go to DAL
        private async Task Change(SubscriptionProduct productItem)
        {
            var currentProductDb = await db.SubscriptionProducts.FirstOrDefaultAsync(pi =>
                                                                                     pi.SubscriptionId == productItem.OldSubscriptionId && pi.ProductId == productItem.OldProductId);

            var isNewItemAlreadyExist = await db.SubscriptionProducts.AnyAsync(pi =>
                                                                               pi.SubscriptionId == productItem.SubscriptionId && pi.ProductId == productItem.ProductId);

            if (currentProductDb != null && !isNewItemAlreadyExist)
            {
                using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    db.SubscriptionProducts.Remove(currentProductDb);
                    db.SubscriptionProducts.Add(productItem);
                    await db.SaveChangesAsync();

                    transaction.Complete();
                }
            }
        }
        // GET: Admin/SubscriptionProduct/Delete/5
        public async Task <ActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SubscriptionProduct subscriptionProduct = await db.SubscriptionProducts.FindAsync(id);

            if (subscriptionProduct == null)
            {
                return(HttpNotFound());
            }
            var viewModel = new SubscriptionProductViewModel
            {
                Products            = db.Products.ToList(),
                Subscriptions       = db.Subscriptions.ToList(),
                SubscriptionProduct = db.SubscriptionProducts.SingleOrDefault(sp => sp.Id == id)
            };

            return(View(viewModel));
        }
Beispiel #18
0
        public static async Task Change(this SubscriptionProduct subscriptionProduct, ApplicationDbContext db)
        {
            var oldSubscriptionProduct = await db.SubscriptionProducts
                                         .FirstOrDefaultAsync(sp =>
                                                              sp.ProductId.Equals(subscriptionProduct.OldProductId) &&
                                                              sp.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            var newSubscriptionProduct = await db.SubscriptionProducts
                                         .FirstOrDefaultAsync(sp =>
                                                              sp.ProductId.Equals(subscriptionProduct.ProductId) &&
                                                              sp.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            if (oldSubscriptionProduct != null && newSubscriptionProduct == null)
            {
                newSubscriptionProduct = new SubscriptionProduct
                {
                    SubscriptionId = subscriptionProduct.SubscriptionId,
                    ProductId      = subscriptionProduct.ProductId
                };

                using (var transaction = new TransactionScope(
                           TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        db.SubscriptionProducts.Remove(oldSubscriptionProduct);
                        db.SubscriptionProducts.Add(newSubscriptionProduct);

                        await db.SaveChangesAsync();

                        transaction.Complete();
                    }
                    catch
                    {
                        transaction.Dispose();
                    }
                }
            }
        }
        public static async Task Change(this SubscriptionProduct subscriptionProduct, ApplicationDbContext db)
        {
            // this method performs subscriptionProduct update in database
            var oldSubscriptionProduct = await db.SubscriptionProducts.FirstOrDefaultAsync(pi => pi.ProductId.Equals(subscriptionProduct.OldProductId) && pi.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            var newSubscriptionProduct = await db.SubscriptionProducts.FirstOrDefaultAsync(pi => pi.ProductId.Equals(subscriptionProduct.ProductId) && pi.SubscriptionId.Equals(subscriptionProduct.SubscriptionId));

            if (oldSubscriptionProduct != null && newSubscriptionProduct == null)
            {
                newSubscriptionProduct = new SubscriptionProduct
                {
                    SubscriptionId = subscriptionProduct.SubscriptionId,
                    ProductId      = subscriptionProduct.ProductId
                };

                // using a transaction here to ensure that both actions complete
                // or if one fails then both fail
                using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        // remove the existing SubscriptionProduct
                        db.SubscriptionProducts.Remove(oldSubscriptionProduct);

                        // add the new (changed) SubscriptionProduct
                        db.SubscriptionProducts.Add(newSubscriptionProduct);

                        await db.SaveChangesAsync();

                        transaction.Complete();
                    }
                    catch
                    {
                        transaction.Dispose();
                    }
                }
            }
        }
Beispiel #20
0
        public static async Task Change(this SubscriptionProduct subscriptionProduct, ApplicationDbContext db)
        {
            var oldSubscriptionProduct = await db.SubscriptionProducts.FirstOrDefaultAsync(pi =>
                                                                                           pi.ProductId.Equals(subscriptionProduct.OldProductId) && pi.SubscriptionId.Equals(subscriptionProduct.OldSubscriptionId));

            var newSubscriptionProduct = await db.SubscriptionProducts.FirstOrDefaultAsync(pi =>
                                                                                           pi.ProductId.Equals(subscriptionProduct.ProductId) && pi.SubscriptionId.Equals(subscriptionProduct.SubscriptionId));

            if (oldSubscriptionProduct != null && newSubscriptionProduct == null)
            {
                newSubscriptionProduct = new SubscriptionProduct
                {
                    SubscriptionId = subscriptionProduct.SubscriptionId,
                    ProductId      = subscriptionProduct.ProductId
                };
                // TransactionScope requires a reference to System.Transactions
                using (var transaction = new TransactionScope(
                           TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        // Remove the exisiting ProductItem
                        db.SubscriptionProducts.Remove(oldSubscriptionProduct);
                        // Add the new (changed) ProductItem
                        db.SubscriptionProducts.Add(newSubscriptionProduct);
                        await db.SaveChangesAsync();

                        transaction.Complete();
                    }
                    catch
                    {
                        transaction.Dispose();
                    }
                }
            }
        }
        private async Task <SubscriptionProductViewModel> GetSubscriptionProductModel(SubscriptionProduct subscriptionProduct)
        {
            var model = Mapper.Map <SubscriptionProductViewModel>(subscriptionProduct);

            model.Subscriptions = await db.Subscriptions.ToListAsync();

            model.Products = await db.Products.ToListAsync();

            return(model);
        }
Beispiel #22
0
        public void SubscriptionGeneratesCorrectly()
        {
            var usPublish = new LocalePublishingInformation
            {
                Name           = "Treasure Finders Plus",
                SmallIcon      = new Uri("https://small-icon-uri", UriKind.Absolute).ToString(),
                LargeIcon      = new Uri("https://large-icon-uri", UriKind.Absolute).ToString(),
                Summary        = "1 new members-only adventure each month + over 10 previously released adventures.",
                Description    = "Get access to a new adventure each month for members only, along with a growing collection of over 10 previously released members-only adventures. Includes Crystal Catchers, The Light Stone, The Dark Pool, The Cave Fish, Long Way Down and more.",
                ExamplePhrases = new List <string>
                {
                    "Alexa, open treasure finders plus",
                    "Alexa, play with treasure finders plus"
                },
                Keywords = new List <string> {
                    "Games"
                },
                CustomProductPrompts = new CustomProductPrompts
                {
                    PurchasePromptDescription = "{PREMIUM_CONTENT_TITLE} includes access to all of our current adventures and a new one each month.",
                    BoughtCardDescription     = "Enjoy {PREMIUM_CONTENT_TITLE}! Ask for a list of adventures to explore your purchase.."
                }
            };

            var sub = new SubscriptionProduct
            {
                ReferenceName           = "treasure_finders_plus",
                SubscriptionInformation = new SubscriptionInformation
                {
                    SubscriptionPaymentFrequency = SubscriptionFrequency.Monthly, TrialPeriodDays = 7
                },
                PublishingInformation = new PublishingInformation
                {
                    DistributionCountries = new[] { "US" }.ToList(),
                Locales = new Dictionary <string, LocalePublishingInformation>
                    {
                        { "en-US", usPublish }
                    },
                AmazonMarketplace = new MarketplacePricing
                    {
                        ReleaseDateUtc      = DateTime.Parse("2018-12-28T01:25Z").ToUniversalTime(),
                        DefaultPriceListing = new PriceListing
                        {
                            Price        = (decimal)1.99,
                            CurrencyCode = "USD"
                        }
                    },
                TaxInformation = new TaxInformation(TaxCategory.Software)
                },
                PrivacyAndCompliance = new PrivacyAndCompliance {
                    Locales = new Dictionary <string, LocalePrivacyAndCompliance>
                    {
                        { "en-US", new LocalePrivacyAndCompliance(new Uri("https://url-to-privacy-policy").ToString()) }
                    }
                },
                TestingInstructions = "Sample testing instructions.",
                PurchasableState    = PurchasableState.Purchasable
            };

            Assert.True(Utility.CompareJson(sub, "subscription.json"));
        }