Exemple #1
0
        public async Task <Result <string> > GetNextSponsoringCode(CancellationToken token)
        {
            var table = _cloudStorageAccount.CreateCloudTableClient()
                        .GetTableReference(_storageOptions.Tables.SponsoringCodes);
            await table.CreateIfNotExistsAsync(token);

            var opts = new Powells.CouponCode.Options
            {
                PartLength = _sponsoringOptions.CodeLength, Parts = _sponsoringOptions.CodeParts
            };
            var ccb      = new CouponCodeBuilder();
            var badWords = ccb.BadWordsList;
            var code     = "";

            var concurrentUpdateError = false;
            var retry = 0;

            do
            {
                try
                {
                    code = ccb.Generate(opts);

                    var tableResults =
                        await table.ExecuteAsync(TableOperation.Retrieve <SponsoringTableEntity>("code", code), token);

                    if ((SponsoringTableEntity)tableResults?.Result != null)
                    {
                        concurrentUpdateError = true;
                    }
                    else
                    {
                        await table.ExecuteAsync(TableOperation.Insert(new SponsoringTableEntity
                        {
                            PartitionKey = "code",
                            RowKey       = code,
                        }), token);
                    }

                    concurrentUpdateError = false;
                }
                catch (StorageException e)
                {
                    retry++;
                    if (retry <= 10 && (e.RequestInformation.HttpStatusCode == 412 ||
                                        e.RequestInformation.HttpStatusCode == 409))
                    {
                        concurrentUpdateError = true;
                    }
                    else
                    {
                        throw;
                    }
                }
            } while (concurrentUpdateError);

            return(Success <string>(code));
        }
Exemple #2
0
        //Setup Coupon Generator Options
        public CouponController()
        {
            _opts = new Options
            {
                Parts      = 2,
                PartLength = 4
            };

            //Initiating Coupon Code Builder
            _ccb = new CouponCodeBuilder();
        }