コード例 #1
0
        public async Task <bool> PostCreateNewMultiUserCode(dynamic data)
        {
            const string APP_KEY    = "X-AppKey";
            const string APP_SECRET = "X-Token";

            systemappuser user    = null;
            UserProfile   profile = null;

            if (Request.Headers.Contains(APP_KEY) && Request.Headers.Contains(APP_SECRET))
            {
                string appKey    = Request.Headers.GetValues(APP_KEY).First();
                string appSecret = Request.Headers.GetValues(APP_SECRET).First();

                using (var sysapps = new userappsEntities())
                {
                    user = sysapps.systemappusers.Where(usr => usr.appSecret.Equals(appSecret) && usr.apptoken.Equals(appKey)).FirstOrDefault();


                    if (user == null)
                    {
                        return(false);
                    }
                    else
                    {
                        using (var exgrip = new exgripEntities())
                        {
                            profile = exgrip.UserProfiles.Where(up => up.UserId == user.systemuserid).FirstOrDefault();

                            if (profile == null)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            else
            {
                return(false);
            }

            WordGenerator gen = new WordGenerator();

            var userId         = profile.AlternateUserId;
            var timeZone       = (string)data.timeZone;
            var amountOfUsers  = (int)data.count;
            var dateString     = (string)data.dateString;
            var dateStringFrom = (string)data.dateStringFrom;
            var codeLink       = (string)data.codeLink;
            var count          = (int)data.count;

            int betacount = 200;

            if (string.IsNullOrEmpty(userId) || string.IsNullOrWhiteSpace(userId))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(timeZone) || string.IsNullOrWhiteSpace(timeZone))
            {
                return(false);
            }

            if (amountOfUsers <= 0)
            {
                return(false);
            }

            DateTime outDate;

            var parseResult = DateTime.TryParse(dateString, out outDate);

            if (!parseResult)
            {
                return(false);
            }


            DateTime outDate2;

            var parseResult2 = DateTime.TryParse(dateStringFrom, out outDate2);

            if (!parseResult2)
            {
                return(false);
            }

            if (DateHelpers.GetTimeZoneInfoForTzdbId(timeZone) == null)
            {
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(codeLink))
            {
                Uri  uriResult;
                bool result = Uri.TryCreate(codeLink, UriKind.Absolute, out uriResult);

                if (!result)
                {
                    return(false);
                }
            }

            if (amountOfUsers == 0 || amountOfUsers <= 0 || amountOfUsers > int.MaxValue)
            {
                return(false);
            }

            using (userappsEntities ctx = new userappsEntities())
            {
                try
                {
                    var customerTime = TimeZoneInfo.ConvertTime(new DateTime(outDate.Year, outDate.Month, outDate.Day, outDate.Hour, outDate.Minute, outDate.Second),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var customerTime2 = TimeZoneInfo.ConvertTime(new DateTime(outDate2.Year, outDate2.Month, outDate2.Day, outDate2.Hour, outDate2.Minute, outDate2.Second),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var allMultiCodes = ctx.promotioncodes.Where(x => x.userid == userId && x.ismulticode == true).ToList();


                    if ((allMultiCodes.Count()) > betacount)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if (count > 2000000)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if ((customerTime2.Ticks > customerTime.Ticks))
                    {
                        return(false);
                    }

                    var word = gen.RandomString(7);

                    promotioncode code = new promotioncode();
                    code.created = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    code.redeemed          = false;
                    code.promocode         = word;
                    code.userid            = userId;
                    code.timezone          = timeZone;
                    code.multicodequantity = amountOfUsers;
                    code.validfrom         = customerTime2;
                    code.validuntil        = customerTime;
                    code.GetCodeLink       = codeLink;
                    code.IsActive          = true;
                    code.ismulticode       = true;

                    ctx.promotioncodes.Add(code);

                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }
コード例 #2
0
        public async Task <bool> PostCreateNewStack(dynamic data)
        {
            const string APP_KEY    = "X-AppKey";
            const string APP_SECRET = "X-Token";

            systemappuser user    = null;
            UserProfile   profile = null;

            if (Request.Headers.Contains(APP_KEY) && Request.Headers.Contains(APP_SECRET))
            {
                string appKey    = Request.Headers.GetValues(APP_KEY).First();
                string appSecret = Request.Headers.GetValues(APP_SECRET).First();

                using (var sysapps = new userappsEntities())
                {
                    user = sysapps.systemappusers.Where(usr => usr.appSecret.Equals(appSecret) && usr.apptoken.Equals(appKey)).FirstOrDefault();


                    if (user == null)
                    {
                        return(false);
                    }
                    else
                    {
                        using (var exgrip = new exgripEntities())
                        {
                            profile = exgrip.UserProfiles.Where(up => up.UserId == user.systemuserid).FirstOrDefault();

                            if (profile == null)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            else
            {
                return(false);
            }


            int betaCount = 500;

            WordGenerator gen = new WordGenerator();



            var timeZone       = (string)data.timeZone;
            var dateString     = (string)data.dateString;
            var dateStringFrom = (string)data.dateStringFrom;
            var codeLink       = (string)data.codeLink;
            var userId         = profile.AlternateUserId;
            var count          = (int)data.count;



            DateTime outDate;

            var parseResult = DateTime.TryParse(dateString, out outDate);

            if (!parseResult)
            {
                return(false);
            }

            DateTime outDate2;

            var parseResult2 = DateTime.TryParse(dateStringFrom, out outDate2);

            if (!parseResult2)
            {
                return(false);
            }

            if (count > betaCount)
            {
                return(false);
            }

            using (userappsEntities ctx = new userappsEntities())
            {
                try
                {
                    var customerTime = TimeZoneInfo.ConvertTime(new DateTime(outDate.Year, outDate.Month, outDate.Day, outDate.Hour, outDate.Minute, outDate.Second),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    var customerTime2 = TimeZoneInfo.ConvertTime(new DateTime(outDate2.Year, outDate2.Month, outDate2.Day, outDate2.Hour, outDate2.Minute, outDate2.Second),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone),
                                                                 DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));

                    if ((customerTime2.Ticks > customerTime.Ticks))
                    {
                        return(false);
                    }

                    var reedemedVouchers = ctx.promotioncodes.Where(x => x.userid == userId && x.redeemed == true && x.ismulticode == false).ToList();

                    var allOnetimes = ctx.promotioncodes.Where(x => x.userid == userId && x.ismulticode == false).ToList();

                    if ((count + allOnetimes.Count()) > betaCount)
                    {
                        throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                    }

                    if ((allOnetimes.Count() == betaCount) && (reedemedVouchers.Count < betaCount) && (reedemedVouchers.Count != 0))
                    {
                        return(false);
                    }
                    else
                    {
                        ctx.Configuration.AutoDetectChangesEnabled = false;
                        ctx.Configuration.ValidateOnSaveEnabled    = false;

                        for (int i = 1; i <= count; i++)
                        {
                            var word = gen.RandomString(7);

                            promotioncode code = new promotioncode();

                            code.created     = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, DateHelpers.GetTimeZoneInfoForTzdbId(timeZone));
                            code.validfrom   = customerTime2;
                            code.validuntil  = customerTime;
                            code.redeemed    = false;
                            code.promocode   = word;
                            code.userid      = userId;
                            code.ismulticode = false;
                            code.timezone    = timeZone;
                            code.GetCodeLink = codeLink;
                            code.IsActive    = true;
                            ctx.promotioncodes.Add(code);
                        }

                        await ctx.SaveChangesAsync();

                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }