Ejemplo n.º 1
0
        public ActionResult _getCount(int count)
        {
            var isCLickable = false;
            var newValue    = count + 1;

            using (var _counter = new CounterDbEntities())
            {
                var recCount = _counter.Counters.Count();
                if (recCount == 0)
                {
                    var counter = new CounterEntity {
                        Counter = newValue
                    };
                    _counter.Counters.Add(counter);
                    isCLickable = true;
                }
                else
                {
                    var currentValue = _counter.Counters.First();


                    if (currentValue.Counter <= 9)
                    {
                        currentValue.Counter = newValue;
                        isCLickable          = true;
                    }
                }

                _counter.SaveChanges();
            }
            return(Json(new { clickable = isCLickable, Counter = newValue }));
        }
Ejemplo n.º 2
0
 public CounterModel(string key)
 {
     _counter = new CounterEntity
     {
         Id  = Guid.NewGuid(),
         Key = key
     };
 }
Ejemplo n.º 3
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "Next/{nameParameter}")] HttpRequest req,
                                                     string nameParameter, TraceWriter log, ExecutionContext context)
        {
            log.Verbose("C# HTTP trigger function processed a request.");
            string countParameter = req.Query["count"];
            string resetParameter = req.Query["reset"];
            string userId         = req.Query["code"];

            // User ID from code query param of x-functions-key header
            if (userId == null)
            {
                userId = req.Headers["x-functions-key"].ToString();
            }
            if (userId == string.Empty)
            {
                userId = "Development";
            }

            // Default count to an increment of one
            if (countParameter == null)
            {
                countParameter = "1";
            }

            // Allow both ?reset=True and ?reset to reset counter
            if (resetParameter == null)
            {
                resetParameter = bool.FalseString;
            }
            else if (resetParameter == string.Empty)
            {
                resetParameter = bool.TrueString;
            }

            // Bail early on bad inputs
            if (nameParameter == null)
            {
                return(new BadRequestObjectResult("The name parameter must be present"));
            }

            if (!System.Int64.TryParse(countParameter, out System.Int64 counterIncrement) || counterIncrement < 0)
            {
                return(new BadRequestObjectResult($"count value must be a positive integer: \"{countParameter}\""));
            }

            if (!bool.TryParse(resetParameter, out bool counterReset))
            {
                return(new BadRequestObjectResult($"reset value must be true/false: \"{resetParameter}\""));
            }

            // Normalize inputs
            nameParameter = nameParameter.ToLower();
            userId        = userId.ToLower();


            // Get a reference to the table and create it as needed
            CloudTable table = getTableClient(context);

            if (await table.CreateIfNotExistsAsync())
            {
                log.Info($"Created table {CounterEntity.tableName}");
            }

            // Read the current counter record if it exists
            TableOperation tableOperation  = TableOperation.Retrieve(userId, nameParameter);
            TableResult    retrievedResult = await table.ExecuteAsync(tableOperation);

            // TODO: Loop on concurrancy errors
            CounterEntity counterEntity;
            string        operationMessage;

            if (retrievedResult.Result == null)
            {
                // First time -- do an insert.  If another process inserts first, this fails (appropriately)
                operationMessage = $"Creating new entry for {userId}:{nameParameter} = {counterIncrement}";
                counterEntity    = new CounterEntity(userId, nameParameter, counterIncrement);
                tableOperation   = TableOperation.Insert(counterEntity, true);
            }
            else
            {
                // Update the current value
                DynamicTableEntity namedCounter = (DynamicTableEntity)retrievedResult.Result;
                namedCounter.Properties.TryGetValue("CurrentCount", out EntityProperty counterProperty);
                System.Int64 updatedCount = counterReset ? 0 : (System.Int64)counterProperty.Int64Value;
                updatedCount    += counterIncrement;
                operationMessage = $"Updating {userId}:{nameParameter} = {counterIncrement}";
                counterEntity    = new CounterEntity(userId, nameParameter, updatedCount)
                {
                    ETag = retrievedResult.Etag
                };
                tableOperation = TableOperation.Replace(counterEntity);
            }

            // Execute the table operation -- Insert or Replace
            TableResult tableResult = await table.ExecuteAsync(tableOperation);

            log.Info($"Status: {tableResult.HttpStatusCode}.  {operationMessage}");
            if (tableResult.HttpStatusCode < 300)
            {
                return((ActionResult) new OkObjectResult($"{counterEntity.CurrentCount}"));
            }
            else
            {
                return((ActionResult) new StatusCodeResult(tableResult.HttpStatusCode));
            }
        }
Ejemplo n.º 4
0
 public CounterModel(CounterEntity counter)
 {
     _counter = counter;
 }